其他分享
首页 > 其他分享> > RecyclerView

RecyclerView

作者:互联网

首先用RecyclerView实现列表视图

建立RecyclerViewActivity.java文件:

 1 public class RecyclerViewActivity extends AppCompatActivity {
 2     private Button BtnLinear;
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_recycler_view);//遇到R标红问题时import class就好了
 7         BtnLinear=findViewById(R.id.btn_linear);
 8         BtnLinear.setOnClickListener(new View.OnClickListener() {
 9             @Override
10             public void onClick(View view) {
11                 Intent intent=new Intent(RecyclerViewActivity.this,LinearRecyclerViewActivity.class);
12                 startActivity(intent);
13             }
14         });
15     }
16 }

对应的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 
 5     <Button
 6         android:id="@+id/btn_linear"
 7         android:layout_width="match_parent"
 8         android:layout_height="wrap_content"
 9         android:text="列表视图">
10     </Button>
11 
12 </LinearLayout>

建立LinearRecyclerViewActivity.java文件:

 1 public class LinearRecyclerViewActivity extends AppCompatActivity {
 2     private RecyclerView RVmain;
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_linear_recycler_view);
 7         RVmain=findViewById(R.id.rv_main);
 8         RVmain.setLayoutManager(new LinearLayoutManager(LinearRecyclerViewActivity.this));
 9         RVmain.setAdapter(new LinearAdapter(LinearRecyclerViewActivity.this));
10     }
11 }

对应的activity_linear_recycler_view.xml文件:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="wrap_content">
 4 
 5     <androidx.recyclerview.widget.RecyclerView
 6         android:id="@+id/rv_main"
 7         android:layout_width="match_parent"
 8         android:layout_height="wrap_content"
 9         android:background="@color/greydrak"/>
10 </RelativeLayout>

最后建立列表视图的适配器,LinearAdapter.java文件:

 1 public class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.LinearViewHolder>{
 2     private Context mcontext;
 3     public LinearAdapter(Context context){
 4         this.mcontext=context;
 5     }
 6     @NonNull
 7     @Override
 8     public LinearAdapter.LinearViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
 9         return new LinearViewHolder(LayoutInflater.from(mcontext).inflate(R.layout.layout_linear_item,parent,false));
10     }//这里原来是RecyclerView.ViewHolder,建立完LinearViewHolder class后要把它改成LinearAdapter.LinearViewHolder就可以了
11 
12     @Override
13     public void onBindViewHolder(@NonNull LinearAdapter.LinearViewHolder holder, int position) {
14         holder.textView.setText("大家好,我又来了hiahia");
15     }//这里原来是RecyclerView.ViewHolder,建立完LinearViewHolder class后要把它改成LinearAdapter.LinearViewHolder就可以了
16 
17     @Override
18     public int getItemCount() {
19         return 20;
20     }//设置有多少行
21 
22     class LinearViewHolder extends RecyclerView.ViewHolder {
23         private TextView textView;
24         public LinearViewHolder(@NonNull View itemView) {
25             super(itemView);
26             textView=itemView.findViewById(R.id.tv_rtitle);
27         }
28     }
29 }

单个元素的样式为layout_linear_iteam.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     android:background="@color/white">
 6 
 7     <TextView
 8         android:id="@+id/tv_rtitle"
 9         android:layout_width="match_parent"
10         android:layout_height="50dp"
11         android:gravity="center"
12         android:textColor="@color/black"
13         android:textSize="20sp"/>
14 </LinearLayout>

最后注意xml文件中总布局的android:layout_height如果是match_parent的话达不到想要的效果,所以要给他改成wrap_content

标签:layout,class,LinearAdapter,LinearViewHolder,RecyclerView,public
来源: https://www.cnblogs.com/konglong-cm/p/15856440.html