GridView realizes radio function

Let's see the effect first

If it's what you need, you can keep looking down

It's easy to implement and directly code
The layout of the main Activity. A Button is used to jump to the filtered Activity and a TextView is used to display the filtered results

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.duanlian.gridviewsingleselectordomo.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:onClick="buttonClick"
        android:text="Screening"/>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:textColor="@color/colorAccent"
        android:layout_centerInParent="true"
        android:text="Screening results"/>
</RelativeLayout>

MainActivity your code logic, comments have been added

package com.duanlian.gridviewsingleselectordomo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.tv_result);
    }

    /**
     * button Click monitoring of
     * @param view
     */
    public void buttonClick(View view) {
        //Jump to filtered activity
        Intent intent = new Intent(MainActivity.this, SingleSelectorActivity.class);
        //The value brought to the next page through the jump with return value
        startActivityForResult(intent,888);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 888 && resultCode == 999) {
            //Get the value after finish ing the previous page
            String pos = data.getStringExtra("pos");
            mTextView.setText(pos);
        }


    }
}

Then filter the Activity. The layout is a GridView to display the options to be selected
A Button is used to click to return to the main Activity and take the selected value

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_single_selector"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.duanlian.gridviewsingleselectordomo.SingleSelectorActivity">
<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:onClick="buttonFinish"
    android:text="Select finish"/>
    <GridView
        android:id="@+id/gridView"
        android:layout_below="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="80dp"
        android:horizontalSpacing="5dp"
        android:verticalSpacing="10dp"
        android:numColumns="auto_fit"/>
</RelativeLayout>

And then there's the filtering logic

package com.duanlian.gridviewsingleselectordomo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.List;

public class SingleSelectorActivity extends AppCompatActivity {

    private GridView mGridView;
    private List<String> mList;
    private GridViewAdapter mAdapter;
    int selectorPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_selector);
        initView();
    }

    private void initView() {
        mGridView = (GridView) findViewById(R.id.gridView);
        //Add data
        mList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            mList.add("The first " + i + " individual");
        }
        mAdapter = new GridViewAdapter(this, mList);
        mGridView.setAdapter(mAdapter);
        //gridView click event
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Pass the clicked position to the adapter
                mAdapter.changeState(position);
                selectorPosition = position;
            }
        });
    }

    /**
     * button Click monitoring of
     *
     * @param view
     */
    public void buttonFinish(View view) {
        Intent intent = new Intent();
        intent.putExtra("pos", "The first " + selectorPosition + " individual");
        setResult(999, intent);
        finish();
    }
}

The main state change is still in the Adapter

package com.duanlian.gridviewsingleselectordomo;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

/**
 * gridView adapter
 */

public class GridViewAdapter extends BaseAdapter {
    private Context mContext;
    private List<String> mList;
    private int selectorPosition;

    public GridViewAdapter(Context context, List<String> mList) {
        this.mContext = context;
        this.mList = mList;

    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return mList != null ? position : 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = View.inflate(mContext, R.layout.item_gridview, null);
        RelativeLayout mRelativeLayout = (RelativeLayout) convertView.findViewById(R.id.ll);
        TextView textView = (TextView) convertView.findViewById(R.id.tv);
        textView.setText(mList.get(position));
        //If the current position is equal to the position passed from the click, change its status
        if (selectorPosition == position) {
            mRelativeLayout.setBackgroundResource(R.drawable.grid_shap_two);
            textView.setTextColor(Color.parseColor("#FF4081"));
        } else {
            //Others return to their original state
            mRelativeLayout.setBackgroundResource(R.drawable.grid_shap_one);
            textView.setTextColor(Color.parseColor("#3F51B5"));
        }
        return convertView;
    }


    public void changeState(int pos) {
        selectorPosition = pos;
        notifyDataSetChanged();

    }
}

And item layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <RelativeLayout
        android:id="@+id/ll"
        android:layout_width="100dp"
        android:layout_height="32dp"
        android:layout_centerInParent="true"
        android:layout_margin="5dp"
        android:background="@drawable/grid_shap_one"
        >

        <TextView
            android:id="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:ellipsize="none"
            android:gravity="center"
            android:singleLine="true"
            android:text="Premier League"
            android:maxLength="7"
            android:textColor="@color/colorPrimary"
            android:textSize="13sp"/>
    </RelativeLayout>
</RelativeLayout>

If you want to click the jump to select Activity and the button to be filtered, show that the state of the button that you selected before is selected. It is also very simple. As long as you cache the position after you select it, point it in to select Activity, instantiate Adapter, then use mAdapter.changeState(position); pass the position that you need to change the state to pass.



Tags: Android xml encoding Java

Posted on Thu, 19 Mar 2020 13:38:03 -0400 by mikeatrpi