您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關Android中怎么通過自定義RecyclerView控件實現Gallery效果,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
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>
數據適配器:
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(); } /** * 創建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; } /** * 設置值 */ @Override public void onBindViewHolder(final ViewHolder viewHolder, final int i) { viewHolder.mImg.setImageResource(mDatas.get(i)); } }
可以看到數據適配器與BaseAdapter比較發生了相當大的變化,主要有3個方法:
getItemCount 這個不用說,獲取總的條目數
onCreateViewHolder 創建ViewHolder
onBindViewHolder 將數據綁定至ViewHolder
可見,RecyclerView對ViewHolder也進行了一定的封裝,但是如果你仔細觀察,你會發出一個疑問,ListView里面有個getView返回View為Item的布局,那么這個Item的樣子在哪控制?
其實是這樣的,我們創建的ViewHolder必須繼承RecyclerView.ViewHolder,這個RecyclerView.ViewHolder的構造時必須傳入一個View,這個View相當于我們ListView getView中的convertView (即:我們需要inflate的item布局需要傳入)。
還有一點,ListView中convertView是復用的,在RecyclerView中,是把ViewHolder作為緩存的單位了,然后convertView作為ViewHolder的成員變量保持在ViewHolder中,也就是說,假設沒有屏幕顯示10個條目,則會創建10個ViewHolder緩存起來,每次復用的是ViewHolder,所以他把getView這個方法變為了onCreateViewHolder。有興趣的自己打印下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 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); //設置布局管理器 LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); mRecyclerView.setLayoutManager(linearLayoutManager); //設置適配器 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)); } }
使用起來也很方便,唯一的區別就是要設置LayoutManager,目前只有一個實現類,就是LinearLayoutManager,可以設置為水平或者垂直。
最后效果圖:
效果很不錯,這就是RecyclerView的基本用法了,但是你會發現一個坑爹的地方,竟然沒有提供setOnItemClickListener這個回調,要不要這么坑爹。。。
2、為RecyclerView添加OnItemClickListener回調
雖然它沒有提供,但是添加個OnItemClickListener對我們來說還不是小菜一碟~
我決定在Adapter中添加這個回調接口:
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的回調接口 * @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)); //如果設置了回調,則設置點擊事件 if (mOnItemClickLitener != null) { viewHolder.itemView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mOnItemClickLitener.onItemClick(viewHolder.itemView, i); } }); } } }
很簡單,創建一個接口,提供一個設置入口,然后在onBindViewHolder中判斷即可。
最后在主Activity中設置監聽:
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實現滾動時內容聯動
首先修改下布局:
布局文件:
<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>
添加一個顯示大圖的區域,把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; /** * 滾動時回調的接口 */ 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); } }
定義了一個滾動時回調的接口,然后在onTouchEvent中,監聽ACTION_MOVE,用戶手指滑動時,不斷把當前第一個View回調回去~
關于我咋知道getChildAt(0)和getChildPosition()可用,起初我以為有getFirstVisibleItem這個方法,后來發現么有;但是發現了getRecycledViewPool()看名字我覺得是Viewholder那個緩存隊列,我想那么直接取這個隊列的第一個不就是我要的View么,后來沒有成功。我就觀察它內部的View,最后發現,第一個顯示的始終是它第一個child,至于getChildPosition這個看方法就看出來了。
現在的效果:
和我之前那個例子的效果是一模一樣的,不過,我還想做一些改變,我覺得Gallery或者說相冊的指示器,下面可能1000來張圖片,我不僅喜歡手指在屏幕上滑動時,圖片會自動切換。我還希望,如果我給指示器一個加速度,即使手指離開,下面還在滑動,上面也會聯動 。而且我還想做些優化,直接在ACTION_MOVE中回調,觸發的頻率太高了,理論上一張圖片只會觸發一次~~
4、優化與打造真正的Gallery效果
既然希望手指離開還能聯動,那么不僅需要ACTION_MOVE需要監聽,還得監聽一個加速度,速度到達一定值,然后繼續移動~~再理一理,需要這么麻煩么,不是能滾動么,那么應該有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 { /** * 記錄當前第一個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) { } /** * * 滾動時,判斷當前第一個View是否發生變化,發生才回調 */ @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方法,而是讓這個類實現RecyclerView.OnScrollListener接口,然后設置監聽,在onScrolled里面進行判斷。
至于優化:我使用了一個成員變化存儲當前第一個View,只有第一個View發生變化時才回調~~太完美了~
看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控件實現Gallery效果了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。