這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Android中怎么通過自定義RecyclerView控件實(shí)現(xiàn)Gallery效果,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)是專業(yè)的獨(dú)山子網(wǎng)站建設(shè)公司,獨(dú)山子接單;提供網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行獨(dú)山子網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
1、RecyclerView的基本用法
首先主Activity的布局文件:
Item的布局文件:
數(shù)據(jù)適配器:
package com.example.zhy_horizontalscrollview03; import java.util.List; import android.content.Context; 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; public class GalleryAdapter extends RecyclerView.Adapter{ private LayoutInflater mInflater; private List mDatas; public GalleryAdapter(Context context, List datats) { mInflater = LayoutInflater.from(context); mDatas = datats; } public static class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View arg0) { super(arg0); } ImageView mImg; TextView mTxt; } @Override public int getItemCount() { return mDatas.size(); } /** * 創(chuàng)建ViewHolder */ @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = mInflater.inflate(R.layout.activity_index_gallery_item, viewGroup, false); ViewHolder viewHolder = new ViewHolder(view); viewHolder.mImg = (ImageView) view .findViewById(R.id.id_index_gallery_item_image); return viewHolder; } /** * 設(shè)置值 */ @Override public void onBindViewHolder(final ViewHolder viewHolder, final int i) { viewHolder.mImg.setImageResource(mDatas.get(i)); } }
可以看到數(shù)據(jù)適配器與BaseAdapter比較發(fā)生了相當(dāng)大的變化,主要有3個方法:
getItemCount 這個不用說,獲取總的條目數(shù)
onCreateViewHolder 創(chuàng)建ViewHolder
onBindViewHolder 將數(shù)據(jù)綁定至ViewHolder
可見,RecyclerView對ViewHolder也進(jìn)行了一定的封裝,但是如果你仔細(xì)觀察,你會發(fā)出一個疑問,ListView里面有個getView返回View為Item的布局,那么這個Item的樣子在哪控制?
其實(shí)是這樣的,我們創(chuàng)建的ViewHolder必須繼承RecyclerView.ViewHolder,這個RecyclerView.ViewHolder的構(gòu)造時必須傳入一個View,這個View相當(dāng)于我們ListView getView中的convertView (即:我們需要inflate的item布局需要傳入)。
還有一點(diǎn),ListView中convertView是復(fù)用的,在RecyclerView中,是把ViewHolder作為緩存的單位了,然后convertView作為ViewHolder的成員變量保持在ViewHolder中,也就是說,假設(shè)沒有屏幕顯示10個條目,則會創(chuàng)建10個ViewHolder緩存起來,每次復(fù)用的是ViewHolder,所以他把getView這個方法變?yōu)榱薿nCreateViewHolder。有興趣的自己打印下Log,測試下。
最后在Activity中使用:
package com.example.zhy_horizontalscrollview03; import java.util.ArrayList; import java.util.Arrays; 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 android.view.Window; public class MainActivity extends Activity { private RecyclerView mRecyclerView; private GalleryAdapter mAdapter; private ListmDatas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initDatas(); //得到控件 mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview_horizontal); //設(shè)置布局管理器 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); mRecyclerView.setLayoutManager(linearLayoutManager); //設(shè)置適配器 mAdapter = new GalleryAdapter(this, mDatas); mRecyclerView.setAdapter(mAdapter); } private void initDatas() { mDatas = new ArrayList (Arrays.asList(R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.l)); } }
使用起來也很方便,唯一的區(qū)別就是要設(shè)置LayoutManager,目前只有一個實(shí)現(xiàn)類,就是LinearLayoutManager,可以設(shè)置為水平或者垂直。
最后效果圖:
效果很不錯,這就是RecyclerView的基本用法了,但是你會發(fā)現(xiàn)一個坑爹的地方,竟然沒有提供setOnItemClickListener這個回調(diào),要不要這么坑爹。。。
2、為RecyclerView添加OnItemClickListener回調(diào)
雖然它沒有提供,但是添加個OnItemClickListener對我們來說還不是小菜一碟~
我決定在Adapter中添加這個回調(diào)接口:
package com.example.zhy_horizontalscrollview03; import java.util.List; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; public class GalleryAdapter extends RecyclerView.Adapter{ /** * ItemClick的回調(diào)接口 * @author zhy * */ public interface OnItemClickLitener { void onItemClick(View view, int position); } private OnItemClickLitener mOnItemClickLitener; public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) { this.mOnItemClickLitener = mOnItemClickLitener; } private LayoutInflater mInflater; private List mDatas; public GalleryAdapter(Context context, List datats) { mInflater = LayoutInflater.from(context); mDatas = datats; } public static class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View arg0) { super(arg0); } ImageView mImg; TextView mTxt; } @Override public int getItemCount() { return mDatas.size(); } @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = mInflater.inflate(R.layout.activity_index_gallery_item, viewGroup, false); ViewHolder viewHolder = new ViewHolder(view); viewHolder.mImg = (ImageView) view .findViewById(R.id.id_index_gallery_item_image); return viewHolder; } @Override public void onBindViewHolder(final ViewHolder viewHolder, final int i) { viewHolder.mImg.setImageResource(mDatas.get(i)); //如果設(shè)置了回調(diào),則設(shè)置點(diǎn)擊事件 if (mOnItemClickLitener != null) { viewHolder.itemView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mOnItemClickLitener.onItemClick(viewHolder.itemView, i); } }); } } }
很簡單,創(chuàng)建一個接口,提供一個設(shè)置入口,然后在onBindViewHolder中判斷即可。
最后在主Activity中設(shè)置監(jiān)聽:
mAdapter = new GalleryAdapter(this, mDatas); mAdapter.setOnItemClickLitener(new OnItemClickLitener() { @Override public void onItemClick(View view, int position) { Toast.makeText(MainActivity.this, position+"", Toast.LENGTH_SHORT) .show(); } }); mRecyclerView.setAdapter(mAdapter);
好了,這樣就行了,看效果圖:
效果還是不錯的,接下來我想改成相冊效果,即上面顯示一張大圖,下面的RecyclerView做為圖片切換的指示器。
3、自定義RecyclerView實(shí)現(xiàn)滾動時內(nèi)容聯(lián)動
首先修改下布局:
布局文件:
添加一個顯示大圖的區(qū)域,把RecyclerView改為自己定義的。
然后看我們自定義RecyclerView的代碼:
package com.example.zhy_horizontalscrollview03; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class CopyOfMyRecyclerView extends RecyclerView { public CopyOfMyRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); } private View mCurrentView; /** * 滾動時回調(diào)的接口 */ private OnItemScrollChangeListener mItemScrollChangeListener; public void setOnItemScrollChangeListener( OnItemScrollChangeListener mItemScrollChangeListener) { this.mItemScrollChangeListener = mItemScrollChangeListener; } public interface OnItemScrollChangeListener { void onChange(View view, int position); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); mCurrentView = getChildAt(0); if (mItemScrollChangeListener != null) { mItemScrollChangeListener.onChange(mCurrentView, getChildPosition(mCurrentView)); } } @Override public boolean onTouchEvent(MotionEvent e) { if (e.getAction() == MotionEvent.ACTION_MOVE) { mCurrentView = getChildAt(0); // Log.e("TAG", getChildPosition(getChildAt(0)) + ""); if (mItemScrollChangeListener != null) { mItemScrollChangeListener.onChange(mCurrentView, getChildPosition(mCurrentView)); } } return super.onTouchEvent(e); } }
定義了一個滾動時回調(diào)的接口,然后在onTouchEvent中,監(jiān)聽ACTION_MOVE,用戶手指滑動時,不斷把當(dāng)前第一個View回調(diào)回去~
關(guān)于我咋知道getChildAt(0)和getChildPosition()可用,起初我以為有g(shù)etFirstVisibleItem這個方法,后來發(fā)現(xiàn)么有;但是發(fā)現(xiàn)了getRecycledViewPool()看名字我覺得是Viewholder那個緩存隊(duì)列,我想那么直接取這個隊(duì)列的第一個不就是我要的View么,后來沒有成功。我就觀察它內(nèi)部的View,最后發(fā)現(xiàn),第一個顯示的始終是它第一個child,至于getChildPosition這個看方法就看出來了。
現(xiàn)在的效果:
和我之前那個例子的效果是一模一樣的,不過,我還想做一些改變,我覺得Gallery或者說相冊的指示器,下面可能1000來張圖片,我不僅喜歡手指在屏幕上滑動時,圖片會自動切換。我還希望,如果我給指示器一個加速度,即使手指離開,下面還在滑動,上面也會聯(lián)動 。而且我還想做些優(yōu)化,直接在ACTION_MOVE中回調(diào),觸發(fā)的頻率太高了,理論上一張圖片只會觸發(fā)一次~~
4、優(yōu)化與打造真正的Gallery效果
既然希望手指離開還能聯(lián)動,那么不僅需要ACTION_MOVE需要監(jiān)聽,還得監(jiān)聽一個加速度,速度到達(dá)一定值,然后繼續(xù)移動~~再理一理,需要這么麻煩么,不是能滾動么,那么應(yīng)該有OnScrollListener啊,小看一把,果然有,哈哈哈~天助我也,下面看修改后的代碼:
package com.example.zhy_horizontalscrollview03; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView.OnScrollListener; import android.util.AttributeSet; import android.view.View; public class MyRecyclerView extends RecyclerView implements OnScrollListener { /** * 記錄當(dāng)前第一個View */ private View mCurrentView; private OnItemScrollChangeListener mItemScrollChangeListener; public void setOnItemScrollChangeListener( OnItemScrollChangeListener mItemScrollChangeListener) { this.mItemScrollChangeListener = mItemScrollChangeListener; } public interface OnItemScrollChangeListener { void onChange(View view, int position); } public MyRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub this.setOnScrollListener(this); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); mCurrentView = getChildAt(0); if (mItemScrollChangeListener != null) { mItemScrollChangeListener.onChange(mCurrentView, getChildPosition(mCurrentView)); } } @Override public void onScrollStateChanged(int arg0) { } /** * * 滾動時,判斷當(dāng)前第一個View是否發(fā)生變化,發(fā)生才回調(diào) */ @Override public void onScrolled(int arg0, int arg1) { View newView = getChildAt(0); if (mItemScrollChangeListener != null) { if (newView != null && newView != mCurrentView) { mCurrentView = newView ; mItemScrollChangeListener.onChange(mCurrentView, getChildPosition(mCurrentView)); } } } }
我放棄了重寫onTouchEvent方法,而是讓這個類實(shí)現(xiàn)RecyclerView.OnScrollListener接口,然后設(shè)置監(jiān)聽,在onScrolled里面進(jìn)行判斷。
至于優(yōu)化:我使用了一個成員變化存儲當(dāng)前第一個View,只有第一個View發(fā)生變化時才回調(diào)~~太完美了~
看MainActivity:
package com.example.zhy_horizontalscrollview03; import java.util.ArrayList; import java.util.Arrays; 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 android.view.View; import android.view.Window; import android.widget.ImageView; import android.widget.Toast; import com.example.zhy_horizontalscrollview03.GalleryAdapter.OnItemClickLitener; import com.example.zhy_horizontalscrollview03.MyRecyclerView.OnItemScrollChangeListener; public class MainActivity extends Activity { private MyRecyclerView mRecyclerView; private GalleryAdapter mAdapter; private ListmDatas; private ImageView mImg ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mImg = (ImageView) findViewById(R.id.id_content); mDatas = new ArrayList (Arrays.asList(R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.l)); mRecyclerView = (MyRecyclerView) findViewById(R.id.id_recyclerview_horizontal); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); mRecyclerView.setLayoutManager(linearLayoutManager); mAdapter = new GalleryAdapter(this, mDatas); mRecyclerView.setAdapter(mAdapter); mRecyclerView.setOnItemScrollChangeListener(new OnItemScrollChangeListener() { @Override public void onChange(View view, int position) { mImg.setImageResource(mDatas.get(position)); }; }); mAdapter.setOnItemClickLitener(new OnItemClickLitener() { @Override public void onItemClick(View view, int position) { // Toast.makeText(getApplicationContext(), position + "", Toast.LENGTH_SHORT) // .show(); mImg.setImageResource(mDatas.get(position)); } }); } }
上述就是小編為大家分享的Android中怎么通過自定義RecyclerView控件實(shí)現(xiàn)Gallery效果了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。