APP学习6(RecycleView)
1. RecycleView
Android5.0之后提供的用于在有限的窗口范围内显示大量数据的控件。
与ListView相比,展示效果、适配器更好。
代码:
activity_main.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="match_parent" android:orientation="vertical" > <androidx.recyclerview.widget.RecyclerView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/mylist" ></androidx.recyclerview.widget.RecyclerView> </LinearLayout>
itemlayout.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="match_parent" android:padding="10dp" android:gravity="center" android:orientation="horizontal" > <ImageView android:layout_width="120dp" android:layout_height="100dp" android:id="@+id/iv" ></ImageView> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/title" android:text="标题" android:layout_marginLeft="20dp" android:textColor="@color/black" android:textSize="25sp"></TextView> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/info" android:text="详细信息" android:layout_marginTop="5dp" android:maxLines="2" android:layout_marginLeft="20dp" android:layout_below="@+id/title" android:textColor="@color/black" android:ellipsize="end" android:textSize="20sp"></TextView> </RelativeLayout> </LinearLayout>
MainActivity.java
package com.example.myapp; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends AppCompatActivity { /* * 1.获取recycleview的控件对象 * 2.设置recycleview控件的适配器,包括item视图的布局和数据内容都要在适配器中定义出来 * 3.设置recycleview的布局管理器 * 4.创建适配器对象,将此对象交给recycleview控件对象 * */ //1. private RecyclerView mylist; private String[] title={"苹果","草莓","西瓜","水蜜桃"}; private int[] imgs={R.drawable.apple,R.drawable.straw,R.drawable.water,R.drawable.mitao}; private String[] info={"苹果时红色的","草莓是草莓","西瓜是西瓜","水蜜桃是粉色的"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mylist = findViewById(R.id.mylist); //3.设置布局管理器 mylist.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,true)); //4.适配器 MyAdapter ma = new MyAdapter(); mylist.setAdapter(ma); } class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder>{ @NonNull @Override//作用:获取itemview的视图对象,并将视图对象传递给viewholder public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View v1 = View.inflate(MainActivity.this,R.layout.itemlayout,null); MyHolder myHolder = new MyHolder(v1); return myHolder; } @Override//onBindViewHolder将值绑定给hold对象 public void onBindViewHolder(@NonNull MyHolder holder, int position) { holder.info.setText(info[position]); holder.title.setText(title[position]); holder.iv.setBackgroundResource(imgs[position]); } @Override public int getItemCount() { return title.length; } class MyHolder extends RecyclerView.ViewHolder{ //需要赋值的控件对象 ImageView iv; TextView title,info; public MyHolder(@NonNull View itemView) { super(itemView); iv = itemView.findViewById(R.id.iv); title = itemView.findViewById(R.id.title); info = itemView.findViewById(R.id.info); } } } }
这种方式特别方便,也需要多理解。