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

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

巧妙解决RecycleView的复用问题,不用model层_第1张图片

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

先上个效果图

巧妙解决RecycleView的复用问题,不用model层_第2张图片
效果图
XML布局




item布局



    

        

            

                

                

                

                

                
            

            

                

                
            

            
        
    

    

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, OnItemClickListener {

    @BindView(R.id.rv_show)
    RecyclerView mRvShow;

    private List 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 extends RecyclerView.Adapter implements View.OnClickListener {

    private List             mDatas;
    private Context             mContext;
    private int                 layoutId;
    private BaseAdapterListener mBaseAdapterListener;
    private OnItemClickListener mOnItemClickListener;

    public BaseAdapter(List 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 {
        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 views;

    public BaseViewHolder(View itemView) {
        super(itemView);
        this.views = new SparseArray<>();
        this.itemView = itemView;
    }

    public  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://upload-images.jianshu.io/upload_images/5286943-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层)