依赖一个用于大量数据展示的新控件,用来代替ListView
使用前需要导入依赖
//添加recyclerView依赖包 implementation 'androidx.recyclerview:recyclerview:1.1.0'实例练习
activity_main.xml
创建一个recyclerview_item.xml
创建两个类
Bean
package com.example.mylistview; public class Bean { String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
MyAdapter
该类中每个方法有着相应的注释
并且在此类中绑定了项点击事件
package com.example.myrecyclerview; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class MyAdapter extends RecyclerView.Adapter{ private List data; private Context context; public MyAdapter(List data, Context context) { this.data = data; this.context = context; } @NonNull @Override public myviewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view=View.inflate(context,R.layout.recyclerview_item,null); return new myviewHolder(view); } // 绑定数据 @Override public void onBindViewHolder(@NonNull myviewHolder holder, int position) { holder.tv.setText(data.get(position).getName()); } //显示的item长度 @Override public int getItemCount() { return data==null ? 0 : data.size(); } public class myviewHolder extends RecyclerView.ViewHolder{ private TextView tv; public myviewHolder(@NonNull View itemView) { super(itemView); tv=itemView.findViewById(R.id.tv); //点击事件 itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(onitemClickListener!=null){ onitemClickListener.onRecyclerItemclick(getAdapterPosition()); } } }); } } private OnRecyclerItemClickListener onitemClickListener; public void setRecyclerItemClickListener(OnRecyclerItemClickListener onitemlistener){ onitemClickListener=onitemlistener; } public interface OnRecyclerItemClickListener{ void onRecyclerItemclick(int position); } }
MainActivity
package com.example.myrecyclerview; import android.util.Log; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.StaggeredGridLayoutManager; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private Listdata=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); for (int i=0;i<1000;i++){ if(i%2!=0){ continue; } Bean bean=new Bean(); bean.setName("博客CSDN碰磕"+i); data.add(bean); } RecyclerView recyclerView=findViewById(R.id.rv); //线性布局 // LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this); // recyclerView.setLayoutManager(linearLayoutManager); //网格布局 // GridLayoutManager gridLayoutManager=new GridLayoutManager(this,3); // recyclerView.setLayoutManager(gridLayoutManager); //瀑布流 StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL); recyclerView.setLayoutManager(staggeredGridLayoutManager); MyAdapter adapter=new MyAdapter(data,this); recyclerView.setAdapter(adapter); adapter.setRecyclerItemClickListener(new MyAdapter.OnRecyclerItemClickListener() { @Override public void onRecyclerItemclick(int position) { Log.e("CSDN", "点击成功" + position); } }); } }
RecyclerView可作多个布局显示,此处练习了
线性布局,网格布局,
最后以瀑布流结尾
最终实例效果:
item点击事件触发效果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)