RecycleVIew CardView总结

    科技2022-07-10  107

    RecycleVIew 

    添加RecycleView依赖 implementation 'androidx.cardview:cardview:1.0.0' implementation 'com.android.support:recyclerview-v7:27.1.1'

     

    findViewById找到控件找到控件 public class Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); //找到控件 mlist = (RecyclerView) this.findViewById(R.id.recycler_view2); } } <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="com.example.beach.Main2Activity"> <androidx.recyclerview.widget.RecyclerView android:background="#d1d1d1" //控件名称 android:id="@+id/recycler_view2" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>

     

    准备数据布局管理器LayoutManager 适配器Adapter 设置适配器 // gridList显示 private void showGrid(boolean VERTICAL, boolean Reverse) { // 布局管理器 GridLayoutManager layoutManager=new GridLayoutManager(this,2); layoutManager.setOrientation(VERTICAL? LinearLayoutManager.HORIZONTAL: LinearLayoutManager.VERTICAL); layoutManager.setReverseLayout(Reverse); // 绑定布局管理器 mlist.setLayoutManager(lm); GridViewAdapter datpter=new GridViewAdapter(mdata); mlist.setAdapter(datpter); } //适配器 Adapter package com.example.beach.Adapters; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import com.example.beach.Beans.ItemBean; import com.example.onecode.R; import org.w3c.dom.Text; import java.util.List; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; public class GridViewAdapter extends RecyclerView.Adapter<GridViewAdapter.InnerHolder> { private final List<ItemBean> mdata; public GridViewAdapter(List<ItemBean> data){ this.mdata=data; } @NonNull @Override public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { // 创建条目 View view=View.inflate(parent.getContext(), R.layout.item_grid_view,null); return new InnerHolder(view); } @Override public void onBindViewHolder(@NonNull InnerHolder holder, int position) { // 绑定数据 holder.setData(mdata.get(position)); } @Override public int getItemCount() { if(mdata !=null){ return mdata.size(); } return 0; } public class InnerHolder extends RecyclerView.ViewHolder { // 控件设置 private TextView mtitle; private ImageView micon; public InnerHolder(@NonNull View itemView) { super(itemView); mtitle=(TextView)itemView.findViewById(R.id.grid_view_title); micon=(ImageView)itemView.findViewById(R.id.grid_view_icon); } // 设置数据 public void setData(ItemBean itemBean) { mtitle.setText(itemBean.title); micon.setImageResource(itemBean.icon); } } }

     

    数据显示

    CardView

    app:cardUseCompatPadding="true" 每行间隔
    Processed: 0.013, SQL: 8