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(); } }
@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(); } } }); }
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); } } } }
虽然简单,但是也记录一下,说不定哪天就能帮上忙呢,有的时候遇到很多问题解决过但是想不起来,可是当你看到相关联的一些代码,或许记忆片段就来了