RecyclerView的使用之HelloWorld
話說RecyclerView已經(jīng)面市很久,也在很多應(yīng)用中得到廣泛的使用,在整個(gè)開發(fā)者圈子里面也擁有很不錯(cuò)的口碑,那說明RecyclerView擁有比ListView,GridView之類控件有很多的優(yōu)點(diǎn),例如:數(shù)據(jù)綁定,Item View創(chuàng)建,View的回收以及重用等機(jī)制。
RecyclerView是伴隨Android 5.0發(fā)布的新控件,是一種列表容器,Google意在用新的RecyclerView來取代老舊的ListView和GridView,它的使用靈活性和性能都要優(yōu)于ListView,接下來通過一系列文章來了解RecyclerView的各種使用方法,本篇來介紹它的初步使用,RecyclerView的“HelloWord“。
RecyclerView與傳統(tǒng)ListView的區(qū)別:
1:ViewHolder模式,傳統(tǒng)的ListView可以通過ViewHolder來提升列表滾動(dòng)的性能,但是這不是必須的,因?yàn)長(zhǎng)istView沒有嚴(yán)格標(biāo)準(zhǔn)的設(shè)計(jì)模式,但是在使用RecyclerView的時(shí)候Adapter必須實(shí)現(xiàn)至少一個(gè)ViewHolder,因?yàn)樗兄鴩?yán)格的ViewHolder設(shè)計(jì)模式。
2:顯示效果,ListView只能實(shí)現(xiàn)垂直的滾動(dòng)列表視圖,相反,RecyclerView可以通過設(shè)置RecyclerView.LayoutManager來定制不同風(fēng)格的視圖,比如水平滾動(dòng)列表或者不規(guī)則的瀑布流列表。
3:列表項(xiàng)動(dòng)畫, 在ListView中沒有提供任何方法或接口,方便開發(fā)者實(shí)現(xiàn)Item的增刪動(dòng)畫。RecyclerView可以通過設(shè)置的RecyclerView.ItemAnimator來為條目增加動(dòng)畫效果。
本文以實(shí)現(xiàn)下面這個(gè)小Demo來了解RecyclerView的初步使用
*圖片素材版權(quán)歸屬于Smartisan.com
1:依賴庫(kù) (本文以Android Studio作為開發(fā)工具)
Gradle配置
compile 'com.android.support:recyclerview-v7:23.1.1'
2:建立Layout,與ListView類似,在布局里面添加RecyclerView
<?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.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/rv_list" /> </LinearLayout>
3:建立RecyclerView Item布局
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" android:id="@+id/cv_item" android:foreground="?android:attr/selectableItemBackground" card_view:cardCornerRadius="4dp" card_view:cardBackgroundColor="#795548" card_view:cardElevation="4dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/iv_pic" android:layout_width="match_parent" android:layout_height="200dp" android:layout_weight="1" /> <TextView android:id="@+id/tv_text" android:padding="20dp" android:textColor="#ffffff" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> </android.support.v7.widget.CardView>
這里使用了一個(gè)叫CardView的控件,繼承自FrameLayout,它是Google提供的一個(gè)卡片式視圖容器,可以很方便的顯示出具有陰影和圓角的卡片式布局,像這樣
CardView跟RecyclerView一樣使用前也需要進(jìn)行導(dǎo)入
compile 'com.android.support:cardview-v7:23.1.1'
4:然后建立RecyclerView的Adapter
import android.content.Context; import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; /** * Created by Lijizhou on 2016/2/3. */ public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.NormalViewHolder> { private LayoutInflater mLayoutInflater; private Context mContext; private String [] mTitle; private int [] mPic; public RecyclerViewAdapter(Context context,String[]title,int[] pic){ mContext=context; mTitle=title; mPic=pic; mLayoutInflater=LayoutInflater.from(context); } //自定義的ViewHolder,持有每個(gè)Item的的所有界面元素 public static class NormalViewHolder extends RecyclerView.ViewHolder{ TextView mTextView; CardView mCardView; ImageView mImageView; public NormalViewHolder(View itemView) { super(itemView); mTextView=(TextView)itemView.findViewById(R.id.tv_text); mCardView=(CardView)itemView.findViewById(R.id.cv_item); mImageView=(ImageView)itemView.findViewById(R.id.iv_pic); } } //在該方法中我們創(chuàng)建一個(gè)ViewHolder并返回,ViewHolder必須有一個(gè)帶有View的構(gòu)造函數(shù),這個(gè)View就是我們Item的根布局,在這里我們使用自定義Item的布局; @Override public NormalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return new NormalViewHolder(mLayoutInflater.inflate(R.layout.item_view,parent,false)); } //將數(shù)據(jù)與界面進(jìn)行綁定的操作 @Override public void onBindViewHolder(NormalViewHolder holder, final int position) { holder.mTextView.setText(mTitle[position]); holder.mImageView.setBackgroundResource(mPic[position]); holder.mCardView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(mContext,mTitle[position],3000).show(); } }); } //獲取數(shù)據(jù)的數(shù)量 @Override public int getItemCount() { return mTitle==null ? 0 : mTitle.length; } }
5:RecycleViewActivity.java
public class RecycleViewActivity extends AppCompatActivity { private RecyclerView mRecyclerView; //item 顯示所需 private String[] title = {"Blog : http://blog.csdn.net/Leejizhou.", "A good laugh and a long sleep are the best cures in the doctor's book.", "all or nothing, now or never ", "Be nice to people on the way up, because you'll need them on your way down.", "Be confident with yourself and stop worrying what other people think. Do what's best for your future happiness!", "Blessed is he whose fame does not outshine his truth.", "Create good memories today, so that you can have a good past" }; /** * 圖片資源版權(quán)歸屬于Smartisan.com */ private int[] pic = {R.mipmap.aa1, R.mipmap.aa0, R.mipmap.aa2, R.mipmap.aa3, R.mipmap.aa4, R.mipmap.aa5, R.mipmap.aa6}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycle); mRecyclerView = (RecyclerView) findViewById(R.id.rv_list); // 創(chuàng)建一個(gè)線性布局管理器 LinearLayoutManager layoutManager = new LinearLayoutManager(this); //設(shè)置垂直滾動(dòng),也可以設(shè)置橫向滾動(dòng) layoutManager.setOrientation(LinearLayoutManager.VERTICAL); //另外兩種顯示模式 // mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2)); Grid視圖 // mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL)); 這里用線性宮格顯示 類似于瀑布流 //RecyclerView設(shè)置布局管理器 mRecyclerView.setLayoutManager(layoutManager); //RecyclerView設(shè)置Adapter mRecyclerView.setAdapter(new RecyclerViewAdapter(this, title, pic)); } }
- RecyclerView的使用之多種Item加載布局
- Android中使用RecyclerView實(shí)現(xiàn)下拉刷新和上拉加載
- 使用RecyclerView添加Header和Footer的方法
- Android項(xiàng)目實(shí)戰(zhàn)之仿網(wǎng)易新聞的頁面(RecyclerView )
- 學(xué)習(xí)Android Material Design(RecyclerView代替ListView)
- Android使用RecyclerView實(shí)現(xiàn)自定義列表、點(diǎn)擊事件以及下拉刷新
- Android應(yīng)用開發(fā)中RecyclerView組件使用入門教程
- Android RecyclerView添加頭部和底部的方法
- Android App開發(fā)中使用RecyclerView實(shí)現(xiàn)Gallery畫廊的實(shí)例
- Android RecyclerView實(shí)現(xiàn)下拉刷新和上拉加載
- Android中RecyclerView布局代替GridView實(shí)現(xiàn)類似支付寶的界面
相關(guān)文章
Android開發(fā)之時(shí)間日期組件用法實(shí)例
這篇文章主要介紹了Android開發(fā)之時(shí)間日期組件用法,主要介紹了TimePicker和DatePicker組件,對(duì)于Android程序開發(fā)有不錯(cuò)的借鑒價(jià)值,需要的朋友可以參考下2014-08-08關(guān)于Android Activity之間跳轉(zhuǎn)問題(Intent)
這篇文章主要介紹了Android Activity之間跳轉(zhuǎn)Intent,當(dāng)一個(gè)Acitivity需要啟動(dòng)另一個(gè)Activity時(shí),通過Intent來表達(dá)自己的意圖,告知系統(tǒng)啟動(dòng)哪個(gè)Activity,本文給大家詳細(xì)講解,需要的朋友可以參考下2022-10-10Android PickerView滾動(dòng)選擇器的使用方法
這篇文章主要為大家詳細(xì)介紹了Android PickerView滾動(dòng)選擇器的使用方法,感興趣的小伙伴們可以參考一下2016-03-03Android消息通知欄的實(shí)現(xiàn)方法介紹
本篇文章是對(duì)Android消息通知欄的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android openGl 繪制簡(jiǎn)單圖形的實(shí)現(xiàn)示例
這篇文章主要介紹了Android openGl 繪制簡(jiǎn)單圖形的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Android中Fragment的基本用法示例總結(jié)
Fragment是activity的界面中的一部分或一種行為,下面這篇文章主要給大家介紹了關(guān)于Android中Fragment的基本用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-05-05基于popupWindow實(shí)現(xiàn)懸浮半透明效果
這篇文章主要為大家詳細(xì)介紹了基于popupWindow實(shí)現(xiàn)懸浮半透明效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04解決Android studio中關(guān)于模擬器的/data目錄不能顯示的問題
這篇文章主要介紹了解決Android studio中關(guān)于模擬器的/data目錄不能顯示的問題,主要原因還是我們權(quán)限不夠,當(dāng)前的用戶沒有權(quán)限訪問data目錄。具體解決方法大家跟隨腳本之家小編一起看看吧2018-06-06實(shí)例講解Android中SQLiteDatabase使用方法
這篇文章主要以一個(gè)簡(jiǎn)單的實(shí)例為大家詳細(xì)講解Android中SQLiteDatabase使用方法,感興趣的小伙伴們可以參考一下2016-05-05詳解Android實(shí)現(xiàn)購(gòu)物車頁面及購(gòu)物車效果(點(diǎn)擊動(dòng)畫)
本篇文章主要介紹了詳解Android實(shí)現(xiàn)購(gòu)物車頁面及購(gòu)物車效果(點(diǎn)擊動(dòng)畫),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-08-08