輪播圖記錄篇,輪播圖

來源:互聯網
上載者:User

輪播圖記錄篇,輪播圖

 

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

雖然簡單,但是也記錄一下,說不定哪天就能幫上忙呢,有的時候遇到很多問題解決過但是想不起來,可是當你看到相關聯的一些代碼,或許記憶片段就來了

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.