RecyclerView做的一個輪播效果,適配器有視圖緩存,避免了一些記憶體問題 首先是藉助 PagerSnapHelper 讓RecyclerView每次只滑動一個,然後添加一個指示器,這裡指示器是動態生成的,自己做了個簡單的view 很簡單的一個效果,直接上代碼 public class Imag ...
RecyclerView做的一個輪播效果,適配器有視圖緩存,避免了一些記憶體問題
首先是藉助 PagerSnapHelper 讓RecyclerView每次只滑動一個,然後添加一個指示器,這裡指示器是動態生成的,自己做了個簡單的view
很簡單的一個效果,直接上代碼
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/bg_indicator_point_selected" android:gravity="center" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" /> <com.hanmaker.bryan.hc.widget.LinIndicate android:id="@+id/lin_indicate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:gravity="center" android:orientation="horizontal"/> </FrameLayout>
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/d20.0"> <com.hanmaker.bryan.hc.widget.MyPhotoView android:id="@+id/img_icon" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitCenter" android:layout_gravity="center" /> </FrameLayout>
public class ImageListAdapter extends BaseQuickAdapter<String,BaseViewHolder> { private CallBack callBack; public ImageListAdapter(int layoutResId,CallBack callBack) { super(layoutResId); this.callBack = callBack; } @Override protected void convert(BaseViewHolder helper, String item) { if (item != null && !item.isEmpty()){ MyPhotoView img =helper.getView(R.id.img_icon); GlideApp.with(AndroidApplication.getInstance().getApplicationContext()) .load(item) .skipMemoryCache(true) .fitCenter() .into(img); img.setOnPhotoTapListener((view, x, y) -> callBack.click()); } } public interface CallBack{ void click(); } }View Code
@Override protected void initData() { Bundle bundle = getArguments(); ArrayList<String> list = bundle.getStringArrayList("Photo"); int position = bundle.getInt("Photo_p", 0); adapter = new ImageListAdapter(R.layout.item_imagelist, new ImageListAdapter.CallBack() { @Override public void click() { if (getActivity() != null) { getActivity().finish(); } } }); adapter.addData(list); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false); recyclerView.setLayoutManager(linearLayoutManager); linearLayoutManager.scrollToPositionWithOffset(position, 0); linearLayoutManager.setStackFromEnd(true); recyclerView.setAdapter(adapter); recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); if (newState == RecyclerView.SCROLL_STATE_IDLE && lin_indicate != null) {//不滑動的時候,避免多次觸發 int lastItemPosition = linearLayoutManager.findLastCompletelyVisibleItemPosition(); lin_indicate.setIndicatePosition(lastItemPosition); } } }); new PagerSnapHelper().attachToRecyclerView(recyclerView); int count = list.size(); lin_indicate.load(count,position); adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() { @Override public void onItemClick(BaseQuickAdapter adapter, View view, int position) { if (getActivity() != null) { getActivity().finish(); } } }); }View Code
public class LinIndicate extends LinearLayout { public LinIndicate(Context context) { super(context); } public LinIndicate(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public LinIndicate(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public void load(int count){ removeAllViews(); for (int i = 0; i < count; i++) { ImageView imageView = new ImageView(getContext()); ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); imageView.setLayoutParams(params); imageView.setPadding(10,10,10,20); addView(imageView); } } /** * @param count 指示器數量 * @param position 預設顯示下標 */ public void load(int count,int position){ removeAllViews(); for (int i = 0; i < count; i++) { ImageView imageView = new ImageView(getContext()); ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); imageView.setLayoutParams(params); imageView.setPadding(10,10,10,20); addView(imageView); } setIndicatePosition(position); } public void setIndicatePosition(int position){ int count = getChildCount(); if (position > count) return; for (int i = 0; i < count; i++) { ImageView imageView = (ImageView) getChildAt(i); if (position == i){ imageView.setImageResource(R.mipmap.ic_page_indicator_focused); }else{ imageView.setImageResource(R.mipmap.ic_page_indicator); } } } }View Code
雖然簡單,但是也記錄一下,說不定哪天就能幫上忙呢,有的時候遇到很多問題解決過但是想不起來,可是當你看到相關聯的一些代碼,或許記憶片段就來了