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.RecyclerView
android:id="@+id/commodity_list"
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/pic"
android:layout_width="match_parent"
android:layout_height="120dp"
android:gravity="center"
android:textSize="50sp"
/>
<TextView
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" />
</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.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;
}
}
}
}
怪我咯2017-04-17 17:24:11
Yes, it is a strange thing to be confused. Your view holder usage is wrong...
OnCreateViewHolder directly creates a new instance and returns it. This method will be called when a new item is needed.
The way you write it, the same holder is used in the entire adapter. Because the display is cached, the second item will not be confused.
OnBind will provide the holder instance you created and operate your data there.
At the same time, the return type of the above methods should be your custom holder.
PHP中文网2017-04-17 17:24:11
What does "repeating out of order" mean? It is position 7, and the data displayed is the one that has been displayed before?
It is recommended that you debug in the onBindViewHolder() method to see the values of related variables.
Looking at the code, there seems to be no problem. Is there any other code, such as the "insert" you mentioned
PHP中文网2017-04-17 17:24:11
This line is the key
This is the correct way to write it
public class LightingRVAdapter extends RecyclerView.Adapter<LightingRVAdapter.LightingViewHolder>
The implemented interface looks like this after correction
@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);
}
@Override
public void onBindViewHolder(final LightingViewHolder holder, int position)
Then operate the data directly in onBindViewHolder
Thanks to Original Wenhai for the correction, thank you