您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)Android中怎么通過自定義RecyclerView控件實(shí)現(xiàn)Gallery效果,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1、RecyclerView的基本用法
首先主Activity的布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.RecyclerView android:id="@+id/id_recyclerview_horizontal" android:layout_width="match_parent" android:layout_height="120dp" android:layout_centerVertical="true" android:background="#FF0000" android:scrollbars="none" /> </RelativeLayout>
Item的布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="120dp" android:layout_height="120dp" android:background="@drawable/item_bg02" > <ImageView android:id="@+id/id_index_gallery_item_image" android:layout_width="80dp" android:layout_height="80dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_margin="5dp" android:scaleType="centerCrop" /> <TextView android:id="@+id/id_index_gallery_item_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/id_index_gallery_item_image" android:layout_centerHorizontal="true" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:textColor="#ff0000" android:text="some info" android:textSize="12dp" /> </RelativeLayout>
數(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<GalleryAdapter.ViewHolder> { private LayoutInflater mInflater; private List<Integer> mDatas; public GalleryAdapter(Context context, List<Integer> 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個(gè)方法:
getItemCount 這個(gè)不用說,獲取總的條目數(shù)
onCreateViewHolder 創(chuàng)建ViewHolder
onBindViewHolder 將數(shù)據(jù)綁定至ViewHolder
可見,RecyclerView對(duì)ViewHolder也進(jìn)行了一定的封裝,但是如果你仔細(xì)觀察,你會(huì)發(fā)出一個(gè)疑問,ListView里面有個(gè)getView返回View為Item的布局,那么這個(gè)Item的樣子在哪控制?
其實(shí)是這樣的,我們創(chuàng)建的ViewHolder必須繼承RecyclerView.ViewHolder,這個(gè)RecyclerView.ViewHolder的構(gòu)造時(shí)必須傳入一個(gè)View,這個(gè)View相當(dāng)于我們ListView getView中的convertView (即:我們需要inflate的item布局需要傳入)。
還有一點(diǎn),ListView中convertView是復(fù)用的,在RecyclerView中,是把ViewHolder作為緩存的單位了,然后convertView作為ViewHolder的成員變量保持在ViewHolder中,也就是說,假設(shè)沒有屏幕顯示10個(gè)條目,則會(huì)創(chuàng)建10個(gè)ViewHolder緩存起來,每次復(fù)用的是ViewHolder,所以他把getView這個(gè)方法變?yōu)榱薿nCreateViewHolder。有興趣的自己打印下Log,測(cè)試下。
最后在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 List<Integer> mDatas; @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<Integer>(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,目前只有一個(gè)實(shí)現(xiàn)類,就是LinearLayoutManager,可以設(shè)置為水平或者垂直。
最后效果圖:
效果很不錯(cuò),這就是RecyclerView的基本用法了,但是你會(huì)發(fā)現(xiàn)一個(gè)坑爹的地方,竟然沒有提供setOnItemClickListener這個(gè)回調(diào),要不要這么坑爹。。。
2、為RecyclerView添加OnItemClickListener回調(diào)
雖然它沒有提供,但是添加個(gè)OnItemClickListener對(duì)我們來說還不是小菜一碟~
我決定在Adapter中添加這個(gè)回調(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<GalleryAdapter.ViewHolder> { /** * 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<Integer> mDatas; public GalleryAdapter(Context context, List<Integer> 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); } }); } } }
很簡(jiǎn)單,創(chuàng)建一個(gè)接口,提供一個(gè)設(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);
好了,這樣就行了,看效果圖:
效果還是不錯(cuò)的,接下來我想改成相冊(cè)效果,即上面顯示一張大圖,下面的RecyclerView做為圖片切換的指示器。
3、自定義RecyclerView實(shí)現(xiàn)滾動(dòng)時(shí)內(nèi)容聯(lián)動(dòng)
首先修改下布局:
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" > <ImageView android:id="@+id/id_content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:layout_margin="10dp" android:scaleType="centerCrop" android:src="@drawable/ic_launcher" /> </FrameLayout> <com.example.zhy_horizontalscrollview03.MyRecyclerView android:id="@+id/id_recyclerview_horizontal" android:layout_width="match_parent" android:layout_height="120dp" android:layout_gravity="bottom" android:background="#FF0000" android:scrollbars="none" /> </LinearLayout>
添加一個(gè)顯示大圖的區(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; /** * 滾動(dòng)時(shí)回調(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); } }
定義了一個(gè)滾動(dòng)時(shí)回調(diào)的接口,然后在onTouchEvent中,監(jiān)聽ACTION_MOVE,用戶手指滑動(dòng)時(shí),不斷把當(dāng)前第一個(gè)View回調(diào)回去~
關(guān)于我咋知道getChildAt(0)和getChildPosition()可用,起初我以為有g(shù)etFirstVisibleItem這個(gè)方法,后來發(fā)現(xiàn)么有;但是發(fā)現(xiàn)了getRecycledViewPool()看名字我覺得是Viewholder那個(gè)緩存隊(duì)列,我想那么直接取這個(gè)隊(duì)列的第一個(gè)不就是我要的View么,后來沒有成功。我就觀察它內(nèi)部的View,最后發(fā)現(xiàn),第一個(gè)顯示的始終是它第一個(gè)child,至于getChildPosition這個(gè)看方法就看出來了。
現(xiàn)在的效果:
和我之前那個(gè)例子的效果是一模一樣的,不過,我還想做一些改變,我覺得Gallery或者說相冊(cè)的指示器,下面可能1000來張圖片,我不僅喜歡手指在屏幕上滑動(dòng)時(shí),圖片會(huì)自動(dòng)切換。我還希望,如果我給指示器一個(gè)加速度,即使手指離開,下面還在滑動(dòng),上面也會(huì)聯(lián)動(dòng) 。而且我還想做些優(yōu)化,直接在ACTION_MOVE中回調(diào),觸發(fā)的頻率太高了,理論上一張圖片只會(huì)觸發(fā)一次~~
4、優(yōu)化與打造真正的Gallery效果
既然希望手指離開還能聯(lián)動(dòng),那么不僅需要ACTION_MOVE需要監(jiān)聽,還得監(jiān)聽一個(gè)加速度,速度到達(dá)一定值,然后繼續(xù)移動(dòng)~~再理一理,需要這么麻煩么,不是能滾動(dòng)么,那么應(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)前第一個(gè)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)時(shí),判斷當(dāng)前第一個(gè)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方法,而是讓這個(gè)類實(shí)現(xiàn)RecyclerView.OnScrollListener接口,然后設(shè)置監(jiān)聽,在onScrolled里面進(jìn)行判斷。
至于優(yōu)化:我使用了一個(gè)成員變化存儲(chǔ)當(dāng)前第一個(gè)View,只有第一個(gè)View發(fā)生變化時(shí)才回調(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 List<Integer> mDatas; 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<Integer>(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)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。