Detailed explanation and DeMo of the new android L control RecyclerView, androidrecycler
We can see on Google's official website that the introduction is as follows:RecyclerView
Is a more advanced and flexible versionListView
. This widget is a container for large sets of views that can be recycled and scrolled very efficiently. UseRecyclerView
Widget when you have lists with elements that change dynamically.
RecyclerView is more advanced and flexible than listview. For many views, it is a container that can be effectively reused and rolled. Use it when the data changes dynamically.
RecyclerView
Is easy to use, because it provides:
- A layout manager for positioning items
- Default animations for common item operations
- You also have the flexibility to define custom layout managers and animations for this widget.
RecyclerView is easy to use because it provides:
It provides a layoutmanager for item locating.
Provides a default animations for item operations.
You can also flexibly define the widget's custom layout manager and animation
To useRecyclerView
Widget, you have to specify an adapter and a layout manager. To create an adapter, you extendRecyclerView.Adapter
Class. The details of the implementation depend on the specifics of your dataset and the type of views. For more information, see the examples below.
To use RecyclerVIew, you must specify an adapter and a layoutmanager. To create an adapter, you must inherit RecyclerView. Adapter. The detailed implementation method depends on your dataset and the type of your view.
Demo introduction is different from official website
Now we have finished introducing the Demo below. If you need to view the Demo on the official website, open here: Official Demo
Since the details here are different from the official Demo, let's take a look at the effect we have to do.
Implement mixed sorting of image text buttons.
First, let's look at my engineering structure.
First, paste my main_acitivy.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="vertical" > <!-- A RecyclerView with some commonly used attributes --><android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent"/></LinearLayout>
You don't need to post several other xml files. You can easily write TextVIew, ImgeView, and so on.
Then let's look at the code. The first is the code in Bean.
Package com. androidl. bob;/*** entity package ** @ author edsheng **/public class Bean {public static final int Y_TYPE = 0; // view type 0 public static final int X_TYPE = 1; // view type 2 public static final int Z_TYPE = 2; // view type 3 private int type; private String text; public Bean (int type, String text) {super (); this. type = type; this. text = text;} public int getType () {return type;} public void setType (int type) {this. type = type;} public String getText () {return text;} public void setText (String text) {this. text = text ;}}
Then there is the code in the Adapter:
Package com. androidl. bob; import java. util. list; import com. example. androidl. r; import android. support. v7.widget. recyclerView; import android. support. v7.widget. recyclerView. viewHolder; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. button; import android. widget. imageButton; import android. widget. imageView; import android. widget. textView; import android. widget. toast;/*** Date: 2014/7/15 ** @ author edsheng **/public class RecycleAdapter extends RecyclerView. adapter <ViewHolder> {private List <Bean> beans; public RecycleAdapter (List <Bean> beans) {super (); this. beans = beans;}/*** internal TextHoler ** @ author edsheng **/public class TextHoler extends RecyclerView. viewHolder {public TextView textView; public TextHoler (View textview) {super (textview); this. textView = (TextView) textview. findViewById (R. id. mytext) ;}}/*** iamgeHolder *** @ author edsheng ***/public class ImageHoler extends RecyclerView. viewHolder {public ImageView Imageview; public ImageHoler (View textview) {super (textview); this. imageview = (ImageView) textview. findViewById (R. id. myiamge) ;}}/*** button's holder ** @ author edsheng **/public class ButtonHolder extends RecyclerView. viewHolder {public Button button; public ButtonHolder (View textview) {super (textview); this. button = (Button) textview. findViewById (R. id. mybutton) ;}@ Overridepublic int getItemCount () {// TODO Auto-generated method stubreturn beans. size ();}/*** obtain the Message Type */@ Overridepublic int getItemViewType (int position) {// TODO Auto-generated method stubreturn beans. get (position ). getType ();}/*** create VIewHolder */@ Overridepublic ViewHolder onCreateViewHolder (ViewGroup parent, int viewtype) {// TODO Auto-generated method stubView v = null; viewHolder holer = null; switch (viewtype) {case Bean. x_TYPE: v = LayoutInflater. from (parent. getContext ()). inflate (R. layout. recylce_item_x, null); holer = new TextHoler (v); break; case Bean. y_TYPE: v = LayoutInflater. from (parent. getContext ()). inflate (R. layout. recylce_item_y, null); holer = new ButtonHolder (v); break; case Bean. z_TYPE: v = LayoutInflater. from (parent. getContext ()). inflate (R. layout. recylce_item_z, null); holer = new ImageHoler (v); break;} return holer;}/*** bind viewholder */@ Overridepublic void onBindViewHolder (ViewHolder holder, int position) {// TODO Auto-generated method stubswitch (getItemViewType (position) {case Bean. x_TYPE: TextHoler textholer = (TextHoler) holder; textholer. textView. setText (beans. get (position ). getText (); break; case Bean. y_TYPE: ButtonHolder buttonHolder = (ButtonHolder) holder; buttonHolder. button. setText (beans. get (position ). getText (); break; case Bean. z_TYPE: ImageHoler imageHoler = (ImageHoler) holder; // imageHoler. imageview. setImageResource (android. r. drawable. checkbox_on_background); break ;}}}
Finally, the Code of the activity.
Package com. androidl. bob; import java. util. arrayList; import java. util. list; import android. app. activity; import android. OS. bundle; import android. support. v7.widget. linearLayoutManager; import android. support. v7.widget. recyclerView; import com. example. androidl. r; public class Mainactivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. main_activity); RecyclerView mRecyclerView = (RecyclerView) findViewById (R. id. my_recycler_view); // improve performance if you know that changes in content /// do not change the size of the RecyclerView // mRecyclerView. setHasFixedSize (true); // create the layout manager LinearLayoutManager mLayoutManager = new LinearLayoutManager (this); mLayoutManager. setOrientation (LinearLayoutManager. VERTICAL); mRecyclerView. setLayoutManager (mLayoutManager); // initialize the data List <Bean> myDataset = new ArrayList <Bean> (); myDataset. add (new Bean (Bean. z_TYPE, "image"); myDataset. add (new Bean (Bean. x_TYPE, "text"); myDataset. add (new Bean (Bean. y_TYPE, "button"); myDataset. add (new Bean (Bean. z_TYPE, "image"); myDataset. add (new Bean (Bean. x_TYPE, "shit"); myDataset. add (new Bean (Bean. x_TYPE, ""); myDataset. add (new Bean (Bean. z_TYPE, "image"); myDataset. add (new Bean (Bean. y_TYPE, "button"); myDataset. add (new Bean (Bean. y_TYPE, "button"); myDataset. add (new Bean (Bean. x_TYPE, "text"); // create AdapterRecycleAdapter mAdapter = new RecycleAdapter (myDataset); mRecyclerView. setAdapter (mAdapter );}}
Demo Portal: Start Transfer
How to use code to generate a View control in android
Private LinearLayout myLayout;
Private TextView mTextView;
MyLayout = new LinearLayout (this );
MTextView = new TextView (this );
MyLayout. addView (mTextView );
How does android use CheckBox + ListView to obtain the values of multiple controls on the selected items!
The score is too low !!!