RecyclerView使用

xiaoxiao2021-02-28  117

第一步:build.gradle加入recyclerview-v7闭包

dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile 'com.android.support:recyclerview-v7:25.0.3' }

第二步:使用recyclerview完成布局:

<android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="wrap_content" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView>

第三步:创建model类,Fruit.class

public class Fruit { private int fruitId; private String fruitName; public Fruit(int fruitId, String fruitName) { this.fruitId = fruitId; this.fruitName = fruitName; } public int getFruitId() { return fruitId; } public String getFruitName() { return fruitName; } }

第四步:创建子项布局文件fruit_item.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="horizontal"> <TextView android:id="@+id/fruit_id" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/fruit_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" /> </LinearLayout>

第四步:创建adapter适配器

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> { private List<Fruit> mFruitList; static class ViewHolder extends RecyclerView.ViewHolder{ private TextView fruitNameText; private TextView fruitIdText; public ViewHolder(View view){ super(view); //实例控件 fruitIdText =(TextView)view.findViewById(R.id.fruit_id); fruitNameText=(TextView)view.findViewById(R.id.fruit_name); } } public FruitAdapter(List<Fruit> fruitList){ mFruitList=mFruitList; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //获得View View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false); ViewHolder viewHolder=new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { //为控件添加数据 Fruit fruit=mFruitList.get(position); holder.fruitIdText.setText(fruit.getFruitId()); holder.fruitNameText.setText(fruit.getFruitName()); } @Override public int getItemCount() { return mFruitList.size(); } }

第五步:使用为recyclerview设置适配器和布局,以及添加数据

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView recyclerView=(RecyclerView)findViewById(R.id.recycler_view); FruitAdapter adapter=new FruitAdapter(initFruitList()); //三种布局管理器 LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this); // GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2); // StaggeredGridLayoutManager staggeredGridLayoutManager=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL); recyclerView.setAdapter(adapter); recyclerView.setLayoutManager(linearLayoutManager); } List<Fruit> initFruitList(){ List<Fruit> fruitList=new ArrayList<Fruit>(); for (int i=0;i<50;i++){ fruitList.add(new Fruit(String.valueOf(i),"fruit "+i)); } return fruitList; } }

效果演示暂无,嘿嘿。

转载请注明原文地址: https://www.6miu.com/read-20463.html

最新回复(0)