欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

RecyclerView三种布局

程序员文章站 2022-03-13 17:05:17
...

RecyclerView三种布局

线性布局

activity

//线性布局管理器
        LinearLayoutManager manager=new LinearLayoutManager(this);
        //设置方向
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        //设置线性布局
        recyclerView_1.setLayoutManager(manager);
        //间隔线
        DividerItemDecoration dividerItemDecoration=new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(getDrawable(R.drawable.bac));
        recyclerView_1.addItemDecoration(dividerItemDecoration);
        List<String> list=new ArrayList<>();
        for(int i=0;i<100;i++){
            list.add("数据"+i);
        }
        MyRecyclerAdapter myRecyclerAdapter = new MyRecyclerAdapter(this, list);
        recyclerView_1.setAdapter(myRecyclerAdapter);

xml布局

<TextView
        android:id="@+id/recycler_text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="19dp"
        android:textColor="@android:color/holo_blue_light"
        android:textStyle="bold"/>

适配器

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.Viewholder>{
    Context context;
    List<String> list;
    public MyRecyclerAdapter(Context context,List<String> list){
        this.context=context;
        this.list=list;
    }

    //把布局绑定给viewholder
    @NonNull
    @Override
    public Viewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(context).inflate(R.layout.recycler_item1,viewGroup,false);
        return new Viewholder(view);
    }

   //绑定布局并给布局中的内容添加数据
    @Override
    public void onBindViewHolder(@NonNull Viewholder viewholder, final int i) {
        viewholder.textView.setText(list.get(i));
        viewholder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"您点击了"+i,Toast.LENGTH_SHORT).show();
            }
        });
        viewholder.textView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(context,"您长按了"+i,Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }


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

    class Viewholder extends RecyclerView.ViewHolder{
         private TextView textView;
        public Viewholder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.recycler_text1);
        }
    }
}

效果图
RecyclerView三种布局

网格布局

activity

 //网格布局管理器  2是网格布局的列数
        GridLayoutManager manager=new GridLayoutManager(this,2);
        //设置方向
        manager.setOrientation(GridLayoutManager.VERTICAL);
        //设置给recyclerview
        recyclerView_1.setLayoutManager(manager);
        //间隔线
        DividerItemDecoration dividerItemDecoration=new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(getDrawable(R.drawable.bac));
        recyclerView_1.addItemDecoration(dividerItemDecoration);

        List<String> list=new ArrayList<>();
        for(int i=0;i<100;i++){
            list.add("http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg");
        }
        MyRecyclerAdapter2 myRecyclerAdapter = new MyRecyclerAdapter2(this, list);
        recyclerView_1.setAdapter(myRecyclerAdapter);

xml布局

  <ImageView
        android:id="@+id/recycler_image2"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerInParent="true"/>

适配器

public class MyRecyclerAdapter2 extends RecyclerView.Adapter<MyRecyclerAdapter2.Viewholder>{
    Context context;
    List<String> list;
    public MyRecyclerAdapter2(Context context, List<String> list){
        this.context=context;
        this.list=list;
    }


    @NonNull
    @Override
    public Viewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(context).inflate(R.layout.recycler_item2,viewGroup,false);
        return new Viewholder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Viewholder viewholder, final int i) {
        Glide.with(context).load(list.get(i)).into(viewholder.imageView);
        viewholder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"您点击了"+i,Toast.LENGTH_SHORT).show();
            }
        });
        viewholder.imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(context,"您长按了"+i,Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }


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

    class Viewholder extends RecyclerView.ViewHolder{
         private ImageView imageView;
        public Viewholder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.recycler_image2);
        }
    }
}

效果图
RecyclerView三种布局

瀑布流布局

activity

 //瀑布流
        StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        //设置布局
        recyclerView_1.setLayoutManager(staggeredGridLayoutManager);
        //间隔线
        DividerItemDecoration dividerItemDecoration=new DividerItemDecoration(this,DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(getDrawable(R.drawable.bac));
        recyclerView_1.addItemDecoration(dividerItemDecoration);
        List<Food> list=new ArrayList<>();
        for(int i=0;i<50;i++){
             Food food=new Food();
             food.name="数据"+i;
             food.pic="http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg";
             list.add(food);
        }
        MyRecyclerAdapter3 myRecyclerAdapter = new MyRecyclerAdapter3(this, list);
        recyclerView_1.setAdapter(myRecyclerAdapter);

xml布局

  <TextView
        android:id="@+id/recycler_text3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="19dp"
        android:textColor="@android:color/holo_blue_light"
        android:textStyle="bold"/>
    <ImageView
        android:id="@+id/recycler_image3"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerInParent="true"/>

适配器

public class MyRecyclerAdapter3 extends RecyclerView.Adapter<MyRecyclerAdapter3.Viewholder>{
    Context context;
    List<Food> list;
    public MyRecyclerAdapter3(Context context, List<Food> list){
        this.context=context;
        this.list=list;
    }


    @NonNull
    @Override
    public Viewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(context).inflate(R.layout.recycler_item3,viewGroup,false);
        return new Viewholder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Viewholder viewholder, final int i) {
        Glide.with(context).load(list.get(i).pic).into(viewholder.imageView);
        viewholder.textView.setText(list.get(i).name);
        ViewGroup.LayoutParams layoutParams = viewholder.imageView.getLayoutParams();
        int run=(int)(Math.random()*101+100);
        layoutParams.height=run;
        viewholder.imageView.setLayoutParams( layoutParams);
        viewholder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"您点击了"+i,Toast.LENGTH_SHORT).show();
            }
        });
        viewholder.imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(context,"您长按了"+i,Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }


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

    class Viewholder extends RecyclerView.ViewHolder{
         private ImageView imageView;
         private TextView textView;
        public Viewholder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.recycler_text3);
            imageView=itemView.findViewById(R.id.recycler_image3);
        }
    }
}

效果图
RecyclerView三种布局