android点击事件乱序,Android Recyclerview插入过多数据就会导致item重复,乱序

博客讨论了在Android应用中RecyclerView遇到的问题,当item数量超过6个时,出现重复和乱序现象。问题源于布局文件和RecyclerView Adapter的实现。适配器的onBindViewHolder方法需要正确设置item的内容,而点击事件处理可能不正确导致显示异常。解决方案可能涉及适配器的更新逻辑和布局管理器的配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

item数量超过6条就会出现item重复,乱序

代码如下

fragment布局

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

tools:context="invinciblejoe.com.lightingbuy.main.LightingFragment">

android:id="@+id/commodity_list"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

recyclerview item布局

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/invinciblejoe.com.lightingbuy"

android:layout_width="match_parent"

android:layout_height="150dp"

android:layout_marginTop="10dp"

android:layout_marginBottom="10dp"

android:layout_marginLeft="10dp"

android:layout_marginRight="10dp"

app:cardBackgroundColor="@color/orange"

app:cardCornerRadius="20dp">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/pic"

android:layout_width="match_parent"

android:layout_height="120dp"

android:gravity="center"

android:textSize="50sp"

/>

android:clickable="true"

android:id="@+id/name"

android:layout_width="match_parent"

android:layout_height="60dp"

android:background="@color/orange"

android:textColor="@android:color/white" />

recycerview adapter

public class LightingRVAdapter extends RecyclerView.Adapter {

private List mlist;

private Context mContext;

private LightingViewHolder viewHolder;

public LightingRVAdapter(Context mContext, List mlist) {

this.mContext = mContext;

this.mlist = mlist;

}

@Override

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_lighting, parent, false);

viewHolder = new LightingViewHolder(v);

return viewHolder;

}

@Override

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

Commodity c = mlist.get(position);

viewHolder.mImageView.setText(c.getName());

viewHolder.mTextView.setText(String.valueOf(c.getPrice_discont()));

}

@Override

public int getItemCount() {

return mlist == null ? 0 : mlist.size();

}

private class LightingViewHolder extends RecyclerView.ViewHolder

implements View.OnClickListener

{

public TextView mTextView;

public TextView mImageView;

public LightingViewHolder(View v )

{

super(v);

mTextView = (TextView) v.findViewById(R.id.name);

mImageView = (TextView) v.findViewById(R.id.pic);

mImageView.setOnClickListener(this);

}

@Override

public void onClick(View view) {

switch (view.getId()) {

case R.id.pic :

mTextView.setText("OnChlic");

break;

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值