如何使用适配器处理项目中视图的单击侦听器?

丹尼尔

我正在尝试使用Adapter进行测验活动并在每个项目中使用多个按钮。onClickListener用于 tstA ImageView单击后,图像发生变化,但其他 tstAImageViews图像也会发生变化(每 4 个项目中一个)。这样做的正确方法是什么?

活动图片

这是我的代码TestAdapter

public class TestsAdapter extends ArrayAdapter<TestsItem>{
    Activity holder;
    int layoutResourceId;
    TestsItem item[];

    public TestsAdapter(Context ctx, int resource, TestsItem[] testItem){
        super (ctx,resource,testItem);
        holder = (Activity)ctx;
        layoutResourceId = resource;
        item = testItem;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        if (convertView==null){
            LayoutInflater inflate = holder.getLayoutInflater();
            convertView = inflate.inflate(layoutResourceId,parent,false);
        }

        final ImageView tstA = (ImageView) convertView.findViewById(R.id.tstA);
        final ImageView tstB = (ImageView) convertView.findViewById(R.id.tstB);
        final ImageView tstC = (ImageView) convertView.findViewById(R.id.tstC);
        final ImageView tstD = (ImageView) convertView.findViewById(R.id.tstD);

        tstA.setBackgroundResource(item[position].Test1);
        tstB.setBackgroundResource(item[position].Test2);
        tstC.setBackgroundResource(item[position].Test3);
        tstD.setBackgroundResource(item[position].Test4);

        tstA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tstA.setImageResource(R.drawable.test_item_blue);
                tstB.setImageResource(R.drawable.test_item_empty);
                tstC.setImageResource(R.drawable.test_item_empty);
                tstD.setImageResource(R.drawable.test_item_empty);
            }
        });

        return  convertView;
    }
}
雷兹·默谢德

假设您item[position].Test1 item[position].Test2是已设置为背景的可绘制资源。

所以你需要onClickListener像这样修改

tstA.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        item[position].Test1 = R.drawable.test_item_blue;
        item[position].Test2 = R.drawable.test_item_empty;
        item[position].Test3 = R.drawable.test_item_empty;
        item[position].Test4 = R.drawable.test_item_empty;

        notifyDataSetChanged();
    }
});

您需要更改在适配器中传递的数组项,以正确跟踪列表项图像。调用notifyDataSetChanged以查看列表中更改的效果。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android:在“项目”上单击“侦听器”以实现列表视图基本适配器实现

使用适配器和数组列表时,我需要在回收视图中的项目上实现单击侦听器类

RecyclerView,适配器,单击侦听器和AsyncTask

如何使用数据绑定在Recycler视图适配器上初始化Click侦听器?

如何在Android中转到ListView适配器项单击侦听器以分段?

使用自定义适配器在AlertDialog中单击侦听器

如何为适配器中的项目设置侦听器?

使用带有参数的自定义绑定适配器侦听器方法的Android自定义视图

将单击侦听器添加到通用RecyclerView适配器

初始化项目选定的侦听器(自定义微调适配器)

如何使用自定义适配器单击列表视图中的项目

Firebase在Android的适配器中添加侦听器

RecyclerView 适配器中的空侦听器

哪个更好:密钥侦听器或密钥适配器?

摇摆。适配器/侦听器与数据绑定

JTable鼠标侦听器鼠标适配器

如何在没有空侦听器的 kotlin 上的适配器上实现点击侦听器?

如何将侦听器从片段传递到适配器?(科特琳)

如何在 OnSuccessListener 中的 recyclerview 适配器中传递侦听器

如何在 recyclerview 适配器上检查空侦听器?

如何处理项目单击侦听器上的行及其内部视图的RecyclerView

自定义回收器视图适配器 - 如何排除某些项目?

用户使用适配器单击时隐藏并显示项目

如何从通过onclick侦听器单击的项目中检索数据

自定义适配器中EditText的文本更改侦听器

侦听器和适配器之间有什么区别?

Spring 集成事件侦听器入站适配器

Android-从片段到适配器的侦听器值?

无法从Recycler View适配器的setOnClick侦听器中启动活动