博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android CheckBox
阅读量:2533 次
发布时间:2019-05-11

本文共 6862 字,大约阅读时间需要 22 分钟。

Today we will implement android checkbox in a ListView. If you haven’t yet implemented a ListView using Custom Adapter then refer .

今天,我们将在ListView中实现android复选框。 如果尚未使用“自定义适配器”实现ListView,请参阅 。

Android复选框 (Android Checkbox)

Below image shows the project structure of our android checkbox example application.

下图显示了我们的android复选框示例应用程序的项目结构。

Android Checkbox示例 (Android Checkbox example)

The code for the activity_main.xml layout file is given below.

下面给出了activity_main.xml布局文件的代码。

The layout for each row in the list is defined in row_item.xml as below.

列表中每一行的布局在row_item.xml定义如下。

The DataModel.java class for the ListView is defined as shown below.

ListView的DataModel.java类的定义如下所示。

package com.journaldev.listviewwithcheckboxes;public class DataModel {    public String name;    boolean checked;    DataModel(String name, boolean checked) {        this.name = name;        this.checked = checked;    }}

The boolean parameter checked would be used for checking and unchecking checkboxes.

checked的布尔参数将用于选中和取消选中复选框。

The MainActivity.java class file is given below.

MainActivity.java类文件如下。

package com.journaldev.listviewwithcheckboxes;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {    ArrayList dataModels;    ListView listView;    private CustomAdapter adapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        listView = (ListView) findViewById(R.id.listView);        dataModels = new ArrayList();        dataModels.add(new DataModel("Apple Pie", false));        dataModels.add(new DataModel("Banana Bread", false));        dataModels.add(new DataModel("Cupcake", false));        dataModels.add(new DataModel("Donut", true));        dataModels.add(new DataModel("Eclair", true));        dataModels.add(new DataModel("Froyo", true));        dataModels.add(new DataModel("Gingerbread", true));        dataModels.add(new DataModel("Honeycomb", false));        dataModels.add(new DataModel("Ice Cream Sandwich", false));        dataModels.add(new DataModel("Jelly Bean", false));        dataModels.add(new DataModel("Kitkat", false));        dataModels.add(new DataModel("Lollipop", false));        dataModels.add(new DataModel("Marshmallow", false));        dataModels.add(new DataModel("Nougat", false));        adapter = new CustomAdapter(dataModels, getApplicationContext());        listView.setAdapter(adapter);        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView parent, View view, int position, long id) {                DataModel dataModel= dataModels.get(position);                dataModel.checked = !dataModel.checked;                adapter.notifyDataSetChanged();            }        });    }}

In the above code, we set the ArrayList of DataModels object to the adapter. Whenever the ListView click listener is invoked we invert the checked value of the respective row and call notifyDataSetChanged() which updates the changes in the adapter class.

在上面的代码中,我们将DataModels对象的ArrayList设置为适配器。 每当调用ListView Click侦听器时,我们都会反转相应行的检查值,并调用notifyDataSetChanged()来更新适配器类中的更改。

The CustomAdapter.java class file for the ListView is given below.

下面给出了ListView的CustomAdapter.java类文件。

package com.journaldev.listviewwithcheckboxes;import android.content.Context;import android.support.annotation.NonNull;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.CheckBox;import android.widget.TextView;import java.util.ArrayList;public class CustomAdapter extends ArrayAdapter {    private ArrayList dataSet;    Context mContext;    // View lookup cache    private static class ViewHolder {        TextView txtName;        CheckBox checkBox;    }    public CustomAdapter(ArrayList data, Context context) {        super(context, R.layout.row_item, data);        this.dataSet = data;        this.mContext = context;    }    @Override    public int getCount() {        return dataSet.size();    }    @Override    public DataModel getItem(int position) {        return dataSet.get(position);    }    @Override    public View getView(int position, View convertView, @NonNull ViewGroup parent) {        ViewHolder viewHolder;        final View result;        if (convertView == null) {            viewHolder = new ViewHolder();            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);            viewHolder.txtName = (TextView) convertView.findViewById(R.id.txtName);            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);            result=convertView;            convertView.setTag(viewHolder);        } else {            viewHolder = (ViewHolder) convertView.getTag();            result=convertView;        }        DataModel item = getItem(position);        viewHolder.txtName.setText(item.name);        viewHolder.checkBox.setChecked(item.checked);        return result;    }}

Let’s see the output when the above application is run.

android checkbox example, android checkbox listview

让我们看看上面的应用程序运行时的输出。

If you’ve followed so far and tried running the application you shall see that the clicking any rows doesn’t highlight it, neither does it change the check status. Only when you specifically click the checkbox it’s toggled. Our goal was to make checkboxes a part of the ListView row layout and not to let it behave independently. The reason for this conflict is:

如果到目前为止,您已经尝试运行该应用程序,则应该看到单击任何行都不会突出显示它,也不会更改检查状态。 仅当您特别单击复选框时,它才会切换。 我们的目标是使复选框成为ListView行布局的一部分,而不是让其独立运行。 发生此冲突的原因是:

Android CheckBox classes have their own set of click and check listeners. In order to avoid an interference between them and the ListView listener we’ve set the following attributes in the CheckBox widget in the xml.

Android CheckBox类具有其自己的单击和检查侦听器集。 为了避免它们与ListView侦听器之间的干扰,我们在xml的CheckBox小部件中设置了以下属性。

  1. android:focusable="false"

    android:focusable="false"
  2. android:focusableInTouchMode="false"

    android:focusableInTouchMode="false"
  3. android:clickable="false"

    android:clickable="false"

These would make the CheckBoxes neither clickable nor focusable separately thereby letting the ListView row clicks function correctly.

这些将使CheckBoxes既不可单击又不可聚焦,从而使ListView行单击正确运行。

The output of the application in action after doing the above fixes is given below.

在完成上述修复后,实际应用程序的输出如下。

That’s all for android checkbox example with ListView. You can download the final android checkbox example project from below link.

这就是带有ListView的android复选框示例的全部内容。 您可以从下面的链接下载最终的android复选框示例项目。

Reference:

参考:

翻译自:

转载地址:http://jhlzd.baihongyu.com/

你可能感兴趣的文章
根据ISBN获取豆瓣API提供的图书信息
查看>>
【转】Python中*args和**kwargs的区别
查看>>
git命令简单使用
查看>>
CODEFORCES 125E MST Company 巧用Kruskal算法
查看>>
C++标准库分析总结(三)——<迭代器设计原则>
查看>>
Ibatis 配置问题
查看>>
Notability 3.1 for Mac 中文共享版 – 好用的文本笔记工具
查看>>
HDU 2089 数位dp入门
查看>>
How do I resolve the CodeSign error: CSSMERR_TP_NOT_TRUSTED?
查看>>
linux下添加定时任务
查看>>
python的第三篇--安装Ubuntu、pycharm
查看>>
LeetCode 1092. Shortest Common Supersequence
查看>>
《区块链技术与应用》北京大学肖臻老师公开课 笔记
查看>>
139句
查看>>
购买《哈利波特》书籍
查看>>
谈谈一些网页游戏失败的原因到底有哪些?(转)
查看>>
备案的问题
查看>>
asp.net下ajax.ajaxMethod使用方法
查看>>
win10+mongodb安装配置
查看>>
window7修改hosts文件
查看>>