admin
2022-08-09 399ac289f80b7a40aa4210341db6b447cacdcf14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.tejia.lijin.app.ui.category.utilview;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
 
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.entity.SecondCategory;
 
import java.util.List;
 
/**
 * Created by User on 2017/7/24.
 */
 
public class ItemHeaderDecoration1 extends RecyclerView.ItemDecoration {
    private Context mContext;
    private List<SecondCategory> mList;
    public static String currentTag = "0";
    private LayoutInflater mLayoutInflater;
    private int mTitleHight = 30;
    private int mTitleTextSize = 16;
    CategorySlideListener slideListener;
 
    public ItemHeaderDecoration1(Context context, List<SecondCategory> dataList) {
        super();
        this.mContext = context;
        this.mList = dataList;
        mTitleHight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, context.getResources().getDisplayMetrics());
        // mTitleTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, context.getResources().getDisplayMetrics());
        this.mLayoutInflater = LayoutInflater.from(mContext);
    }
 
    public static void setCurrentTag(String tag) {
        ItemHeaderDecoration1.currentTag = tag;
    }
 
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
    }
 
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
    }
 
    TextView tv_recommend_taobao;
    TextView tv_recommend_jingdong;
    TextView tv_recommend_pinduoduo;
    View v_recommend_taobao;
    View v_recommend_jingdong;
    View v_recommend_pinduoduo;
 
    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        //获取到视图中第一个可见的item的position
        int position = ((LinearLayoutManager) parent.getLayoutManager()).findFirstVisibleItemPosition();
//        int position = ((GridLayoutManager) parent.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
        if (position == 0)
            return;
        if (position > 1) {
            position = 1;
        }
        String tag = mList.get(position).getTag();
        boolean flag = false;
        View topTitleView = mLayoutInflater.inflate(R.layout.item_recommend_platform, parent, false);
 
 
 
        int toDrawWidthSpec;//用于测量的widthMeasureSpec
        int toDrawHeightSpec;//用于测量的heightMeasureSpec
        RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) topTitleView.getLayoutParams();
        if (lp == null) {
            lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);//这里是根据复杂布局layout的width height,new一个Lp
            topTitleView.setLayoutParams(lp);
        }
        topTitleView.setLayoutParams(lp);
        if (lp.width == ViewGroup.LayoutParams.MATCH_PARENT) {
            //如果是MATCH_PARENT,则用父控件能分配的最大宽度和EXACTLY构建MeasureSpec。
            toDrawWidthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth() - parent.getPaddingLeft() - parent.getPaddingRight(), View.MeasureSpec.EXACTLY);
        } else if (lp.width == ViewGroup.LayoutParams.WRAP_CONTENT) {
            //如果是WRAP_CONTENT,则用父控件能分配的最大宽度和AT_MOST构建MeasureSpec。
            toDrawWidthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth() - parent.getPaddingLeft() - parent.getPaddingRight(), View.MeasureSpec.AT_MOST);
        } else {
            //否则则是具体的宽度数值,则用这个宽度和EXACTLY构建MeasureSpec。
            toDrawWidthSpec = View.MeasureSpec.makeMeasureSpec(lp.width, View.MeasureSpec.EXACTLY);
        }
        //高度同理
        if (lp.height == ViewGroup.LayoutParams.MATCH_PARENT) {
            toDrawHeightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight() - parent.getPaddingTop() - parent.getPaddingBottom(), View.MeasureSpec.EXACTLY);
        } else if (lp.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
            toDrawHeightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight() - parent.getPaddingTop() - parent.getPaddingBottom(), View.MeasureSpec.AT_MOST);
        } else {
            toDrawHeightSpec = View.MeasureSpec.makeMeasureSpec(mTitleHight, View.MeasureSpec.EXACTLY);
        }
        //依次调用 measure,layout,draw方法,将复杂头部显示在屏幕上。
        topTitleView.measure(toDrawWidthSpec, toDrawHeightSpec);
        topTitleView.layout(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getPaddingLeft() + topTitleView.getMeasuredWidth(), parent.getPaddingTop() + topTitleView.getMeasuredHeight());
        topTitleView.draw(c);//Canvas默认在视图顶部,无需平移,直接绘制
        if (flag)
            c.restore();//恢复画布到之前保存的状态
        if (!TextUtils.equals(tag, currentTag)) {
            currentTag = tag;
        }
    }
 
    /**
     * 点击状态变化
     */
    private void clickState(int goodsType) {
        tv_recommend_taobao.setTextColor(tv_recommend_taobao.getResources().getColor
                (goodsType == 1 ? R.color.theme : R.color.text_black_1));
        v_recommend_taobao.setVisibility(goodsType == 1 ? View.VISIBLE : View.GONE);
        tv_recommend_jingdong.setTextColor(tv_recommend_jingdong.getResources().getColor
                (goodsType == 2 ? R.color.theme : R.color.text_black_1));
        v_recommend_jingdong.setVisibility(goodsType == 2 ? View.VISIBLE : View.GONE);
        tv_recommend_pinduoduo.setTextColor(tv_recommend_pinduoduo.getResources().getColor
                (goodsType == 3 ? R.color.theme : R.color.text_black_1));
        v_recommend_pinduoduo.setVisibility(goodsType == 3 ? View.VISIBLE : View.GONE);
    }
}