RecyclerView嵌套RecyclerView

在项目中有时候遇到recyclerview嵌套recyclerview的情况,虽然不建议这样嵌套使用,但有时候也没办法。
示例:
这里写图片描述
代码如下:
1、在Activity中代码如下

package com.example.recyclerview;

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

import java.util.ArrayList;

import adapter.MyListAdapter;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerview;
    private ArrayList list;
    private MyListAdapter listAdapter;
    private LinearLayoutManager linearLayoutManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerview= (RecyclerView) findViewById(R.id.recyclerview);
        linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview.setLayoutManager(linearLayoutManager);
        initDate();
        listAdapter=new MyListAdapter(this,list);
        recyclerview.setAdapter(listAdapter);
    }
    private void initDate() {
        list=new ArrayList();
        for(int i=0;i<15;i++){
            list.add("this is "+i);
        }
    }
}

2、在ListAdapter中

package adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.recyclerview.R;

import java.util.ArrayList;

/** * Created by Administrator on 2016/12/11. */

public class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.MyViewHolder> {

    private ArrayList list;
    private Context context;
    private GridLayoutManager gridLayoutManager;
    private ArrayList list_image;

    public MyListAdapter(Context context, ArrayList list) {
        this.list = list;
        this.context = context;
        gridLayoutManager =new GridLayoutManager(context,3);
        gridLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        list_image=new ArrayList();
        list_image.add("http://e.hiphotos.baidu.com/movie/pic/item/7aec54e736d12f2ec564664446c2d562853568b5.jpg");
        list_image.add("http://c.hiphotos.baidu.com/movie/pic/item/e4dde71190ef76c6e52d8fe29516fdfaae51677f.jpg");
        list_image.add("http://c.hiphotos.baidu.com/movie/pic/item/fd039245d688d43f5d3648ec741ed21b0ef43b15.jpg");
        list_image.add("http://e.hiphotos.baidu.com/movie/pic/item/1ad5ad6eddc451da65ded2f5bffd5266d0163280.jpg");
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_item, null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.tv.setText(list.get(position).toString());
        if(list_image.size()>0) {
            holder.recyclerImage.setLayoutManager(gridLayoutManager);
            ImageAdapter adapter = new ImageAdapter(context, list_image);
            holder.recyclerImage.setLayoutManager(new GridLayoutManager(context, 3));
            holder.recyclerImage.setAdapter(adapter);
        }else{
            holder.recyclerImage.setVisibility(View.INVISIBLE);
        }
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        RecyclerView recyclerImage;
        public MyViewHolder(View itemView) {
            super(itemView);
            tv= (TextView) itemView.findViewById(R.id.text);
            recyclerImage= (RecyclerView) itemView.findViewById(R.id.recyclerImage);
        }
    }
}

3、最后在List中嵌套recyclerview代码

package adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.example.recyclerview.R;

import java.util.ArrayList;

/** * Created by Administrator on 2016/12/11. */

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.MyImageHolder> {
    private Context context;
    private ArrayList list;
    private DisplayMetrics dm;

    public ImageAdapter(Context context, ArrayList list){
        this.context=context;
        this.list=list;
        dm = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
    }
    @Override
    public MyImageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.list_image,null);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((dm.widthPixels - dip2px(20)) / 3, (dm.widthPixels - dip2px(20)) / 3);
        view.setLayoutParams(lp);
        MyImageHolder holder=new MyImageHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyImageHolder holder, int position) {
        Glide.with(context)
                .load(list.get(position))
                .centerCrop()
                .crossFade()
                .into(holder.imageView);
        holder.itemView.setTag(list.get(position));

    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    class MyImageHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        public MyImageHolder(View itemView) {
            super(itemView);
            imageView= (ImageView) itemView.findViewById(R.id.imageview);
        }
    }
    int dip2px(float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}

代码下载地址:
http://download.csdn.net/detail/androidxiaogang/9708426

参考大白博客
http://www.voidcn.com/article/p-zmrhpxcd-yb.html

相关文章
相关标签/搜索