OnCheckedChangedListener用于在片段内的listview中加载的复选框

时间:2017-01-04 18:48:20

标签: android listview android-fragments oncheckedchanged

我正在尝试为列表视图中加载的所有复选框声明一个更改侦听器,其中listview也由片段加载,这是我的代码:

片段:

public class AddGroupMembersFragment extends Fragment{

    ArrayList<HashMap<Integer, Member>> groupMembersList;
    ListView lv;
    CheckBox checkBox;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_add_group_members, container, false);
        groupMembersList = new ArrayList<>();
        lv = (ListView) rootView.findViewById(R.id.group_members_list);
        new AddGroupMembersFragment.PostDataAsyncTask().execute();
        // Inflate the layout for this fragment
        return rootView;
    }

    AdapterView.OnItemClickListener checkBoxChanged = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
            // AdapterView is the parent class of ListView
            ListView lv = (ListView) arg0;
            if(lv.isItemChecked(position)){
                if(selectAllSubmit.getVisibility()==View.INVISIBLE){
                    selectAllSubmit.setVisibility(View.VISIBLE);
                }
            }
        }
    };

    public class PostDataAsyncTask extends AsyncTask<String, String, String> {

        ...

        @Override
        protected void onPostExecute(String lenghtOfFile) {
            lv.setAdapter(new GroupMembersListAdapter(getActivity(),groupMembersList,1));
            lv.setOnItemClickListener(checkBoxChanged);
        }
    }

listview item.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:buttonTint="@color/colorPrimary" />
        ...

我想使用oncheckchangedlistener而不是on itemclicklistener,有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您需要创建一个适配器并将其分配给您的列表。

示例:

IsSelected

在你的片段中:

公共类StackFragment extends Fragment实现了CompoundButton.OnCheckedChangeListener {

class MyAdapter extends BaseAdapter { //or ArrayAdapter, or what you prefer
    private final CompoundButton.OnCheckedChangeListener listener;
    MyAdapter(CompoundButton.OnCheckedChangeListener listener) {
        this.listener = listener;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;
        if (view == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item);
        }
        CheckBox checkBox = view.findViewById(R.id.check_box_id);
        checkBox.setOnCheckedChangeListener(listener);
        return view;

    }
}

}