优雅实现RecyclerView多种布局

要优雅就要符合 开闭原则,单一职责,当增加新的类型事只能扩展不能修改源代码。


package com.multitypeitem.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wanjian on 2017/1/12.
 */

public class MultiTypeRecyclerAdapter extends RecyclerView.Adapter {
    public static String TAG = MultiTypeRecyclerAdapter.class.getName();
    private List mItemViewList = new ArrayList<>();
    private TypeMapPolicy mTypeMapPolicy = new DefaultTypeMapPolicy();

    @Override
    public int getItemViewType(int position) {
        ItemViewFactory item = mItemViewList.get(position);
        return mTypeMapPolicy.toType(item);
    }

    public void setTypeMapPolicy(TypeMapPolicy typeMapPolicy) {
        mTypeMapPolicy = typeMapPolicy;
    }

    public void setData(List datas) {
        if (datas == null || datas.isEmpty()) {
            return;
        }
        mItemViewList.clear();

        appendData(datas);
    }

    public void appendData(List datas) {
        if (datas == null || datas.isEmpty()) {
            return;
        }
        for (ItemViewFactory item : datas) {
            if (item == null) {
                continue;
            }
            mItemViewList.add(item);
            item.attachAdapter(this);
        }
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return mTypeMapPolicy.toItemView(viewType).innerCreateVH(parent);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ItemViewFactory viewFactory = mItemViewList.get(position);
        viewFactory.innerBindVH(holder, position);
    }

    @Override
    public int getItemCount() {
        return mItemViewList.size();
    }


}



itemviewfactory

package com.multitypeitem.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

/**
 * Created by wanjian on 2017/1/23.
 */

public abstract class ItemViewFactory {
    D mData;
    private RecyclerView.Adapter mAdapter;
    private Context mContext;
    private int mPosition;

    public ItemViewFactory(Context context, D data) {
        this.mData = data;
        this.mContext = context;
    }

    //不加修饰符,只供内部使用,禁止外部调用
    H innerCreateVH(ViewGroup parent) {
        return onCreateViewHolder(mContext, parent);
    }

    void innerBindVH(RecyclerView.ViewHolder holder, int position) {
        mPosition = position;
        onBindViewHolder(mContext, ((H) holder), mData);
    }

    void attachAdapter(RecyclerView.Adapter adapter) {
        mAdapter = adapter;
    }

    protected void refresh(D data) {
        resetData(data);
        if (mAdapter != null) {
            mAdapter.notifyItemChanged(mPosition);
        }
    }

    protected void resetData(D data) {
        this.mData = data;
    }

    public abstract H onCreateViewHolder(Context context, ViewGroup parent);

    public abstract void onBindViewHolder(Context context, H holder, D data);
}

package com.multitypeitem.adapter;

/**
 * Created by wanjian on 2017/1/23.
 */

public interface TypeMapPolicy {
    int toType(ItemViewFactory viewFactory);

    ItemViewFactory toItemView(int type);
}

package com.multitypeitem.adapter;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by wanjian on 2017/1/23.
 */

public class DefaultTypeMapPolicy implements TypeMapPolicy {

    private List mTypesMapping = new ArrayList<>();

    @Override
    public int toType(ItemViewFactory item) {
        Class clz = item.getClass();

        for (int i = mTypesMapping.size() - 1; i > -1; i--) {
            ItemViewFactory itemViewFactory = mTypesMapping.get(i);
            if (itemViewFactory.getClass() == clz) {
                return i;
            }
        }
        mTypesMapping.add(item);
        return mTypesMapping.size() - 1;
    }

    @Override
    public ItemViewFactory toItemView(int viewType) {
        return mTypesMapping.get(viewType);
    }
}

各种类型view
package com.multitypeitem;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.multitypeitem.adapter.ItemViewFactory;

import static com.multitypeitem.adapter.MultiTypeRecyclerAdapter.TAG;

/**
 * Created by wanjian on 2017/1/12.
 */

public class ItemView1 extends ItemViewFactory {

    public ItemView1(Context context, Integer data) {
        super(context, data);
    }

    @Override
    public Item1VH onCreateViewHolder(Context context, ViewGroup parent) {
        Log.d(TAG, "onCreateViewHolder:..... 1");
        return new Item1VH(LayoutInflater.from(context).inflate(R.layout.item1, parent, false));
    }

    @Override
    public void onBindViewHolder(Context context, Item1VH holder, final Integer data) {
        holder.mTextView.setText(data + "");
        Log.w(TAG, "onBindViewHolder: " + data);
        holder.mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refresh(data + 10000);
            }
        });
    }


    static class Item1VH extends RecyclerView.ViewHolder {

        TextView mTextView;

        public Item1VH(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.tv);
        }
    }
}

package com.multitypeitem;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.multitypeitem.adapter.ItemViewFactory;
import com.multitypeitem.adapter.MultiTypeRecyclerAdapter;

/**
 * Created by wanjian on 2017/1/12.
 */

public class ItemView2 extends ItemViewFactory {

    public ItemView2(Context context, String data) {
        super(context, data);
    }

    @Override
    public Item2VH onCreateViewHolder(Context context, ViewGroup parent) {
        Log.d(MultiTypeRecyclerAdapter.TAG, "onCreateViewHolder:----------- 2");
        return new ItemView2.Item2VH(LayoutInflater.from(context).inflate(R.layout.item2, parent, false));
    }

    @Override
    public void onBindViewHolder(Context context, Item2VH holder, final String data) {
        holder.mTextView.setText(data);

        holder.mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refresh("change..." + data);
            }
        });
    }

    static class Item2VH extends RecyclerView.ViewHolder {
        TextView mTextView;

        public Item2VH(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.tv);
        }
    }
}

其他view类似


main布局




    


item布局




    

使用

package com.multitypeitem;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.multitypeitem.adapter.ItemViewFactory;
import com.multitypeitem.adapter.MultiTypeRecyclerAdapter;

public class MainActivity extends AppCompatActivity {

    RecyclerView mRecyclerView;

    MultiTypeRecyclerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mRecyclerView = (RecyclerView) findViewById(R.id.recycler);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);

        mRecyclerView.setLayoutManager(layoutManager);

        RecyclerView.RecycledViewPool recycledViewPool = new RecyclerView.RecycledViewPool();
        //设置每种布局最大缓存数量,可以不设置
        recycledViewPool.setMaxRecycledViews(0, 15);
        recycledViewPool.setMaxRecycledViews(1, 15);
        recycledViewPool.setMaxRecycledViews(2, 15);
        recycledViewPool.setMaxRecycledViews(3, 15);
        recycledViewPool.setMaxRecycledViews(4, 15);
        mRecyclerView.setRecycledViewPool(recycledViewPool);

        mAdapter = new MultiTypeRecyclerAdapter();
        mRecyclerView.setAdapter(mAdapter);

        List data = getData();
        mAdapter.setData(data);

        findViewById(R.id.but).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.setData(getData2());
            }
        });
    }


    public List getData() {
        List data = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            data.add(new ItemView1(this, i));
        }

        for (int i = 0; i < 1; i++) {
            data.add(new ItemView2(this, "view2---------- " + i));
        }

//        for (int i = 0; i < 10; i++) {
//            data.add(new ItemView3(this, new Date(2017-1900, 1, 1 + i)));
//        }
//
        for (int i = 0; i < 8; i++) {
            data.add(new ItemView1(this, 800 + i));
        }
//
        for (int i = 0; i < 2; i++) {
            data.add(new ItemView4(this, i));
        }
//
//        for (int i = 0; i < 50; i++) {
//            data.add(new ItemView5(this, "view5 " + i));
//        }

        return data;
    }

    public List getData2() {
        List data = new ArrayList<>();

        for (int i = 0; i < 3; i++) {
            data.add(new ItemView3(this, new Date(2017 - 1900, 1, 1 + i)));
        }

        for (int i = 0; i < 3; i++) {
            data.add(new ItemView2(this, "view2---------- " + i));
        }


//        for (int i = 0; i < 5; i++) {
//            data.add(new ItemView1(this, i));
//        }
//


        for (int i = 0; i < 2; i++) {
            data.add(new ItemView4(this, i));
        }

        for (int i = 0; i < 5; i++) {
            data.add(new ItemView5(this, "view5 " + i));
        }


        for (int i = 0; i < 8; i++) {
            data.add(new ItemView1(this, 800 + i));
        }

        return data;
    }
}

效果

优雅实现RecyclerView多种布局_第1张图片




git

https://github.com/android-notes/multityperecyclerview



你可能感兴趣的:(安卓)