一:RecyclerView的用法:
RecyclerView是google在2014年I/O大会上提出新的用于取代ListView的组件,是 android-support-v7-21 版本中新增的一个 Widgets,它的灵活性与可替代性比listview更好。
使用 RecyclerView首先应该认识两个要点:
1. Adapter:使用RecyclerView之前,需要继承RecyclerView.Adapter定义一个自己的适配器,作用是将数据与每一个item的界面进行绑定,注意,与ListView不同,这里不在是使用传统的ArrayAdapter、SimpleAdapter、BaseAdapter。
2.LayoutManager: 控制每个Item项的布局显示。
目前SDK中提供了三种自带的LayoutManager:
LinearLayoutManager(可以实现垂直滚动的线性列表、水平线滚动性列表)
GridLayoutManager (可以实现垂直Grid列表、水平线Grid列表列表)
StaggeredGridLayoutManager (可以实现瀑布流列表)
RecyclerView的详细用法可以参考:
http://developer.android.com/reference/android/support/v7/widget/RecyclerView.html
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1118/2004.html
http://blog.csdn.net/lmj623565791/article/details/45059587
二:SwipeRefreshLayout的用法:
SwipeRefreshLayout也是一个下拉刷新控件,已经被加在在support v4兼容包下,两个要点:
1. 只要在需要刷新的控件最外层加上SwipeRefreshLayout,然后他的child首先是可滚动的view,如RecyclerView、ListView等
2. 实现SwipeRefreshLayout.OnRefreshListener接口,并找到SwipeRefreshLayout组件注册监听事件。注意:下拉刷新的数据更新逻辑代码应该写在onRefresh()方法中。
SwipeRefreshLayout的详细用法可以参考:
http://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1028/1861.html
三、下面我通过RecyclerView+SwipeRefreshLayout实现下拉刷新的垂直列表来演示RecyclerView及SwipeRefreshLayout的用法:
运行效果图gif:
1. 在build.gradle 中添加依赖
1
| compile 'com.android.support:recyclerview-v7:22.2.0'
|
这里只要是recyclerview-v7:21.0.+就可以了。
2. 创建布局文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/id_swiperefreshlayout" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView android:id="@+id/id_recyclerview" android:layout_width="match_parent" android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
|
3.继承RecyclerView.Adapter创建自己的Adapter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder>{
public String[] datass;
public MyRecyclerViewAdapter(String[] datas){ super(); this.datass = datas; }
public static interface OnItemClickListener{ void onItemClick(View view, int positon); }
private OnItemClickListener mOnItemClickListener; public void setOnItemClickListener(OnItemClickListener listener) { this.mOnItemClickListener = listener; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1,parent,false); ViewHolder viewHolder= new ViewHolder(view); return viewHolder; }
@Override public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.textView.setText(datass[position]); if (mOnItemClickListener!=null){ holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mOnItemClickListener.onItemClick(holder.itemView, position); } }); } }
@Override public int getItemCount() { return datass.length; }
public class ViewHolder extends RecyclerView.ViewHolder { public TextView textView;
public ViewHolder(View itemView) { super(itemView); textView = (TextView) itemView.findViewById(android.R.id.text1); } } }
|
4.在Activity中找到RecyclerView并绑定自定义的Adapter显示列表,实现SwipeRefreshLayout.OnRefreshListener接口,并找到SwipeRefreshLayout组件注册监听事件。注意:下拉刷新的数据更新逻辑代码应该写在onRefresh()方法中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{
private RecyclerView mRecyclerView; private SwipeRefreshLayout swipeRefreshLayout; private MyRecyclerViewAdapter adapter;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } });
initVerticalRecyclerView();
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.id_swiperefreshlayout); swipeRefreshLayout.setColorSchemeResources(android.R.color.white, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light); swipeRefreshLayout.setOnRefreshListener(this); }
private void initVerticalRecyclerView() {
mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview); LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setHasFixedSize(true);
final String[] datas = new String[20]; for(int i=0; i<datas.length; i++){ datas[i]="item"+i; }
adapter = new MyRecyclerViewAdapter(datas);
mRecyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new MyRecyclerViewAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int positon) { Toast.makeText(getBaseContext(), "item"+positon, Toast.LENGTH_LONG).show();
} }); }
@Override public void onRefresh() { new Handler().postDelayed(new Runnable() { @Override public void run() { swipeRefreshLayout.setRefreshing(false); for (int i=0; i<11; i++) { int temp = (int) (Math.random() * 10); adapter.datass[i] = "new" + temp; } adapter.notifyDataSetChanged(); } }, 1000);
} }
|
完整源码可以到我的github下载:https://github.com/crazyfzw/RecyclerViewDemo