RecyclerView使用
JonFullerto
9年前
来自: http://my.oschina.net/gef/blog/607017
android.support.v7.widget.RecyclerView是v7包里的一个widget,非常强大的一个控件,完全可以代替listview,gridview和瀑布流。
这里主要讲三点:
-
为recyclerView设置LayoutManager
-
RecyclerView.Adapter
-
删除和添加数据的动画效果展示
1.
RecyclerView中有三种LayoutManager,如下:
LayoutManager | Orientation |
LinearLayoutManager | HORIZONTAL、VERTICAL |
GridLayoutManager | HORIZONTAL、VERTICAL |
StaggeredGridLayoutManager | HORIZONTAL、VERTICAL |
例如:
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); //设置layoutmanager recyclerView.setLayoutManager(linearLayoutManager);
2.RecyclerView.Adapter
这个RecyclerView.Adapter可不得了啊,首先可以带范型,看源码:
public static abstract class Adapter<VH extends ViewHolder> {。。。}
然后是个抽象类,举个例子:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{ @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(parent.getContext(),R.layout.myview,null); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.content.setText(arrayList.get(position)); } @Override public int getItemCount() { return arrayList.size(); } public class ViewHolder extends RecyclerView.ViewHolder{ private TextView content; public ViewHolder(View itemView) { super(itemView); content = (TextView) ((FrameLayout) itemView).getChildAt(0); } } }
这里我自定了一个ViewHolder继承RecyclerView.ViewHolder,说一下MyAdapter的执行流程:首先执行onCreateViewHolder方法,返回了我们自定义的ViewHolder,然后onBindViewHolder方法。可以看出RecyclerView.Adapter对ViewHolder进行了封装。
3.删除和添加数据的动画效果展示,如果是baseAdapter怎么刷新呢,调用notifyDataSetChanged()方法,那在使用RecyclerView时呢:
notifyItemInserted(int position); notifyItemRemoved(int position);
而且还有动画效果。
好了来个例子:
R.layout.activity_recycler_view文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.sumu.gefdemo.activity.RecyclerViewActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </RelativeLayout>
actiivty中:
package com.sumu.gefdemo.activity; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.TextView; import com.sumu.gefdemo.R; import java.util.ArrayList; import java.util.Random; public class RecyclerViewActivity extends AppCompatActivity { private RecyclerView recyclerView; private MyAdapter myAdapter; private ArrayList<String> arrayList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycler_view); recyclerView = (RecyclerView) findViewById(R.id.recyclerview); /** * 三种LayoutManager */ //1 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); //2. GridLayoutManager gridLayoutManager = new GridLayoutManager(this,4); gridLayoutManager.setOrientation(GridLayoutManager.HORIZONTAL); //3. StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL); //设置layoutmanager recyclerView.setLayoutManager(staggeredGridLayoutManager); Random random = new Random(); for (int i = 0;i < 100;i++){ random.nextInt(); arrayList.add(i + "gefufeng" + random.nextInt(10000000)); } myAdapter = new MyAdapter(); recyclerView.setAdapter(myAdapter); } public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{ @Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(parent.getContext(),R.layout.myview,null); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.content.setText(arrayList.get(position)); } @Override public int getItemCount() { return arrayList.size(); } public class ViewHolder extends RecyclerView.ViewHolder{ private TextView content; public ViewHolder(View itemView) { super(itemView); content = (TextView) ((FrameLayout) itemView).getChildAt(0); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0,1,1,"add"); menu.add(Menu.NONE,2,2,"delete"); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == 1){ arrayList.add(1,"gefufenggefufenggefufenggefufenggefufeng"); myAdapter.notifyItemInserted(1); }else{ arrayList.remove(2); myAdapter.notifyItemRemoved(2); } return super.onOptionsItemSelected(item); } }
R.layout.myview文件:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:padding="10dp" android:background="#3f3" android:gravity="center" android:text="gefufebg" android:layout_margin="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </FrameLayout>
效果图(其中之一):