RecyclerView 中显示多布局(二)

优化上一篇的效果,本篇将带来 GridView 和 ListView 的混排,及优化代码逻辑。
1,效果图:
RecyclerView 中显示多布局(二)_第1张图片

2,MainActivity.java 给 RecyclerView 设置间隔,设置不同数据类型;

package com.gyq.diverserecyclerview;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.gyq.diverserecyclerview.adapter.DiverseAdapter;
import com.gyq.diverserecyclerview.bean.DiverseDataOne;
import com.gyq.diverserecyclerview.bean.DiverseDataThree;
import com.gyq.diverserecyclerview.bean.DiverseDataTwo;

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

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private DiverseAdapter mAdapter;
    private int[] colors = {android.R.color.holo_red_dark,android.R.color.holo_green_dark,android.R.color.holo_blue_bright};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        final GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2);
//        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,
//                LinearLayoutManager.VERTICAL,false));
        //此方法是item 占满 还是占部分,分母 new GridLayoutManager(this,2) 中的2可以看成是分母,;
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int type = mRecyclerView.getAdapter().getItemViewType(position);
                if (type == 3) {
                    return gridLayoutManager.getSpanCount();   //占一行
                }else {
                    return 1;    // 占1/2
                }
            }
        });
        mRecyclerView.setLayoutManager(gridLayoutManager);

        //设置间隔
        mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) view.getLayoutParams();
                int spanSize = layoutParams.getSpanSize();
                int spanIndex = layoutParams.getSpanIndex();
                outRect.top = 20;
                if (spanSize != gridLayoutManager.getSpanCount()) {
                    if (spanIndex ==1) {
                        outRect.left = 10;
                    }else {
                        outRect.right = 18;
                    }
                }

            }
        });
        mAdapter = new DiverseAdapter(this);
        mRecyclerView.setAdapter(mAdapter);

        initData();
    }

    private void initData() {


        List list1 = new ArrayList<>();

        for (int i = 0;i < 10;i++){

            DiverseDataOne data = new DiverseDataOne();
            data.avatarColor = colors[0];
            data.name = "name: " + 1;
            list1.add(data);
        }

        List list2 = new ArrayList<>();

        for (int i = 0;i < 10;i++){

            DiverseDataTwo data = new DiverseDataTwo();
            data.avatarColor = colors[1];
            data.name = "name: " + 1;
            data.content = "content: " + i;
            list2.add(data);
        }

        List list3 = new ArrayList<>();
        for (int i = 0;i < 10;i++){

            DiverseDataThree data = new DiverseDataThree();
            data.avatarColor = colors[2];
            data.name = "name: " + 1;
            data.content = "content: " + i;
            data.contentColor = colors[0];
            list3.add(data);
        }

        mAdapter.addList(list1,list2,list3);
        mAdapter.notifyDataSetChanged();

    }
}

3,三个 Bean 类
a) DiverseDataOne.java

package com.gyq.diverserecyclerview.bean;

/**
 * Created by gyq on 2018/1/2 14:12
 */

public class DiverseDataOne {
    public int avatarColor;

    public String name;
}

b) DiverseDataTwo.java

package com.gyq.diverserecyclerview.bean;

/**
 * Created by gyq on 2018/1/2 14:12
 */

public class DiverseDataTwo {
    public int avatarColor;

    public String name;

    public String content;
}

c) DiverseDataThree.java

package com.gyq.diverserecyclerview.bean;

/**
 * Created by gyq on 2018/1/2 14:12
 */

public class DiverseDataThree {
    public int avatarColor;

    public String name;

    public String content;

    public int contentColor;
}

4,ViewHolder 类
a),BaseViewHolder.java

package com.gyq.diverserecyclerview.base;

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

/**
 * Created by gyq on 2017/12/29 17:27
 */

public abstract class BaseViewHolder<T> extends RecyclerView.ViewHolder {
    public BaseViewHolder(View itemView) {
        super(itemView);
    }

    public abstract void bindHodler(T data);
}

b)OneViewHolder.java

package com.gyq.diverserecyclerview.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyq.diverserecyclerview.R;
import com.gyq.diverserecyclerview.base.BaseViewHolder;
import com.gyq.diverserecyclerview.bean.DiverseDataOne;

/**
 * Created by gyq on 2017/12/29 17:19
 */

public class OneViewHolder extends BaseViewHolder {
    public ImageView avatar;
    public TextView name;
    public OneViewHolder(View itemView) {
        super(itemView);

        avatar = (ImageView) itemView.findViewById(R.id.avatar_color);
        name = (TextView) itemView.findViewById(R.id.name);
    }

    @Override
    public void bindHodler(DiverseDataOne data) {
        avatar.setBackgroundResource(data.avatarColor);
        name.setText(data.name);
    }

}

c) TwoViewHolder.java

package com.gyq.diverserecyclerview.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyq.diverserecyclerview.R;
import com.gyq.diverserecyclerview.base.BaseViewHolder;
import com.gyq.diverserecyclerview.bean.DiverseDataTwo;

/**
 * Created by gyq on 2017/12/29 17:19
 */

public class TwoViewHolder extends BaseViewHolder {
    public ImageView avatar;
    public TextView name;
    public TextView content;
    public TwoViewHolder(View itemView) {
        super(itemView);

        avatar = (ImageView) itemView.findViewById(R.id.avatar_color);
        name = (TextView) itemView.findViewById(R.id.name);
        content = (TextView) itemView.findViewById(R.id.content);
    }

    @Override
    public void bindHodler(DiverseDataTwo data) {
        avatar.setBackgroundResource(data.avatarColor);
        name.setText(data.name);
        content.setText(data.content);
    }

}

d)ThreeViewHolder.java

package com.gyq.diverserecyclerview.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyq.diverserecyclerview.R;
import com.gyq.diverserecyclerview.base.BaseViewHolder;
import com.gyq.diverserecyclerview.bean.DiverseDataThree;

/**
 * Created by gyq on 2017/12/29 17:19
 */

public class ThreeViewHolder extends BaseViewHolder {
    public ImageView avatar;
    public ImageView contentColor;
    public TextView name;
    public TextView content;
    public ThreeViewHolder(View itemView) {
        super(itemView);

        avatar = (ImageView) itemView.findViewById(R.id.avatar_color);
        contentColor = (ImageView) itemView.findViewById(R.id.content_color);
        name = (TextView) itemView.findViewById(R.id.name);
        content = (TextView) itemView.findViewById(R.id.content);
    }

    @Override
    public void bindHodler(DiverseDataThree data) {
        avatar.setBackgroundResource(data.avatarColor);
        name.setText(data.name);
        content.setText(data.content);
        contentColor.setBackgroundResource(data.contentColor);
    }

}
  1. 适配器
package com.gyq.diverserecyclerview.adapter;

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

import com.gyq.diverserecyclerview.R;
import com.gyq.diverserecyclerview.bean.DiverseDataOne;
import com.gyq.diverserecyclerview.bean.DiverseDataThree;
import com.gyq.diverserecyclerview.bean.DiverseDataTwo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gyq on 2017/12/29 16:29
 */

public class DiverseAdapter extends RecyclerView.Adapter {
    public final static int TYPE_ONE = 1;
    public final static int TYPE_TWO = 2;
    public final static int TYPE_THREE = 3;
    private LayoutInflater mInflater;

    private List mTypes = new ArrayList<>();
    private Map mPositions = new HashMap<>();

    private List list1;
    private List list2;
    private List list3;
    public DiverseAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    public void addList(List list1, List list2, List list3) {
        addListByType(TYPE_ONE,list1);
        addListByType(TYPE_TWO,list2);
        addListByType(TYPE_THREE,list3);

        this.list1 = list1;
        this.list2 = list2;
        this.list3 = list3;

    }

    private void addListByType(int type,List list){
        mPositions.put(type,mTypes.size());
        for (int i=0;ipublic RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_ONE :
                return new OneViewHolder(mInflater.inflate(R.layout.item_type_one,parent,false));
            case TYPE_TWO :
                return new TwoViewHolder(mInflater.inflate(R.layout.item_type_two,parent,false));
            case TYPE_THREE :
                return new ThreeViewHolder(mInflater.inflate(R.layout.item_type_three,parent,false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
       // ((BaseViewHolder)holder).bindHodler(mList.get(position));
        int viewType = getItemViewType(position);
        int realPosition = position - mPositions.get(viewType);
        switch (viewType) {
            case TYPE_ONE :
                ((OneViewHolder)holder).bindHodler(list1.get(realPosition));
                break;
            case TYPE_TWO :
                ((TwoViewHolder)holder).bindHodler(list2.get(realPosition));
                break;
            case TYPE_THREE :
                ((ThreeViewHolder)holder).bindHodler(list3.get(realPosition));
                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        return mTypes.get(position);
    }

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

你可能感兴趣的:(Android,基础)