Method 1: RecyclerView

Source: Internet
Author: User

Method 1: RecyclerView
I. Preface

RecyclerView is a new control added under the Google V7 package to replace ListView. In RecyclerView, ViewHolder is standardized and similar to convertView in ListView for view easing.

Let's talk about RecyclerView. It can quickly implement the effects of listview, gridview, and waterfall streams by setting LayoutManager, and set horizontal and vertical display, it is also very easy to add an animation effect (with ItemAnimation, you can set the animation for loading and removal to facilitate Dynamic Browsing), which is also officially recommended. the following is an official description:

RecyclerView is a more advanced and flexible version of ListView. this widget is a container for large sets of views that can be recycled and scrolled very efficiently. use the RecyclerView widget when you have lists with elements that change dynamically. simply put, you need to use it when you need to dynamically display a group of data.

After talking about so many virtual objects, let's look at how to implement them in the code.

II. Implementation

First, use this control. You need to add a package reference to the gradle file (used with the official CardView)

compile 'com.android.support:cardview-v7:21.0.3'compile 'com.android.support:recyclerview-v7:21.0.3'

Then use it in the XML file.

xmlns:android="https://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/recycler_view"android:layout_centerVertical="true"android:layout_centerHorizontal="true"/>

Then set it in Activity

Public class MainActivity extends ActionBarActivity {@ InjectView (R. id. recycler_view) RecyclerView mRecyclerView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ButterKnife. inject (this); mRecyclerView. setLayoutManager (new LinearLayoutManager (this); // linear display is similar to listview // mRecyclerView. setLayoutManager (new GridLayoutManager (this, 2); // use a linear grid to display a grid view. // mRecyclerView. setLayoutManager (new StaggeredGridLayoutManager (2, OrientationHelper. VERTICAL); // The linear cells are used here to display a waterfall stream similar to the mRecyclerView. setAdapter (new NormalRecyclerViewAdapter (this);} @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. menu_main, menu); return true ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}

Then the adapter code

public class NormalRecyclerViewAdapter extends RecyclerView.Adapter
 
   {private final LayoutInflater mLayoutInflater;private final Context mContext;private String[] mTitles;public NormalRecyclerViewAdapter(Context context) {mTitles = context.getResources().getStringArray(R.array.titles);mContext = context;mLayoutInflater = LayoutInflater.from(context);}@Overridepublic NormalTextViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {return new NormalTextViewHolder(mLayoutInflater.inflate(R.layout.item_text, parent, false));}@Overridepublic void onBindViewHolder(NormalTextViewHolder holder, int position) {holder.mTextView.setText(mTitles[position]);}@Overridepublic int getItemCount() {return mTitles == null ? 0 : mTitles.length;}public static class NormalTextViewHolder extends RecyclerView.ViewHolder {@InjectView(R.id.text_view)TextView mTextView;NormalTextViewHolder(View view) {super(view);ButterKnife.inject(this, view);view.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Log.d("NormalTextViewHolder", "onClick--> position = " + getPosition());}});}}}
 
III,

LinearLayoutManager Style

GridLayoutManager Style

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.