用RecyclerView实现水平滚动和网格视图
作者:互联网
建立RecyclerViewActivity.java文件
1 public class RecyclerViewActivity extends AppCompatActivity { 2 private Button BtnLinear; 3 private Button BtnShuiping; 4 private Button BtnGrid; 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_recycler_view);//遇到R标红问题时import class就好了 9 BtnLinear=findViewById(R.id.btn_linear); 10 BtnShuiping=findViewById(R.id.btn_shuiping); 11 BtnGrid=findViewById(R.id.btn_grid); 12 BtnLinear.setOnClickListener(new View.OnClickListener() { 13 @Override 14 public void onClick(View view) { 15 Intent intent=new Intent(RecyclerViewActivity.this,LinearRecyclerViewActivity.class); 16 startActivity(intent); 17 } 18 }); 19 BtnShuiping.setOnClickListener(new View.OnClickListener() { 20 @Override 21 public void onClick(View view) { 22 Intent intent=new Intent(RecyclerViewActivity.this,ShuipRecyclerViewActivity.class); 23 startActivity(intent); 24 } 25 }); 26 BtnGrid.setOnClickListener(new View.OnClickListener() { 27 @Override 28 public void onClick(View view) { 29 Intent intent=new Intent(RecyclerViewActivity.this,GridRecyclerViewActivity.class); 30 startActivity(intent); 31 } 32 }); 33 } 34 }
对应的activity_recycler_view.xml文件:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="wrap_content" 4 android:orientation="vertical"> 5 6 <Button 7 android:id="@+id/btn_linear" 8 android:layout_width="match_parent" 9 android:layout_height="wrap_content" 10 android:text="列表视图"> 11 </Button> 12 13 <Button 14 android:id="@+id/btn_shuiping" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" 17 android:text="水平滚动"/> 18 19 <Button 20 android:id="@+id/btn_grid" 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" 23 android:text="网格视图"> 24 </Button> 25 26 27 </LinearLayout>
实现水平滚动:
建立ShuipRecyclerViewActivity.java文件:
1 public class ShuipRecyclerViewActivity extends AppCompatActivity { 2 private RecyclerView RVshuip; 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_shuip_recycler_view); 7 RVshuip=findViewById(R.id.rv_shuip); 8 LinearLayoutManager linearLayoutManager=new LinearLayoutManager(ShuipRecyclerViewActivity.this); 9 linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);//注意linearLayoutManager的开头大小写,注意区分 10 RVshuip.setLayoutManager(linearLayoutManager); 11 RVshuip.addItemDecoration(new MyDecoration()); 12 RVshuip.setAdapter(new ShuipAdapter(ShuipRecyclerViewActivity.this, new ShuipAdapter.OnItemClickListener() { 13 @Override 14 public void onClick(int pos) { 15 Toast.makeText(ShuipRecyclerViewActivity.this, "点击"+pos, Toast.LENGTH_SHORT).show(); 16 } 17 })); 18 } 19 20 class MyDecoration extends RecyclerView.ItemDecoration { 21 @Override 22 public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { 23 super.getItemOffsets(outRect, view, parent, state); 24 outRect.set(getResources().getDimensionPixelOffset(R.dimen.deviderHeight), 0, 0, 0); 25 }//给列表视图在左面设置分隔线,记得在上面添加RVshuip.setLayoutManager(linearLayoutManager);这句话 26 } 27 }
对应的activity_shuip_recycler_view.xml文件:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent"> 4 5 <androidx.recyclerview.widget.RecyclerView 6 android:id="@+id/rv_shuip" 7 android:layout_width="match_parent" 8 android:layout_height="wrap_content" 9 android:background="@color/greydrak"/> 10 </LinearLayout>
建立适配器ShuipAdapter.java文件:
1 public class ShuipAdapter extends RecyclerView.Adapter<ShuipAdapter.LinearViewHolder>{ 2 private Context mcontext; 3 private OnItemClickListener mlistener; 4 public ShuipAdapter(Context context, OnItemClickListener listener){ 5 this.mcontext=context; 6 this.mlistener=listener; 7 } 8 @NonNull 9 @Override 10 public ShuipAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 11 return new LinearViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.layout_shuip_item,parent,false)); 12 }//这里原来是RecyclerView.ViewHolder,建立完LinearViewHolder class后要把它改成LinearAdapter.LinearViewHolder就可以了 13 14 @Override 15 public void onBindViewHolder(@NonNull ShuipAdapter.LinearViewHolder holder, int position) { 16 holder.textView.setText("大家好"); 17 holder.itemView.setOnClickListener(new View.OnClickListener() { 18 @Override 19 public void onClick(View view) { 20 //Toast.makeText(mcontext, "click...", Toast.LENGTH_SHORT).show();//第一种方法 21 mlistener.onClick(position);//第二种方法, 22 } 23 });//增加点击事件,这是第一种方法 24 }//这里原来是RecyclerView.ViewHolder,建立完LinearViewHolder class后要把它改成LinearAdapter.LinearViewHolder就可以了 25 26 @Override 27 public int getItemCount() { 28 return 20; 29 }//设置有多少行 30 31 class LinearViewHolder extends RecyclerView.ViewHolder { 32 private TextView textView; 33 public LinearViewHolder(@NonNull View itemView) { 34 super(itemView); 35 textView=itemView.findViewById(R.id.tv_rtitle); 36 } 37 } 38 public interface OnItemClickListener{ 39 void onClick(int pos); 40 }//点击事件的第二种方法,建立接口 41 }
最后是单个元素样式layout_shuip_item.xml文件:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="wrap_content" 3 android:layout_height="wrap_content" 4 android:orientation="vertical" 5 android:background="@color/white" 6 android:padding="10dp"> 7 8 <TextView 9 android:id="@+id/tv_rtitle" 10 android:layout_width="wrap_content" 11 android:layout_height="50dp" 12 android:gravity="center" 13 android:textColor="@color/black" 14 android:textSize="20sp"/> 15 </LinearLayout>
实现网格视图:
建立GridRecyclerViewActivity.java文件:
1 public class GridRecyclerViewActivity extends AppCompatActivity { 2 private RecyclerView RVgrid; 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_grid_recycler_view); 7 RVgrid=findViewById(R.id.rv_grid); 8 RVgrid.setLayoutManager(new GridLayoutManager(GridRecyclerViewActivity.this,3));//3是几列的意思 9 RVgrid.setAdapter(new GridAdapter(GridRecyclerViewActivity.this, new GridAdapter.OnItemClickListener() { 10 @Override 11 public void onClick(int pos) { 12 Toast.makeText(GridRecyclerViewActivity.this, "又点击了"+pos, Toast.LENGTH_SHORT).show(); 13 } 14 })); 15 } 16 }
对应的activity_grid_recycler_view.xml文件:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent"> 4 <androidx.recyclerview.widget.RecyclerView 5 android:id="@+id/rv_grid" 6 android:layout_width="match_parent" 7 android:layout_height="wrap_content"/> 8 </LinearLayout>
建立适配器GridAdapter.java文件:
1 public class GridAdapter extends RecyclerView.Adapter<GridAdapter.LinearViewHolder>{ 2 private Context mcontext; 3 private OnItemClickListener mlistener; 4 public GridAdapter(Context context, OnItemClickListener listener){ 5 this.mcontext=context; 6 this.mlistener=listener; 7 } 8 @NonNull 9 @Override 10 public GridAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 11 return new LinearViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.layout_grid_recyclerview_item,parent,false)); 12 }//这里原来是RecyclerView.ViewHolder,建立完LinearViewHolder class后要把它改成LinearAdapter.LinearViewHolder就可以了 13 14 @Override 15 public void onBindViewHolder(@NonNull GridAdapter.LinearViewHolder holder, int position) { 16 holder.textView.setText("这是啥?!"); 17 holder.itemView.setOnClickListener(new View.OnClickListener() { 18 @Override 19 public void onClick(View view) { 20 //Toast.makeText(mcontext, "click...", Toast.LENGTH_SHORT).show();//第一种方法 21 mlistener.onClick(position);//第二种方法, 22 } 23 });//增加点击事件,这是第一种方法 24 }//这里原来是RecyclerView.ViewHolder,建立完LinearViewHolder class后要把它改成LinearAdapter.LinearViewHolder就可以了 25 26 @Override 27 public int getItemCount() { 28 return 21; 29 }//设置有多少个 30 31 class LinearViewHolder extends RecyclerView.ViewHolder { 32 private TextView textView; 33 public LinearViewHolder(@NonNull View itemView) { 34 super(itemView); 35 textView=itemView.findViewById(R.id.tv_rtitle); 36 } 37 } 38 public interface OnItemClickListener{ 39 void onClick(int pos); 40 }//点击事件的第二种方法,建立接口 41 }
最后是单个元素样式layout_grid_recyclerview_item.xml文件:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="wrap_content" 3 android:layout_height="wrap_content" 4 android:orientation="vertical" 5 android:background="@color/white" 6 android:padding="10dp"> 7 8 <TextView 9 android:id="@+id/tv_rtitle" 10 android:layout_width="wrap_content" 11 android:layout_height="50dp" 12 android:gravity="center" 13 android:textColor="@color/black" 14 android:textSize="20sp"/> 15 </LinearLayout>
标签:void,网格,视图,class,LinearViewHolder,Override,new,RecyclerView,public 来源: https://www.cnblogs.com/konglong-cm/p/15856925.html