CheckedTextView使用Demo

由于CheckedTextView使用很少,所以用的不多,之前自己用TextView和CheckBox结合使用过,实现类似的功能。但是不如使用CheckedTextView这么优雅,所以这次做项目的时候就使用这个了,虽然不是新的什么点,但是作为一个总结吧,记录下来!!

直接用代码来看问题:
步骤一:
做主界面的布局,直接使用了一个的ListView,

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:divider="?attr/layoutparentbg"
    android:dividerHeight="1dp"
    android:listSelector="@android:color/transparent"
    android:orientation="vertical"
    android:overScrollMode="never"
    android:scrollbars="none"
    />

步骤二:
自定义该ListView 的样式布局文件:

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

    <ImageView
        android:id="@+id/img_icon"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/intro_common_height"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="@dimen/intro_margin_20"
        android:layout_centerVertical="true"
        android:contentDescription="@string/app_name"
        android:scaleType="fitCenter"
        android:src="@drawable/icon_res_friend" />
    <CheckedTextView
        android:id="@+id/checktv_title"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:gravity="center_vertical"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:paddingLeft="6dip"
        android:filterTouchesWhenObscured="true"
        android:focusableInTouchMode="true"
        android:text="公司"
        android:layout_toRightOf="@+id/img_icon"
        android:layout_marginLeft="@dimen/intro_margin_15"
        android:textSize="12sp"
        android:paddingRight="6dip"
        />

</RelativeLayout>

步骤三:代码实现部分:
适配器:

   class CompanyListAdapter extends BaseAdapter
    {
        private ArrayList<InsuranceCompany> riskArrayList;

        public ArrayList<InsuranceCompany> getRiskArrayList()
        {
            return riskArrayList;
        }

        public CompanyListAdapter(ArrayList<InsuranceCompany> regionArrayList)
        {
            this.riskArrayList = regionArrayList;
            checkedMap = new HashMap<>();
        }

        public void updateRefresh(ArrayList<InsuranceCompany> refreshRegions)
        {
            if (riskArrayList == null)
            {
                riskArrayList = new ArrayList<>();
            }
            this.riskArrayList.clear();
            this.riskArrayList.addAll(refreshRegions);
            this.notifyDataSetChanged();
        }

        @Override
        public int getCount()
        {
            if (riskArrayList == null)
                return 0;
            return riskArrayList.size();
        }

        @Override
        public Object getItem(int position)
        {
            return riskArrayList.get(position);
        }

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

        class Holder
        {
            ImageView img_icon;

            CheckedTextView text_AreaName;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            Holder holder;
            if (convertView == null)
            {
                // android.R.layout.simple_list_item_checked
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_insurancecompany, null);
                holder = new Holder();
                holder.text_AreaName = (CheckedTextView)convertView.findViewById(R.id.checktv_title);
                holder.img_icon = (ImageView)convertView.findViewById(R.id.img_icon);
                convertView.setTag(holder);
            }
            else
            {
                holder = (Holder)convertView.getTag();
            }
            InsuranceCompany region = riskArrayList.get(position);
            holder.text_AreaName.setText(region.name);
            if (checkedMap.get(position) != null && checkedMap.get(position) == true)
            {
                holder.text_AreaName.setChecked(true);
            }
            else
            {
                holder.text_AreaName.setChecked(false);
            }
            holder.text_AreaName.setOnClickListener(new View.OnClickListener()
            {

                @Override

                public void onClick(View v)
                {

                    // TODO Auto-generated method stub

                    CheckedTextView checkedTextView = (CheckedTextView)v;
                    checkedTextView.toggle();
                    checkedMap.put(position, checkedTextView.isChecked());

                }

            });

            Glide.with(mContext).load("http://p4.so.qhimg.com/t01eead9be5f0a448de.jpg").into(holder.img_icon);
            // Glide.with(mContext).load(region.icon).into(holder.img_icon);
            return convertView;
        }
    }

关键部分:必须在适配器里给CheckedTextView 设置监听器,设置选中和未选中的状态

 holder.text_AreaName.setOnClickListener(new View.OnClickListener()
            {

                @Override

                public void onClick(View v)
                {

                    // TODO Auto-generated method stub

                    CheckedTextView checkedTextView = (CheckedTextView)v;
                    checkedTextView.toggle();
                    checkedMap.put(position, checkedTextView.isChecked());

                }

            });

这样,CheckedTextView 选中和未选中,就能进行改变和存储了!
效果图:这里写图片描述

写的不好,希望尽情指正不足!提前感谢

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
HuggingFace是一个开源的自然语言处理(NLP)模型库,提供了各种预训练的AI模型。您可以通过访问HuggingFace的官方网站或GitHub页面来使用它们。引用 使用HuggingFace的Demo非常简单。您可以通过以下步骤来使用HuggingFace的Demo: 1. 首先,您需要打开HuggingFace的官方网站或GitHub页面,找到您感兴趣的Demo模型。 2. 在Demo页面,您可以看到模型的描述和用法说明。这些说明将帮助您理解如何使用Demo模型。 3. 接下来,您可以使用提供的示例代码或命令行指令来运行Demo。这些示例代码或指令通常会在文档中提供。 4. 根据需要,您可以在Demo中输入相应的文本或参数。这些输入将作为模型的输入,模型会根据这些输入生成相应的输出。 举个例子,如果您想使用HuggingFace的ChatGPT模型进行对话,您可以通过编写代码或使用命令行指令来启动Demo。然后,您可以输入要与模型对话的文本,并查看模型生成的响应。引用 另外,HuggingFace还提供了其他类型的Demo。例如,它使用了名为「runwayml/stable-diffusion-1-5」的模型来生成跳舞的猫的图片。您可以根据Demo文档中提供的说明和示例代码来使用这个功能。引用 总结起来,要使用HuggingFace的Demo,您需要找到相应的模型,并按照Demo文档中的说明进行操作。您可以通过提供输入来与模型交互,并查看生成的输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值