利用RecyclerView实现瀑布流效果

  • 引入类包
    implementation ‘com.android.support:recyclerview-v7:26.1.0’

  • 主活动界面代码

  • private MyRecycleViewAdapter myRecycleViewAdapter;
    private RecyclerView recyclerView;
    private ArrayList datas;

  • recyclerView=(RecyclerView)view.findViewById(R.id.recyclerview);
    myRecycleViewAdapter=new MyRecycleViewAdapter(this.getActivity(),datas);
    recyclerView.setAdapter(myRecycleViewAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false));

  • 定义适配器
    package taoba.sdbi.com.taoba;

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

import java.util.ArrayList;

/**

  • Created by Administrator on 2019/11/25.
    */

public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.ViewHolder> {

private final Context context;
private ArrayList<String> datas;
private LayoutInflater inflater;

public MyRecycleViewAdapter(Context context,ArrayList<String> datas)
{
    this.datas=datas;
    this.context=context;
    inflater=LayoutInflater.from(context);

}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView=View.inflate(context,R.layout.recyclreview_item,null);
    return new ViewHolder(itemView);

}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    String data=datas.get(position);
    holder.tv_title.setText(data);


}

@Override
public int getItemCount()  {

    return datas.size();
}

class ViewHolder extends RecyclerView.ViewHolder {
   private ImageView recyclerview_image_one;
   private TextView tv_title;
    public ViewHolder(View itemView) {
        super(itemView);
        recyclerview_image_one=(ImageView)itemView.findViewById(R.id.recyclerview_image_one);
        tv_title=(TextView)itemView.findViewById(R.id.tv_title);
         }
}

}

  • 主活动布局文件

  •     <android.support.v7.widget.RecyclerView
         android:id="@+id/recyclerview"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_alignParentLeft="true"
         android:layout_alignParentStart="true"
         android:layout_marginTop="10dp"
         android:layout_below="@+id/linearLayout3">
    
     </android.support.v7.widget.RecyclerView>
    
  • recyclerview——item。xml

  • <?xml version="1.0" encoding="utf-8"?>

<ImageView
   android:id="@+id/recyclerview_image_one"
    android:src="@drawable/close_one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    />
<TextView
    android:id="@+id/tv_title"
    android:text="Context"
    android:gravity="center"
    android:layout_width="100dp"
    android:layout_height="100dp" />
  • 这样一个瀑布流效果就完成了
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页