Android ListView中的item只能有一个选中的问题和ListView与activity交互的问题

版权声明:清晨不起早,误一天的事;幼年不勤学,误一生的事。 https://blog.csdn.net/Dr_abandon/article/details/79816809

Android中ListView中的item与activity的交互有几种方式:可以用回调,广播等,下面的方式是动态广播的方式

ListView中的item选中事件,比如有多个item,每个item中都有一个CheckBox,我们要只选择其中的一个,这是我们需要给每个item设置一个标记,如果选中,就标记为true,其他的全部改为false,然后再刷新适配器即可。

下面先看效果图

这里写图片描述

TextListActivity .class

public class TextListActivity extends AppCompatActivity {

    public static final String CHECK = "checkbox";
    public static final String NAME = "name";
    private Boolean mCheckable = false;
    TextAdapter adapter;

    private ArrayList<TextCb> mData;

    ListView lv;
    TextView tv_add;
    TextView tv_edit;


    int selectId = -1;


    //广播
    private IntentFilter intentFilter;
    private NetworkChangeReceiver networkChangeReceiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_list);
        lv = findViewById(R.id.lv);
        tv_add = findViewById(R.id.tv_add);
        tv_edit = findViewById(R.id.tv_edit);
        intData();
    }

    private void intData() {
        mData = new ArrayList<>();


        for (int i = 0; i < 10; i++) {
            TextCb textCb = new TextCb("拔罐" + i, false);
            mData.add(textCb);
        }
        adapter = new TextAdapter(this, mData);
        lv.setAdapter(adapter);


        //动态接受网络变化的广播接收器
        intentFilter = new IntentFilter();
        intentFilter.addAction("getSelectId");
        networkChangeReceiver = new NetworkChangeReceiver();
        registerReceiver(networkChangeReceiver, intentFilter);


        tv_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (selectId != -1) {
                    Toast.makeText(getBaseContext(), "选择了" + selectId, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getBaseContext(), "请选择一个项目", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //取消动态网络变化广播接收器的注册
        unregisterReceiver(networkChangeReceiver);
    }

    //自定义接受网络变化的广播接收器
    class NetworkChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();

            selectId = intent.getIntExtra("select", -1);
            if (networkInfo != null && networkInfo.isAvailable()) {

               // Toast.makeText(context, "network is available", Toast.LENGTH_SHORT).show();
            } else {
               // Toast.makeText(context, "network is unavailable", Toast.LENGTH_SHORT).show();
            }
        }
    }



}

适配器

public class TextAdapter extends BaseAdapter {


    int selectId = -1;

    private final Context context;
    private final ArrayList<TextCb> list;


    public TextAdapter(Context context, ArrayList<TextCb> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public TextCb getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        final ViewHolder holder;
        if (view == null) {
            view = View.inflate(context, R.layout.item_text, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else holder = (ViewHolder) view.getTag();

        holder.tv_name.setText(list.get(i).getName());
        holder.cb.setChecked(list.get(i).b);

        holder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.cb.isChecked()) {
                    selectId = i;
                    list.get(i).b = true;
                } else {
                    selectId = -1;
                }

                for (int j = 0; j < list.size(); j++) {
                    if (selectId != j) {
                        list.get(j).b = false;
                    }
                }

                notifyDataSetChanged();

//发送广播
                Intent intent = new Intent("getSelectId").putExtra("select", selectId);
                context.sendBroadcast(intent);
            }
        });


        return view;
    }


    class ViewHolder {
        CheckBox cb;
        TextView tv_name;

        public ViewHolder(View view) {
            cb = view.findViewById(R.id.cb);
            tv_name = view.findViewById(R.id.tv_name1);
        }
    }
}

其他布局,和数据类


//item的

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


    <LinearLayout
        android:id="@+id/ll_test1"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@android:color/white">

        <CheckBox
            android:id="@+id/cb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="18dp" />

        <TextView
            android:id="@+id/tv_name1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="18dp"
            android:text="拔罐" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="0" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginRight="15dp"
            android:text="积分" />

    </LinearLayout>


</LinearLayout>


//显示的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="51dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="20dp"
            android:text="在线项目" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:id="@+id/tv_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="新增"

            />

        <View
            android:layout_width="1px"
            android:layout_height="14dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="22dp" />

        <TextView
            android:id="@+id/tv_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"
            android:text="编辑" />
    </LinearLayout>


    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"></ListView>


</LinearLayout>



//数据类
public class TextCb {

    String name;

    Boolean b;


    public TextCb(String name, Boolean b) {
        this.name = name;
        this.b = b;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Boolean getB() {
        return b;
    }

    public void setB(Boolean b) {
        this.b = b;
    }
}

猜你喜欢

转载自blog.csdn.net/Dr_abandon/article/details/79816809