Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 当一个视图用作模板并且id重复时,如何在单击其父项时选择单个复选框大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我从数据库中收到一系列数据并将其显示一个由复选框和其他一些视图组成的层中.事实上,我已经制作了一个控制自定义.
我的问题是如何在点击任何图层时打开该图层的复选框,其余部分都会关闭.

public class myclass extends LinearLayout {

LinearLayout linear;
public int id;
public static ImageView pic;
public static TextView titlepro;
public static checkBox rdb;

public myclass(Context context) {
    super(context);
    init(context);
}
public myclass(Context context,@Nullable AttributeSet attrs) {
    super(context,attrs);
    init(context);

}
public myclass(Context context,@Nullable AttributeSet attrs,int defStyleAttr) {
    super(context,attrs,defStyleAttr);
    init(context);

}
public void init(Context context){
    LayoutInflater inflater=(LayoutInflater)context.getSystemservice(Context.LAYOUT_INFLATER_serviCE);
    View view=inflater.inflate(R.layout.linear,this,truE);
    linear=(LinearLayout)findViewById(R.id.linear2);
    pic=(ImageView)view.findViewById(R.id.pic);
    titlepro=(TextView)view.findViewById(R.id.txt1);
    rdb=(checkBox)view.findViewById(R.id.rdb1);

    rdb.setOncheckedchangelistener(new CompoundButton.oncheckedchangelistener() {
        @Override
        public void oncheckedChanged(CompoundButton buttonView,Boolean ischecked) {
            if (ischecked){

            }
        }
    });
    linear.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            if (rdb.ischecked()){
                rdb.setchecked(false);
            }else {
                rdb.setchecked(true);
            }
            Toast.makeText(G.context,id+"",Toast.LENGTH_LONG).show();
        }
    });

}}

我的问题是,当您单击图层时,将检查其他图层的复选框,但正确返回ID.

解决方法

问题是因您使用的ID对于所有复选框都是相同的.

有几种方法可以解决这个问题:

1.linear =(的LinearLayout)findViewById(R.id.linear2); – >线性=(的LinearLayout)view.findViewById(R.id.linear2);

如果那不起作用:

2.虑创建一个包含图层中所有视图的新片段.然后,您可以在视图中添加多个.在课程中虑处理图层点击事件.

>使用Recycler视图,然后您将获得点击项目的索引并从那里处理.如果您从db获取条目并且您可能知道可能有多少条目,那么您的UI可能会变得太重.使用Recycler视图将有助于提高性能

大佬总结

以上是大佬教程为你收集整理的android – 当一个视图用作模板并且id重复时,如何在单击其父项时选择单个复选框全部内容,希望文章能够帮你解决android – 当一个视图用作模板并且id重复时,如何在单击其父项时选择单个复选框所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。