android recyview2个模仿淘宝详情界面浏览,竖向滑动,同时切换头部状态

 

 

头部:

package com.example.colormaterial.adapter;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.colormaterial.R;
import com.example.colormaterial.interfaces.OnClickListenerSelect;
import com.example.colormaterial.net.model.EditModel;

import java.util.List;

/**
 * author : jian11058
 * time    : 5/21/21
 * description:
 */
public class TitleSelectAdapter extends BaseQuickAdapter<EditModel,BaseViewHolder> {

    private OnClickListenerSelect onClickListenerSelect;

    public TitleSelectAdapter( @Nullable List<EditModel> data) {
        super(R.layout.item_title_select, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, EditModel item) {
        int adapterPosition = helper.getAdapterPosition();
        TextView tv=helper.getView(R.id.item_detail_title_tv);
        ImageView img=helper.getView(R.id.item_detail_title_img);
        tv.setText(item.title);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSelect();
                item.isVip=true;
                notifyDataSetChanged();
                if (onClickListenerSelect!=null){
                    onClickListenerSelect.select(adapterPosition);
                }
            }
        });
        if (item.isVip){
            img.setVisibility(View.VISIBLE);
        }else {
            img.setVisibility(View.INVISIBLE);
        }
        tv.setSelected(item.isVip);
    }
    private void setSelect(){
        for (EditModel datum : getData()) {
            datum.isVip=false;
        }
    }

    public void setSelectPosition(int position){
        if (position<1){
            position=0;
        }
        setSelect();
        getData().get(position).isVip=true;
        notifyDataSetChanged();
    }
    public void setOnclickListener(OnClickListenerSelect onclickListener){
        onClickListenerSelect=onclickListener;
    }

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:id="@+id/item_detail_title_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/detail_title_example"
        android:textSize="@dimen/sp_14"
        android:textColor="@drawable/select_blue1_grey_color"
        android:paddingLeft="@dimen/dp_4"
        android:paddingRight="@dimen/dp_4"
        android:paddingBottom="@dimen/dp_10"/>

    <ImageView
        android:id="@+id/item_detail_title_img"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_2"
        android:background="@color/blue_1"/>
</LinearLayout>

 

activity中监听事件做处理:

    private void initRecycleview() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(manager);
        adapter=new DetailAdapter();
        recyclerView.setAdapter(adapter);

        LinearLayoutManager managerTitle = new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false);
        recyclerView_title.setLayoutManager(managerTitle);
        List<EditModel> list=new ArrayList<>();
        String[] titles = getResources().getStringArray(R.array.detail_title);
        for (int i = 0; i < titles.length; i++) {
            EditModel editModel=new EditModel();
            editModel.title=titles[i];
            if (i==0){
                editModel.isVip=true;
            }
            list.add(editModel);
        }
        adapter_title=new TitleSelectAdapter(list);
        recyclerView_title.setAdapter(adapter_title);

         recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                //关键代码
                if (postion != manager_detail.findFirstVisibleItemPosition()) {
                    postion = manager_detail.findFirstVisibleItemPosition();
                    adapter_title.setSelectPosition(postion);
                }
            }
        });

        adapter_title.setOnclickListener(new OnClickListenerSelect() {
            @Override
            public void select(int position) {
                //这个是跳转到尾部,如果是个长列表,就会让列表的尾部出现在屏幕上
                manager.scrollToPosition(postion);
                //这个是跳转到首位,0是与底部的距离,默认写0
                manager_detail.scrollToPositionWithOffset(position,0);

            }
        });
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian11058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值