Carousel image record

Source: Internet
Author: User

Carousel image record

 

RecyclerView provides a carousel effect. The adapter has a View cache, which avoids some memory problems.

The first is to use PagerSnapHelper to let the RecyclerView slide only one at a time, and then add an indicator. Here the indicator is dynamically generated and you have made a simple view.

A very simple effect, directly on the code

<?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. addOnScrollLi Stener (new RecyclerView. onScrollListener () {@ Override public void onScrollStateChanged (RecyclerView recyclerView, int newState) {super. onScrollStateChanged (recyclerView, newState); if (newState = RecyclerView. SCROLL_STATE_IDLE & lin_indicate! = Null) {// avoid triggering int lastItemPosition = linearLayoutManager multiple times when the image is not sliding. 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 (Base QuickAdapter 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 number of indicators * @ param position default display subscript */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

It's simple, but it's also worth a record. Maybe it can help me one day. Sometimes I 've encountered a lot of problems but I can't remember it. But when you see some associated code, maybe the memory fragment is coming.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.