RecyclerView做的一个轮播效果,适配器有视图缓存,避免了一些内存问题
首先是借助 PagerSnapHelper 让RecyclerView每次只滑动一个,然后添加一个指示器,这里指示器是动态生成的,自己做了个简单的view
很简单的一个效果,直接上代码
public class ImageListAdapter extends BaseQuickAdapter{ 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(); ArrayListlist = 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); } } }}
虽然简单,但是也记录一下,说不定哪天就能帮上忙呢,有的时候遇到很多问题解决过但是想不起来,可是当你看到相关联的一些代码,或许记忆片段就来了