美文网首页程序员
巧妙解决RecycleView的复用问题,不用model层

巧妙解决RecycleView的复用问题,不用model层

作者: Master_文 | 来源:发表于2018-04-19 00:22 被阅读0次

    大伙肯定遇到过RecycleView的复用问题,一般都是在model层设置一个标志,然后根据这个标志控制.

    前几天做项目,遇到这个问题的时候,想着,有没有其他方式解决这个问题,搞了大概十几分钟吧,发现一个不错的方式,记录一下,各位大佬有好的方式,请在下方留言,小弟不胜感激!!

    先上个效果图

    效果图
    XML布局
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/rv_show"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.sy.gwb.ui.activity.DiscountCouponActivity">
    
    </android.support.v7.widget.RecyclerView>
    
    item布局
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/cv_discount_coupon"
            android:layout_width="345dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:gravity="center_horizontal"
            android:orientation="vertical"
            app:cardCornerRadius="8dp">
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <RelativeLayout
                    android:id="@+id/rl_discount_coupon_header"
                    android:layout_width="match_parent"
                    android:layout_height="81dp"
                    android:background="#FFFBB82F">
    
                    <View
                        android:id="@+id/view_center"
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:layout_centerInParent="true"/>
    
                    <TextView
                        android:id="@+id/tv_price"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:layout_marginRight="21dp"
                        android:text="¥30"
                        android:textColor="#FFFFFFFF"
                        android:textSize="16sp"/>
    
                    <View
                        android:layout_width="4dp"
                        android:layout_height="match_parent"
                        android:layout_alignParentRight="true"
                        android:layout_marginRight="76dp"
                        android:background="@mipmap/icon_dashed"/>
    
                    <TextView
                        android:id="@+id/tv_discount_coupon_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_above="@id/view_center"
                        android:layout_marginBottom="2dp"
                        android:layout_marginLeft="15dp"
                        android:text="优惠券"
                        android:textColor="#FFFFFFFF"
                        android:textSize="16sp"/>
    
                    <TextView
                        android:id="@+id/tv_discount_coupon_desc"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignLeft="@id/tv_discount_coupon_name"
                        android:layout_below="@id/view_center"
                        android:layout_marginTop="2dp"
                        android:text="选我就对了"
                        android:textColor="#FFFFFFFF"
                        android:textSize="12sp"/>
                </RelativeLayout>
    
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="31dp"
                    android:layout_below="@id/rl_discount_coupon_header"
                    android:background="@color/white">
    
                    <TextView
                        android:id="@+id/tv_discount_coupon_validity"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="13dp"
                        android:text="有效期:2018.03.12-2018.08.01"
                        android:textColor="#FF828282"
                        android:textSize="12sp"/>
    
                    <ImageView
                        android:id="@+id/iv_select"
                        android:layout_width="22dp"
                        android:layout_height="22dp"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:layout_marginRight="5dp"
                        android:src="@mipmap/normal_selected"
                        android:visibility="gone"/>
                </RelativeLayout>
    
                <View
                    android:layout_width="match_parent"
                    android:layout_height="4dp"
                    android:layout_alignBottom="@id/rl_discount_coupon_header"
                    android:layout_marginTop="10dp"
                    android:background="@mipmap/discount_coupon_line"/>
            </RelativeLayout>
        </android.support.v7.widget.CardView>
    
        <View
            android:id="@+id/view"
            android:layout_width="match_parent"
            android:layout_height="68dp"/>
    </LinearLayout>
    
    activity代码
    package com.sy.gwb.ui.activity;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.widget.ImageView;
    
    import com.sy.gwb.R;
    import com.sy.gwb.adapter.BaseAdapter;
    import com.sy.gwb.adapter.BaseViewHolder;
    import com.sy.gwb.adapter.OnItemClickListener;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import butterknife.BindView;
    import butterknife.ButterKnife;
    
    public class DiscountCouponActivity extends AppCompatActivity implements BaseAdapter.BaseAdapterListener<String>, OnItemClickListener {
    
        @BindView(R.id.rv_show)
        RecyclerView mRvShow;
    
        private List<String> mList = new ArrayList<>();
        private int         prePosition;            // 之前的位置
        private int         currentPosition;        // 当前的位置
        private BaseAdapter mAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_discount_coupon);
            ButterKnife.bind(this);
            for (int i = 0; i < 10; i++) {
                mList.add("");
            }
            // 这个没什么好说的
            mAdapter = new BaseAdapter(mList, this, R.layout.item_discount_coupons, this);
            mRvShow.setLayoutManager(new LinearLayoutManager(this));
            mRvShow.setAdapter(mAdapter);
            mAdapter.setOnItemClickListener(this);
        }
    
    
        @Override
        public void convert(final BaseViewHolder holder, String str) {
            int layoutPosition = holder.getLayoutPosition();
            holder.setVisible(R.id.view, layoutPosition == mList.size() - 1);
            ImageView iv_select = holder.getView(R.id.iv_select);
            holder.setVisible(R.id.iv_select);
            if (layoutPosition == currentPosition) {
                iv_select.setImageResource(R.mipmap.green_selected);
            } else {
                iv_select.setImageResource(R.mipmap.normal_selected);
            }
        }
    
        @Override
        public void onItemClick(int position) {
            // TODO: 2018/4/18 如果是相同的不设置
            if (prePosition != position) {
                currentPosition = position;
                mAdapter.notifyItemChanged(position);
                mAdapter.notifyItemChanged(prePosition);
                prePosition = position;
            }
        }
    }
    
    

    自己封装的BaseAdapter

    package com.sy.gwb.adapter;
    
    import android.content.Context;
    import android.support.annotation.LayoutRes;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import java.util.List;
    
    /**
     * Created by ${GongWenbo} on 2018/3/30 0030.
     */
    
    public class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> implements View.OnClickListener {
    
        private List<T>             mDatas;
        private Context             mContext;
        private int                 layoutId;
        private BaseAdapterListener mBaseAdapterListener;
        private OnItemClickListener mOnItemClickListener;
    
        public BaseAdapter(List<T> datas, Context context, @LayoutRes int layoutId, BaseAdapterListener baseAdapterListener) {
            this.mDatas = datas;
            this.mContext = context;
            this.layoutId = layoutId;
            this.mBaseAdapterListener = baseAdapterListener;
        }
    
        @Override
        public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(layoutId, parent, false);
            view.setOnClickListener(this);
            return new BaseViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(BaseViewHolder holder, int position) {
            if (mBaseAdapterListener != null) {
                mBaseAdapterListener.convert(holder, mDatas.get(position));
                holder.itemView.setTag(position);
            }
        }
    
        @Override
        public int getItemCount() {
            return mDatas == null ? 0 : mDatas.size();
        }
    
        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            mOnItemClickListener = onItemClickListener;
        }
    
        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick((Integer) v.getTag());
            }
        }
    
        public interface BaseAdapterListener<T> {
            void convert(BaseViewHolder holder, T t);
        }
    
    }
    
    自己封装的BaseViewHolder
    package com.sy.gwb.adapter;
    
    import android.graphics.Color;
    import android.support.annotation.IdRes;
    import android.support.v7.widget.RecyclerView;
    import android.util.SparseArray;
    import android.view.View;
    import android.widget.TextView;
    
    /**
     * Created by ${GongWenbo} on 2018/3/30 0030.
     */
    
    public class BaseViewHolder extends RecyclerView.ViewHolder {
        protected View              itemView;
        private   SparseArray<View> views;
    
        public BaseViewHolder(View itemView) {
            super(itemView);
            this.views = new SparseArray<>();
            this.itemView = itemView;
        }
    
        public <T extends View> T getView(@IdRes int viewId) {
            View view = views.get(viewId);
            if (view == null) {
                view = itemView.findViewById(viewId);
                views.put(viewId, view);
            }
            return (T) view;
        }
    
        public View setTitle(@IdRes int viewId, String title) {
            View view = getView(viewId);
            if (view instanceof TextView) {
                ((TextView) view).setText(title);
            } else {
                throw new ClassCastException("you need give me TextView!");
            }
            return view;
        }
    
        public View setVisible(@IdRes int viewId) {
            return setVisible(viewId, true);
        }
    
        public View setVisible(@IdRes int viewId, boolean visible) {
            View view = getView(viewId);
            view.setVisibility(visible ? View.VISIBLE : View.GONE);
            return view;
        }
    
        public void setBackgroundColor(@IdRes int viewId, String color) {
            View view = getView(viewId);
            view.setBackgroundColor(Color.parseColor(color));
        }
    ![u=2120199729,3161366045&fm=85&s=5A6DA144FEFF2F9E10EF848F0300709A.jpg](https://img.haomeiwen.com/i5286943/563c0d6bc9fab061.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    }
    
    Adapter的条目点击OnItemClickListener
    package com.sy.gwb.adapter;
    
    /**
     * Created by GongWenBo on 2018/4/7.
     */
    
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
    

    相关文章

      网友评论

        本文标题:巧妙解决RecycleView的复用问题,不用model层

        本文链接:https://www.haomeiwen.com/subject/lllmkftx.html