Android RecyclerView, androidrecycler
What is RecyclerView?
RecyclerView is a new view group designed to provide similar rendering methods for any adapter-based view. It is supported in the latest support-V7 version as the successor to the ListView and GridView controls.
Extensibility is fully taken into account when developing RecyclerView, so you can use it to create any layout that you think. However, it is slightly inconvenient to use. This is Android-it's not that easy to do one thing.
The RecyclerView architecture provides a plug-in experience, highly decoupled, and exceptional flexibility. By setting different LayoutManager, ItemDecoration, and ItemAnimator provided by the RecyclerView architecture, the results are astonishing.
RecyclerView can implement the following functions:
ListView Functions
Functions of the GridView
Horizontal ListView
Horizontal ScrollView
Waterfall effect
Easy to add and remove animations for items
Basic usage
Introduce official V7 packages
Main. xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" tools:showIn="@layout/activity_main"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="fill_parent" android:layout_height="fill_parent" /></RelativeLayout>
The statements in java code are the same as those in common controls;
recyclerView = (RecyclerView) findViewById(R.id.recyclerview); recyclerView.setLayoutManager(new GridLayoutManager(this,2)); recyclerView.setAdapter(adapter);
SetLayoutManager requires a LayoutManager class, which has three implementations:
1. Current LinearLayoutManager manager, supporting both horizontal and vertical
2. GridLayoutManager grid layout manager
3. StaggeredGridLayoutManager waterfall layout manager
The above Code uses GridLayoutManager to display the grid layout of one or two columns;
Let's talk about the adapter. RecyclerView contains a new type of adapter, which also requires ViewHolder. Two main methods need to be rewritten during use: one for displaying the view and its owner'sonCreateViewHolder(ViewGroup parent, int viewType)To bind data to a view.onBindViewHolder(ViewHolder holder, int position). The benefit of doing so is that,onCreateViewHolderIt is called only when we really need to create a new view, and we do not need to check whether it has been recycled.
Adapter code:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.myViewHolder> { private Context context; private List<String> mDatas; public RecyclerAdapter(Context context, List<String> mDatas) { super(); this.context = context; this.mDatas = mDatas; } @Override public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { myViewHolder holder = new myViewHolder(LayoutInflater.from(context).inflate(R.layout.item_home, parent, false)); return holder; } @Override public void onBindViewHolder(final myViewHolder holder, int position) { holder.tv.setText(mDatas.get(position)); } @Override public int getItemCount() { return mDatas.size(); } class myViewHolder extends RecyclerView.ViewHolder { TextView tv; public myViewHolder(View itemView) { super(itemView); tv = (TextView) itemView.findViewById(R.id.pos); } } }
Item code:
<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pos" android:layout_width="144dip" android:layout_height="72dip" android:background="@color/colorPrimary" android:gravity="center" android:layout_margin="4dip" android:textColor="#ffffff" />
The reason why we set a 4dp margin is that RecyclerView does not provide us with an attribute that you want to set the split line like listview, so you can set a margin in item, of course, you can use the addItemDecoration method of RecyclerView to customize a split line.
Run the above Code to display the effect:
Display Results Using LinearLayoutManager
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Display Effect of StaggeredGridLayoutManager
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
To use StaggeredGridLayoutManager to display the waterfall effect, you must randomly set the height of the item in the adapter.
Adapter code
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.myViewHolder> { private Context context; private List<String> mDatas; private List<Integer> mHeights; public RecyclerAdapter(Context context, List<String> mDatas) { super(); ... ... mHeights = new ArrayList<Integer>(); for (int i = 0; i < mDatas.size(); i++) { mHeights.add((int) (100 + Math.random() * 300)); }
} @Override public void onBindViewHolder(final myViewHolder holder, int position) { ViewGroup.LayoutParams lp = holder.tv.getLayoutParams(); lp.height = mHeights.get(position); holder.tv.setLayoutParams(lp); ... ... }
The other code is the same as the above adapter. Here, only the added part is added, and the running effect is as follows:
In the above Code, the second parameter constructed by StaggeredGridLayoutManager transmits an orientation. IfStaggeredGridLayoutManager.VERTICALIndicates the number of columns. If yesStaggeredGridLayoutManager.HORIZONTALThe number of rows. For example
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.HORIZONTAL));
Change
@Override public void onBindViewHolder(final myViewHolder holder, int position) { ViewGroup.LayoutParams lp = holder.tv.getLayoutParams(); lp.width = mHeights.get(position); holder.tv.setLayoutParams(lp);}
The running effect is as follows:
Only static images are pasted here, which can actually slide horizontally.
Let's talk about the animation effect of items. RecyclerView supports Custom Animation effects of items. Many user-defined effects have also been displayed on github. paste a connection here.
Https://github.com/wasabeef/recyclerview-animators
Of course, the system also provides us with a default effect.
recyclerView.setItemAnimator(new DefaultItemAnimator());
A line of code is enough. The result is as follows:
Add two methods to the adapter.
public void addData(int position) { mDatas.add(position, "New Item"); notifyItemInserted(position); } public void removeData(int position) { mDatas.remove(position); notifyItemRemoved(position); }
Activity.
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); switch (id) { case R.id.action_add: adapter.addData(1); break; case R.id.action_delete: adapter.removeData(1); break; } return super.onOptionsItemSelected(item); }
Note that RecyclerView is used to update data.notifyItemInserted(position)AndnotifyItemRemoved(position)HerenotifyDataSetChangedDifferent, although RecyclerView also providesnotifyDataSetChangedMethod, but usenotifyDataSetChangedThere is no animation effect.
The RecyclerView has a strong scalability, and the system does not provide the setOnItemClickListener Method for us. Therefore, we need to define the item callback interface in the adapter.
public interface OnItemClickLitener { void onItemClick(View view, int position); void onItemLongClick(View view, int position); } private OnItemClickLitener mOnItemClickLitener; public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) { this.mOnItemClickLitener = mOnItemClickLitener; }
Call in onBindViewHolder
@Override public void onBindViewHolder(final myViewHolder holder, int position) { ... ...if (mOnItemClickLitener != null) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int pos = holder.getLayoutPosition(); mOnItemClickLitener.onItemClick(holder.itemView, pos); } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { int position=holder.getLayoutPosition(); mOnItemClickLitener.onItemLongClick(holder.itemView,position); return true; } }); } }
Add the following code to the Activity:
adapter.setOnItemClickLitener(new RecyclerAdapter.OnItemClickLitener() { @Override public void onItemClick(View view, int position) { Toast.makeText(MainActivity.this, position + " click",Toast.LENGTH_SHORT).show(); } @Override public void onItemLongClick(View view, int position) { Toast.makeText(MainActivity.this, position + " LongClick",Toast.LENGTH_SHORT).show(); } });
Running effect:
To those who are not familiar with RecyclerView, please leave a message for the source code.
References
Https://github.com/wasabeef/recyclerview-animators
Http://blog.csdn.net/lmj623565791/article/details/45059587
Http://blog.jobbole.com/74208/