问题描述
item数量超过6条就会出现item重复,乱序
代码如下
fragment布局
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' 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.support.v7.widget.RecyclerViewandroid: android:layout_width='match_parent'android:layout_height='wrap_content' /></LinearLayout>
recyclerview item布局
<?xml version='1.0' encoding='utf-8'?><android.support.v7.widget.CardView 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'> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='match_parent'android:orientation='vertical'><TextView android: android:layout_width='match_parent' android:layout_height='120dp' android:gravity='center' android:textSize='50sp' /><TextView android:clickable='true' android: android:layout_width='match_parent' android:layout_height='60dp' android:background='@color/orange' android:textColor='@android:color/white' /> </LinearLayout></android.support.v7.widget.CardView>
recycerview adapter
public class LightingRVAdapter extends RecyclerView.Adapter { private List<Commodity> mlist; private Context mContext; private LightingViewHolder viewHolder; public LightingRVAdapter(Context mContext, List<Commodity> 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.ViewHolderimplements 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);}@Overridepublic void onClick(View view) { switch (view.getId()) {case R.id.pic : mTextView.setText('OnChlic'); break; }} }}
问题解答
回答1:不错乱才是怪事,你的view holder用法不对啊..onCreateViewHolder那里直接创建新的实例返回,这个方法会在需要新的item的时候被调用。你这样写法,整个adapter中使用的是同一个holder,因为显示有缓存所以不会第二个item就错乱。OnBind那里会提供你创建的holder实例,在那里操作你的数据。同时,以上方法的返回类型都应该是你的自定义holder。
回答2:重复乱序是什么意思,是第position 7的位置,显示的数据是之前显示过的?建议你在onBindViewHolder()方法中debug下,看看相关变量的值。
看代码好像没什么问题。是否有其他的代码,比如你说的“插入”
回答3:这一行是关键啊这是正确的写法
public class LightingRVAdapter extends RecyclerView.Adapter<LightingRVAdapter.LightingViewHolder>
改正后实现接口是这样子的
@Override public LightingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_lighting, parent, false);return new LightingViewHolder(v); }
@Overridepublic void onBindViewHolder(final LightingViewHolder holder, int position)
然后直接在onBindViewHolder内操作数据
感谢 原文海 的指正,谢谢