美文网首页Android安卓view
Android RecyclerView +SnapHelper

Android RecyclerView +SnapHelper

作者: 小于先森 | 来源:发表于2018-06-01 17:05 被阅读469次

    效果图

    这里写图片描述

    此效果已被产品砍掉,所以有些适配bug就不修改了 此博客只为记录下代码
    默哀3秒
    1秒
    2秒
    3秒

    代码

    • 布局文件
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:padding="15dp"
        android:layout_width="85dip"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/bg_circle_5_blue"
        >
        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/img_face"
            android:layout_width="55dip"
            android:layout_height="55dip"
            android:src="@drawable/default_img"
            />
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:layout_marginTop="10dp"
            android:textSize="@dimen/px_28"
            android:text="@string/default_string"
            />
    </LinearLayout>
    
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        >
        <corners
            android:radius="5dp"
            ></corners>
        <stroke
            android:width="1dp"
            android:color="@color/most_line"
            ></stroke>
        <solid
            android:color="@color/white"
            ></solid>
    </shape>
    
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        >
        <corners
            android:radius="5dp"
            ></corners>
        <stroke
            android:width="1dp"
            android:color="@color/most_line"
            ></stroke>
        <solid
            android:color="@color/app_color_theme"
            ></solid>
    </shape>
    
    • Adapter
    package inter.baisong.adapter;
    
    import android.content.Context;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import com.uuzuche.lib_zxing.DisplayUtil;
    
    import inter.baisong.R;
    import inter.baisong.base.ConfigYibaisong;
    
    /**
     * Created by 于德海 on 2018/6/1.
     * package inter.baisong.adapter
     * email : yudehai0204@163.com
     *
     * @describe
     */
    
    public class UnionUnionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private Context mContext;
        private final int PLACEHOLDER_CODE =-1;
        private int placeholder_width;//布局写死85dip +20 dip margin
        private int center_index,last_index;
        public UnionUnionAdapter(Context mContext) {
            this.mContext = mContext;
            placeholder_width = DisplayUtil.dip2px(mContext,105);
            center_index=1;
            last_index=1;
        }
    
        /***
         * 正常一屏显示的个数
         * @return
         */
        public int getMeaSureItemCount(){
            if(ConfigYibaisong.window_x%placeholder_width>0){
                return ConfigYibaisong.window_x/placeholder_width+1;
            }else {
                return ConfigYibaisong.window_x/placeholder_width+1;
            }
        }
        /***
         * 设置中心
         * @param index
         */
        public void setCenter_index(int index){
            if(index==center_index)
                return;
            last_index = center_index;
            center_index = index;
            notifyItemChanged(last_index);
            notifyItemChanged(center_index);
        }
    
    
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if(viewType==PLACEHOLDER_CODE){
                View view = new View(mContext);
                int view_width = parent.getMeasuredWidth()/2-placeholder_width/2;
                RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(view_width,RecyclerView.LayoutParams.WRAP_CONTENT);
                view.setLayoutParams(params);
                PlaceHolderViewHolder holder = new PlaceHolderViewHolder(view);
                return holder;
            }
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_frag_union_union,parent,false);
            UnionViewHolder viewHolder = new UnionViewHolder(view);
            return viewHolder;
        }
    
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if(holder instanceof UnionViewHolder){
                if(position == center_index){
                    holder.itemView.setBackgroundResource(R.drawable.bg_union_item_center);
                }else {
                    holder.itemView.setBackgroundResource(R.drawable.bg_union_item_default);
                }
            }
        }
    
        @Override
        public int getItemCount() {
            return 20+2;
        }
    
        @Override
        public int getItemViewType(int position) {
            if(position==0||position== getItemCount()-1){
                return PLACEHOLDER_CODE;
            }
            return super.getItemViewType(position);
        }
    
        class UnionViewHolder extends RecyclerView.ViewHolder{
    
            public UnionViewHolder(View itemView) {
                super(itemView);
            }
        }
    
        class PlaceHolderViewHolder extends RecyclerView.ViewHolder{
    
            public PlaceHolderViewHolder(View itemView) {
                super(itemView);
            }
        }
    
    }
    
    
    • Fragment
    package inter.baisong.fragment;
    
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.support.v4.view.ViewPager;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.LinearSnapHelper;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.LinearLayout;
    
    import com.lzy.okgo.utils.OkLogger;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import inter.baisong.R;
    import inter.baisong.adapter.HomeViewPagerAdapter;
    import inter.baisong.adapter.UnionExperienceAdapter;
    import inter.baisong.adapter.UnionRedPacketAdapter;
    import inter.baisong.adapter.UnionUnionAdapter;
    import inter.baisong.bean.BannerBean;
    import inter.baisong.widgets.CoustomSwipRefresh;
    
    /**
     * Created by 于德海 on 2018/5/31.
     * package inter.baisong.fragment
     * email : yudehai0204@163.com
     *
     * @describe
     */
    
    public class UnionFragment extends Fragment {
        private View view;
        private CoustomSwipRefresh mSwipe;
        private RecyclerView mRecyclerView,mRecyclerView_redpecket,mRecyclerView_experience;
        private ViewPager mViewPager;
        private UnionUnionAdapter mUinonAdapter;
        private HomeViewPagerAdapter mBannerAdapter;
        private LinearLayout ll_hotsale,ll_tower;
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            if(view==null){
                view = inflater.inflate(R.layout.frag_union,container,false);
                mSwipe = view.findViewById(R.id.mSwipe);
                mViewPager = view.findViewById(R.id.mViewPager);
                mRecyclerView = view.findViewById(R.id.mRecyclerView);
                mRecyclerView_redpecket = view.findViewById(R.id.mRecyclerView_redpecket);
                mRecyclerView_experience = view.findViewById(R.id.mRecyclerView_experience);
                ll_hotsale = view.findViewById(R.id.ll_hotsale);
                ll_tower = view.findViewById(R.id.ll_tower);
                mUinonAdapter = new UnionUnionAdapter(getActivity());
                mBannerAdapter = new HomeViewPagerAdapter(getActivity());
    
            }
            initData();
            initListener();
            return view;
        }
    
        private void initData() {
            /***
             * 假数据 banner
             */
            mViewPager.setAdapter(mBannerAdapter);
            List<BannerBean.Data> banner_list =new ArrayList<>();
            for (int i=0;i<3;i++){
                BannerBean.Data data = new BannerBean().new Data();
                if(i==0){
                    data.setPicpatch("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2407350602,1649604847&fm=27&gp=0.jpg");
                }
                if(i==1){
                    data.setPicpatch("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=204321001,3815511924&fm=27&gp=0.jpg");
                }
                if(i==2){
                    data.setPicpatch("https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3821526675,3796993096&fm=27&gp=0.jpg");
                }
                banner_list.add(data);
            }
            mBannerAdapter.setmList(banner_list);
            mBannerAdapter.notifyDataSetChanged();
            //公会RecyclerView
            mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
            LinearSnapHelper mLinearSnapHelper = new LinearSnapHelper();
            mLinearSnapHelper.attachToRecyclerView(mRecyclerView);
            mRecyclerView.setAdapter(mUinonAdapter);
            //红包
            mRecyclerView_redpecket.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
            mRecyclerView_redpecket.setAdapter(new UnionRedPacketAdapter(getActivity()));
            //体验
            mRecyclerView_experience.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
            mRecyclerView_experience.setAdapter(new UnionExperienceAdapter(getActivity()));
            //热卖
            ll_hotsale.removeAllViews();
            for (int i=0;i<3;i++){
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_frag_union_hotsale,ll_hotsale,false);
                ll_hotsale.addView(view);
            }
            //任务
            ll_tower.removeAllViews();
            for (int i=0;i<3;i++){
                View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_frag_union_tower,ll_tower,false);
                ll_tower.addView(view);
            }
        }
        private void initListener() {
    
            mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                int itemCount_page = mUinonAdapter.getMeaSureItemCount();
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if(newState==RecyclerView.SCROLL_STATE_IDLE){
                        RecyclerView.LayoutManager manager = mRecyclerView.getLayoutManager();
                        if(manager instanceof LinearLayoutManager){
                            LinearLayoutManager linearManager = (LinearLayoutManager) manager;
                            //获取最后一个可见view的位置
                            int lastItemPosition = linearManager.findLastVisibleItemPosition();
                            //获取第一个可见view的位置
                            int firstItemPosition = linearManager.findFirstVisibleItemPosition();
                            if(lastItemPosition-firstItemPosition<itemCount_page){
                                if(firstItemPosition==0){
                                    mUinonAdapter.setCenter_index(1);
                                }else {
                                    mUinonAdapter.setCenter_index(mUinonAdapter.getItemCount()-2);
                                }
                            }else {
                                mUinonAdapter.setCenter_index(firstItemPosition+itemCount_page/2);
                            }
                            OkLogger.e("union",firstItemPosition+"  ======  "+lastItemPosition+" ==== "+itemCount_page);
                        }
                    }
                }
            });
        }
    }
    
    

    尾语

    UI效果永远最后写 要不极有可能等于白费功夫。。。

    2018.6.1

    相关文章

      网友评论

        本文标题:Android RecyclerView +SnapHelper

        本文链接:https://www.haomeiwen.com/subject/jxoglftx.html