【Android】Recyclerview-2-添加多个子布局

1、添加依赖

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:recyclerview-v7:25.1.0'
    testCompile 'junit:junit:4.12'
}
2、在主布局中声明RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myrecyclerviewtest.MainActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/mrecyclerview"
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        android:layout_alignParentBottom="true"
        />
</RelativeLayout>
3、创建两个子布局recycler_item.xml和recycler_item_2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="400dp"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/img_big"
        android:layout_width="200dp"
        android:layout_height="400dp"
        android:src="@drawable/pic1"
        android:visibility="visible"
        />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="200dp"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/img_two"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:id="@+id/img_small_1"
            android:src="@drawable/pic3"
            />
        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:id="@+id/img_small_2"
            android:src="@drawable/pic3"
            android:layout_alignParentBottom="true"
            />
    </LinearLayout>
</LinearLayout>
4、新建一个DataBean类

public class DataBean {
    int imageViewId;
    public int getImageViewId() {
        return imageViewId;
    }
    public void setImageViewId(int imageViewId) {
        this.imageViewId = imageViewId;
    }
}
5、新建两个ViewHolder类,ViewHolderOne和ViewHolderTwo

public class ViewHolderOne extends RecyclerView.ViewHolder {
    public ImageView bigImg;
    public ViewHolderOne(View itemView) {
        super(itemView);
        bigImg = (ImageView)itemView.findViewById(R.id.img_big);
    }
}
public class ViewHolderTwo  extends RecyclerView.ViewHolder{
    public ImageView Img_1;
    public ImageView Img_2;
    public ViewHolderTwo(View itemView) {
        super(itemView);
        Img_1 = (ImageView) itemView.findViewById(R.id.img_small_1);
        Img_2 = (ImageView) itemView.findViewById(R.id.img_small_2);
    }
}
6、新建一个RecyclerAdapter类

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    List<DataBean> dataBeanList;
    public RecyclerAdapter (List<DataBean> list){
        dataBeanList = list;
    }
    /**
     * 不是一次性遍历完list集合的,是根据RecyclerView的滑动逐步加载的
     * 每一次遍历,都会调用onCreateViewHolder()方法
     * @param holder onCreateViewHolder()方法返回holder
     * @param position list集合遍历的位置
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(position<2){
            //虽然是父类的引用,但实际指向子类的对象,所以是可以强制转换为子类的引用的
            ViewHolderOne viewHolderOne = (ViewHolderOne) holder;
            viewHolderOne.bigImg.setImageResource(dataBeanList.get(position).getImageViewId());
        }else if(position ==2){
            ViewHolderTwo viewHolderTwo = (ViewHolderTwo) holder;
            viewHolderTwo.Img_1.setImageResource(dataBeanList.get(position).getImageViewId());
            viewHolderTwo.Img_2.setImageResource(dataBeanList.get(position+1).getImageViewId());
        }
    }
    @Override//创建ViewHolder引用暂存类
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        RecyclerView.ViewHolder holder = null;
        if(viewType<2){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);
            holder = new ViewHolderOne(view);
        }else if(viewType ==2){
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_2,parent,false);
            holder = new ViewHolderTwo(view);
        }
        /**
         * 自动向上转型,所以子类的对象holder,能赋给父类RecyclerView.ViewHolder的引用
         * 父类创建的对象的内存肯定是 <= 子类创建的对象的内存,因为子类对象=父类对象+子类扩充的属性和方法
         * 当子类对象自动向上转型赋给父类引用时,子类扩充的属性和方法是被屏蔽的,因为父类的引用没有指向这些
         * 属性的指针,但是当子类重写了父类的方法(只是方法)时,这个父类的引用可以调用这个被重写的方法,
         * 因为他们有了如下指向:父类的引用-->父类的方法-->父类被重写的方法
         * 这就是Java的----多态
         */
        return holder;
    }
    @Override//遍历list集合的长度
    public int getItemCount() {
        return dataBeanList.size()-1;
    }
    @Override//onCreateViewHolder(ViewGroup parent, int viewType)中的viewType参数由此获得
    public int getItemViewType(int position) {
        return position;
    }
}
7、在MainActivity中初始化配置

public class MainActivity extends AppCompatActivity {
    private List<DataBean> list;
    private int res[];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = new ArrayList<>();
        initRes();
        initData();
        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.mrecyclerview);
        RecyclerAdapter adapter = new RecyclerAdapter(list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayout.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }
    private void initRes(){
        res = new int[]{R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4};
    }
    private void initData(){
        for(int i=0;i<res.length;i++){
            DataBean dataBean = new DataBean();
            dataBean.setImageViewId(res[i]);
            list.add(dataBean);
        }
    }
}
最后,效果图



















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值