Android Recyclerview插入过多数据就会导致item重复,乱序

查看:322
本文介绍了Android Recyclerview插入过多数据就会导致item重复,乱序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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;
            }
        }
    }

}

解决方案

不错乱才是怪事,你的view holder用法不对啊..
onCreateViewHolder那里直接创建新的实例返回,这个方法会在需要新的item的时候被调用。
你这样写法,整个adapter中使用的是同一个holder,因为显示有缓存所以不会第二个item就错乱。
OnBind那里会提供你创建的holder实例,在那里操作你的数据。
同时,以上方法的返回类型都应该是你的自定义holder。

这篇关于Android Recyclerview插入过多数据就会导致item重复,乱序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆