仿京东分类Recycleview实现两边联动

仿京东分类Recycleview实现两边联动

今天是个好日子  2020-8-18  好久没写博客  该出来练练了  话不多说 上代码

先看看效果

仿京东分类Recycleview实现两边联动_第1张图片

首先上布局

这是主布局






    

    




    


    

    

仿京东分类Recycleview实现两边联动_第2张图片

第一个adapter布局





    

        

        
    




仿京东分类Recycleview实现两边联动_第3张图片

第二个子布局




    
        
        

    
    

        

        

    


    

仿京东分类Recycleview实现两边联动_第4张图片

两个recycleview就OK了

然后请求数据获取到数据分别加入两个adapter

这是主界面代码

/**
 *会员权益
 */
public class MembershipFragment extends BaseMainFragmeng {


    private RecyclerView rcy_title;
    private RecyclerView rcy_icon;
    private MembershipAdapter1 adapter1;
    private MembershipAdapter2 adapter2;
    private  LinearLayoutManager manager;
    /**
     * 是否来自点击
     */
    private boolean mIsFromClick = false;


    public static MembershipFragment newInstance() {
        MembershipFragment fragment = new MembershipFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_membership, container, false);
        initView(view);
        getData();
        getStudyRank();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        return view;
    }

    private void getStudyRank() {


    }


    private void getData() {
        getVipRightship(this)
                .subscribe(new DataObserver>(false) {
                    @Override
                    protected void onDataListEmpty() {

                        onActivityFailure();
                    }

                    @Override
                    protected void onDataNull() {
                        onActivityFailure();

                    }

                    @Override
                    protected void onError(String errorMsg, String errCode) {
                        onActivityFailure();

                    }

                    @Override
                    protected void onSuccess(List data) {

                        onGetDoorGodCategory(data);
                    }
                });

    }


    private void onGetDoorGodCategory(List data) {

        adapter1.setData(data);
        adapter2.setData(data);

    }

    private void onActivityFailure() {

    }




    private void getVipRightsData(VipBean data) {


    }




    private void initView(View view) {


        Bundle arguments = getArguments();
        if (arguments != null) {
        }

        rcy_title = view.findViewById(R.id.rcy_title);
        rcy_icon=view.findViewById(R.id.rcy_icon);
        LinearLayoutManager layoutManager1=new LinearLayoutManager(getActivity());
        adapter1=new MembershipAdapter1(getActivity());
        rcy_title.setLayoutManager(layoutManager1);
        rcy_title.setAdapter(adapter1);



        adapter2=new MembershipAdapter2(getActivity());
//        LRecyclerViewAdapter mLRecyclerViewAdapter = new LRecyclerViewAdapter(adapter2);
        rcy_icon.setAdapter(adapter2);
         manager = new LinearLayoutManager(getActivity());
        rcy_icon.setLayoutManager(manager);
//        View header = LayoutInflater.from(getActivity()).inflate(R.layout.item_vip_head_ship,null, false);
//        mLRecyclerViewAdapter.addHeaderView(header);
//        TextView title = header.findViewById(R.id.title);
//        title.getPaint().setFakeBoldText(true);



        rcy_icon.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (mIsFromClick) {//防止来回调
                    return;
                }
                changePosition();
            }
        });


        adapter1.setOnAddClickListener(new MembershipAdapter1.OnAddClickListener() {
            @Override
            public void onShareClick(int postion) {
                adapter1.setmPosition(postion);

                mIsFromClick = true;//不走onScrolled,防止来回调
                manager.scrollToPositionWithOffset(postion,0);

                mIsFromClick = false;//放开


                /*
                *
                *    LinearSmoothScroller topScroller = new LinearSmoothScroller(getActivity()) {

                    @Override
                    protected int getHorizontalSnapPreference() {
                        return SNAP_TO_START;//具体见源码注释
                    }

                    @Override
                    protected int getVerticalSnapPreference() {
                        return SNAP_TO_START;//具体见源码注释
                    }

                    @Override
                    protected void onStop() {
                        super.onStop();
                        //发送一个延时handler
                        //因为onStop时还有惯性动画
                        TimeUtil.mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mIsFromClick = false;
                            }
                        }, 500);
                    }
                };
                topScroller.setTargetPosition(postion);
                manager.startSmoothScroll(topScroller);
*/


            }
        });


    }

    private void changePosition() {

        int firstPosition = manager.findFirstVisibleItemPosition();
        if (adapter1.mCheckedPosition != firstPosition) {
            adapter1.mCheckedPosition = firstPosition;
            adapter1.notifyDataSetChanged();

            //此方法无置顶效果
            rcy_title.scrollToPosition(adapter1.mCheckedPosition);
        }


    }

    //获取会员权益
    public Observable>> getVipRightship(RxFragment fragment) {
        RequestBaseOs requestBannerBean = new RequestBaseOs();
        requestBannerBean.setOs(1);
        return RxHttp.getInstance().getSysteService().getVipRights(requestBannerBean)
                .compose(RxUtils.>>switchSchedulers())
                .compose(fragment.>>bindToLifecycle());
    }
    @Subscribe  //订阅事件
    public void onEventMainThread(MessageEvent event) {
        if (event.getAction().equals(EventBusAction.ACTION_REFRSH)) {
            getData();

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}

下面是adapter


/*
 * 会员权益title
 * */
public class MembershipAdapter1 extends RecyclerView.Adapter {
    private Context context;
    private List list = new ArrayList<>();
    public int mCheckedPosition = 0;

    public MembershipAdapter1(Context context) {
        this.context = context;

    }
    public int getmPosition() {
        return mCheckedPosition;
    }

    public void setmPosition(int mPosition) {
        this.mCheckedPosition = mPosition;
    }


    public void setData(List data) {
        if (data != null) {
            list.clear();
            list.addAll(data);
            notifyDataSetChanged();
        }
    }


    public void addData(List data) {
        if (data != null) {
            list.addAll(data);
            notifyItemRangeChanged(0, data.size());
        }
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.itme_membership1, parent, false);
        ViewHolder holder = new ViewHolder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        String categoryTitle = list.get(position).getCategoryTitle();
        if (!TextUtils.isEmpty(categoryTitle)){
            StringBuffer buffer=new StringBuffer(categoryTitle);
            buffer.insert(2,"\n");
            holder.title.setText(buffer.toString());
        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              onItemAddClick.onShareClick(position);
              notifyDataSetChanged();
            }
        });



        if (position== mCheckedPosition){
            holder.title.setTextColor(Color.parseColor("#F05557"));
            holder.tv.setBackgroundColor( Color.parseColor("#FFFFFF"));//选中灰色,不选择白色
            holder.line.setVisibility(View.VISIBLE);
        }else{
            holder.tv.setBackgroundColor( Color.parseColor("#F8F8F8"));//选中灰色,不选择白色
            holder.line.setVisibility(View.GONE);
            holder.title.setTextColor(Color.parseColor("#333333"));
        }


    }


    @Override
    public int getItemCount() {

        return list == null ? 0 : list.size();


    }

    public class ViewHolder extends RecyclerView.ViewHolder {


        private TextView title;
        private LinearLayout tv;
        private View line;

        public ViewHolder(View itemView) {
            super(itemView);
            tv=itemView.findViewById(R.id.tv);
            title = itemView.findViewById(R.id.title);//标题
            line=itemView.findViewById(R.id.line);

        }
    }

    public static interface OnAddClickListener {

        public void onShareClick(int postion);
    }

    // add click callback
    OnAddClickListener onItemAddClick;

    public void setOnAddClickListener(OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }
}
public class MembershipAdapter2 extends RecyclerView.Adapter {
    private Context context;
    private List list = new ArrayList<>();


    public MembershipAdapter2(Context context) {
        this.context = context;

    }

    public void setData(List data) {
        if (data != null) {
            list.clear();
            list.addAll(data);
            notifyDataSetChanged();
        }
    }


    public void addData(List data) {
        if (data != null) {
            list.addAll(data);
            notifyItemRangeChanged(0, data.size());
        }
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.itme_membership2, parent, false);
        ViewHolder holder = new ViewHolder(inflate);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //  List info = list.get(position).getCategoryTitle();
        String categoryTitle = list.get(position).getCategoryTitle();

        if (!TextUtils.isEmpty(categoryTitle)) {
            holder.title.setText(categoryTitle);
            holder.title.getPaint().setFakeBoldText(true);
        }

        MembershipAdapter3 adapter3 = new MembershipAdapter3(context);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
        holder.recycleview.setLayoutManager(gridLayoutManager);
        holder.recycleview.setAdapter(adapter3);
        holder.recycleview.setNestedScrollingEnabled(false);
        adapter3.setData(list.get(position).getVo());
        if (position == 0) {
            holder.ll.setVisibility(View.VISIBLE);
        }else{
            holder.ll.setVisibility(View.GONE);
        }
        holder.title2.getPaint().setFakeBoldText(true);


    }


    @Override
    public int getItemCount() {

        return list == null ? 0 : list.size();


    }

    public class ViewHolder extends RecyclerView.ViewHolder {


        private TextView title, title2;
        private RecyclerView recycleview;
        private LinearLayout ll;

        public ViewHolder(View itemView) {
            super(itemView);

            title = itemView.findViewById(R.id.title);//标题
            title2 = itemView.findViewById(R.id.title2);//标题
            recycleview = itemView.findViewById(R.id.recycleview);//icon
            ll=itemView.findViewById(R.id.ll);//

        }
    }

    public static interface OnAddClickListener {

        public void onShareClick();
    }

    // add click callback
    OnAddClickListener onItemAddClick;

    public void setOnAddClickListener(OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }
}

这就大公告层了

只要按照步骤来 一切都OK

你可能感兴趣的:(android,android,studio)