RecylerView的介绍与简单应用

简单介绍

    RecylerView能够灵活地实现大数据集的展示,视图的复用管理比ListView更好,能够显示列表,网格,瀑布流等形式,且不同的ViewHolder能够实现item多元化的功能.

使用准备,添加依赖

implementation 'androidx.recyclerview:recyclerview:1.1.0'

下列例子所有依赖

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
}

RecyclerView 的简单使用

一:RecyclerView 的初始化

        recyclerView = findViewById(R.id.recycler1);
        recyclerView.setLayoutManager(new LinearLayoutManager(LinearRecycleActivity.this));
       //recyclerView.setAdapter(new CLinearAdapter(LinearRecycleActivity.this));简单使用
        recyclerView.setAdapter(new CLinearAdapter(LinearRecycleActivity.this, new CLinearAdapter.onItemClickListener() {
            @Override
            public void click(int pos) {
                Toast.makeText(LinearRecycleActivity.this,"InActivity cilik is "+pos,Toast.LENGTH_SHORT).show();
            }
        }));//这里嵌入了一个lambda表达式,用于实现监听功能

 //xml:       
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:padding="50dp">

    <Button
        android:id="@+id/btn_toListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="toListView"
        android:textAllCaps="false"/>

</LinearLayout>
  • 二:RecyclerView 的 Adapter

public class CLinearAdapter extends RecyclerView.Adapter <CLinearAdapter.LinearViewHolder>{

    private Context context;

    private onItemClickListener listener;

    CLinearAdapter(Context context,onItemClickListener listener){
        this.context = context;
        this.listener = listener;
    }

    @Override
    public CLinearAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LinearViewHolder linearViewHolder = new LinearViewHolder(LayoutInflater.from(context).inflate(R.layout.linear_item,parent,false));
        return linearViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull CLinearAdapter.LinearViewHolder holder, int position) {
        //设置item
        holder.textView.setText("onBindViewHolder");
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//点击事件
               // Toast.makeText(context,"inAdapter onClick is "+position,Toast.LENGTH_SHORT).show();
                listener.click(position);
            }
        });


    }

    @Override
    public int getItemCount() {
        return 20;
    }//设置item的个数

    class LinearViewHolder extends RecyclerView.ViewHolder{//item的样式

        TextView textView;

        public LinearViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.linear_text);
        }
    }

    public interface  onItemClickListener{//给外部的接口,以实现点击事件
        void click(int pos);
    }
}

//xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:background="#ffffff">

    <TextView
        android:gravity="center"
        android:text="hello"
        android:id="@+id/linear_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp" />

</LinearLayout>
这里体现了一个RecyclerView 和 ListView的不同点:在ListView中,可以直接调用监听事件,但RecyclerView需要自己去定义实现监听
  • 二:设为水平滚动

public class HorRecyleActivity extends AppCompatActivity {

    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hor);

        recyclerView = findViewById(R.id.recycler_hor);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(HorRecyleActivity.this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        recyclerView.setAdapter(new CLinearAdapter(HorRecyleActivity.this, new CLinearAdapter.onItemClickListener() {
            @Override
            public void click(int pos) {
                Toast.makeText(HorRecyleActivity.this,"InActivity cilik is "+pos,Toast.LENGTH_SHORT).show();
            }
        }));
    }

}

  • 三:item设为网格布局

public class GridRecyleActivity extends AppCompatActivity {

    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_recyle);

        recyclerView = findViewById(R.id.recycler_grid);
        recyclerView.setLayoutManager(new GridLayoutManager(GridRecyleActivity.this,5));//一行5个
        recyclerView.setAdapter(new CLinearAdapter(GridRecyleActivity.this, new CLinearAdapter.onItemClickListener() {
            @Override
            public void click(int pos) {
                Toast.makeText(GridRecyleActivity.this,"InActivity cilik is "+pos,Toast.LENGTH_SHORT).show();
            }
        }));
    }
}
  • 四:RecyclerView扩展:瀑布流

public class FallRecyleActivity2 extends AppCompatActivity {
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fall_recyle2);
        recyclerView = findViewById(R.id.recycler_fall);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(5,StaggeredGridLayoutManager.VERTICAL));//5列
        recyclerView.setAdapter(new CLinearAdapter(FallRecyleActivity2.this, new CLinearAdapter.onItemClickListener() {
            @Override
            public void click(int pos) {
                Toast.makeText(FallRecyleActivity2.this,"InActivity cilik is "+pos,Toast.LENGTH_SHORT).show();
            }
        }));
    }
}
//Adpater继承的对象不同
public class StaggerdAdapter extends RecyclerView.Adapter <StaggerdAdapter.LinearViewHolder>{
  • 五:多种viewHolder

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

    private Context context;

    private onItemClickListener listener;

    CLinearAdapter(Context context,onItemClickListener listener){
        this.context = context;
        this.listener = listener;
    }

    @Override
    public int getItemViewType(int position){
        return  position&1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType==1) {
            LinearViewHolder linearViewHolder = new LinearViewHolder(LayoutInflater.from(context).inflate(R.layout.linear_item,parent,false));
            return linearViewHolder;
        }
        else {
            LinearViewHolder2 linearViewHolder = new LinearViewHolder2(LayoutInflater.from(context).inflate(R.layout.linear2_item,parent,false));
            return linearViewHolder;
        }

    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder,final int position) {
        //设置item
        if(getItemViewType(position)==1){
            ((LinearViewHolder)holder).textView.setText("onBindViewHolder1");
        }else{
            ((LinearViewHolder2)holder).textView.setText("onBindViewHolder2");
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//点击事件
               // Toast.makeText(context,"inAdapter onClick is "+position,Toast.LENGTH_SHORT).show();
                listener.click(position);
            }
        });


    }

    @Override
    public int getItemCount() {
        return 20;
    }//设置item的个数

    class LinearViewHolder extends RecyclerView.ViewHolder{//item的样式

        TextView textView;

        public LinearViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.linear_text);
        }
    }

    class LinearViewHolder2 extends RecyclerView.ViewHolder{//item的样式

        TextView textView;

        public LinearViewHolder2(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.linear_text2);
        }
    }

    public interface  onItemClickListener{//给外部的接口
        void click(int pos);
    }

}

上一篇:设计模式-抽象工厂模式


下一篇:只需一篇文章“吃透”Android多线程技术,安卓内存优化工具