按下android

时间:2016-06-25 08:07:35

标签: android listview android-fragments checkbox baseadapter

IMG

如何在按下按钮时使用BaseAdapter在ListView中显示CheckBox,它会在每个列表项中显示CheckBox(按钮位于ListView外部),当我点击每个列表项中的按钮CheckBox显示时....建议一些的解决方案。

提前致谢

1 个答案:

答案 0 :(得分:0)

试试这个兄弟:

- >类

<强> CustomAdapter.java

import android.app.*;
import android.view.*;
import android.widget.*;
import java.util.*;

public class CustomAdapter extends ArrayAdapter<CustomItem> {
    private List<CustomItem> list;
    private Activity context;
    private ViewHolder holder;
    private List<CheckBox> checksList;

    public CustomAdapter(Activity context, List<CustomItem> list) {
        super(context, R.layout.list_item, list);
        this.context = context;
        this.list = list;
        checksList = new ArrayList<>();
    }

    @Override
    public CustomItem getItem(int pos) {
        return list.get(pos);
    }

    static class ViewHolder {
        public TextView tvName;
        public CheckBox cbCheck;
    }

    public void toggleChecks() {
        if (holder.cbCheck.getVisibility() == View.VISIBLE) {
            for (int i=0; i<checksList.size(); i++)
                checksList.get(i).setVisibility(View.INVISIBLE);
        }
        else {
            for (int i=0; i<checksList.size(); i++)
                checksList.get(i).setVisibility(View.VISIBLE);
        }
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        if (convertView == null) {
            convertView = context.getLayoutInflater().inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
            holder.cbCheck = (CheckBox) convertView.findViewById(R.id.cbCheck);
            holder.cbCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    list.get((Integer) buttonView.getTag()).setCheck(buttonView.isChecked());
                    ((ListView) parent).setItemChecked(position, buttonView.isChecked());
                    notifyDataSetChanged();
                }
            });
            holder.cbCheck.setVisibility(View.INVISIBLE);
            convertView.setTag(holder);
            convertView.setTag(R.id.tvName, holder.tvName);
            convertView.setTag(R.id.cbCheck, holder.cbCheck);
        } else holder = (ViewHolder) convertView.getTag();

        CustomItem item = list.get(position);

        holder.tvName.setText(item.getName());
        holder.cbCheck.setTag(position);
        holder.cbCheck.setChecked(item.getCheck());
        checksList.add(holder.cbCheck);

        return convertView;
    }
}

<强> CustomItem.java

public class CustomItem {
    private String name;
    private boolean check;

    public CustomItem(String name, boolean check) {
        this.name = name;
        this.check = check;
    }

    public String getName() {
        return name;
    }
    public boolean getCheck() {
        return check;
    }

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

<强> Main.java

public class Main extends AppCompatActivity {
    CustomAdapter adapter;

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

        List<CustomItem> itemList = new ArrayList<CustomItem>();
        itemList.add(new CustomItem("ITEM 1", false /* NOT CHECKED */));
        itemList.add(new CustomItem("ITEM 2", false /* NOT CHECKED */));
        itemList.add(new CustomItem("ITEM 3", true  /*   CHECKED   */));
        itemList.add(new CustomItem("ITEM 4", false /* NOT CHECKED */));
        itemList.add(new CustomItem("ITEM 5", true  /*   CHECKED   */));

        adapter = new CustomAdapter(this, itemList);
        ListView listView = (ListView) findViewById(R.id.listView1);
        listView.setAdapter(adapter);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.toggleChecks();
            }
        });
    }
}

- &GT;资源

<强> activity_main.xml中

<?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="match_parent">
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/button" />
</RelativeLayout>

<强> list_item.xml

<?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">

    <CheckBox
        android:text=""
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/cbCheck" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/tvName" />

</LinearLayout>