1. 程式人生 > >Android ListView中的item只能有一個選中的問題和ListView與activity互動的問題

Android ListView中的item只能有一個選中的問題和ListView與activity互動的問題

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;
    }
}