New Android control RecyclerView analysis and pull-up and pull-down refresh, android drop-down box controls
Overview:
RecyclerView is a new Widgets in the android-support-v7-21 version, and RecyclerView is an upgraded version of ListView, more advanced and flexible. In future development, we can directly use RecyclerView to replace ListView.
Features:
1. Horizontal display
2. Eliminate dislocation
3. Standardized ViewHolder
Horizontal:
private void initHorizaontal(List<ItemModel> models) {RecyclerView recyclerView = (RecyclerView) findViewById(R.id.activity_main_horizontal_recyclerview);LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);recyclerView.setLayoutManager(layoutManager);RecyclerViewAdapter adapter = new RecyclerViewAdapter(MainActivity.this, models);recyclerView.setAdapter(adapter);}
Vertical:
public void initVertical(List<ItemModel> models) {RecyclerView recyclerView = (RecyclerView) findViewById(R.id.activity_main_vertical_recyclerview);LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);layoutManager.setOrientation(LinearLayoutManager.VERTICAL);recyclerView.setLayoutManager(layoutManager);RecyclerViewAdapter adapter = new RecyclerViewAdapter(MainActivity.this, models);recyclerView.setAdapter(adapter);}
Adapter changes:
Google's improvements to RecyclerView have also solved a headache for beginners: dislocation.
public class RecyclerViewAdapter extends RecyclerView.Adapter<ViewHolder> {private List<ItemModel> mList = null;private Context mContext = null;private LayoutInflater mInflater = null;public RecyclerViewAdapter(Context context, List<ItemModel> list) {mContext = context;mList = list;mInflater = LayoutInflater.from(mContext);}@Overridepublic int getItemCount() {if (mList != null) {return mList.size();}return 0;}@Overridepublic void onBindViewHolder(ViewHolder viewHolder, int position) {((ItemViewHolder)viewHolder).mLabelTextView.setText(mList.get(position).getLabel());}@Overridepublic ViewHolder onCreateViewHolder(ViewGroup viewGroup, int arg1) {View view = mInflater.inflate(R.layout.list_item, viewGroup, false);ItemViewHolder holder = new ItemViewHolder(view);holder.mLabelTextView = (TextView) view.findViewById(R.id.list_item_textview);return holder;}public static class ItemViewHolder extends ViewHolder{ public ItemViewHolder(View itemView) { super(itemView); } private TextView mLabelTextView = null; }}
Pull up or pull down to refresh:
For pull-down refresh, Android has implemented the SwipeRefreshLayout control. We put the SwipeRefreshLayout package outside the RecyclerView, and then set it to bind to an OnRefreshListener.
Layout:
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/swipe_refresh_widget" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.RecyclerView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="@null" android:scrollbars="vertical" /></android.support.v4.widget.SwipeRefreshLayout>
Listener:
@Override public void onRefresh() { handler.sendEmptyMessageDelayed(0, 3000); }
For pull-up refresh, We customize a FooterView and thread.
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == adapter.getItemCount()) { handler.sendEmptyMessageDelayed(1, 3000); } } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); lastVisibleItem = mLayoutManager.findLastVisibleItemPosition(); } });
Download related source code:
Preliminary use of RecyclerView
RecyclerView pull-up and pull-down refresh