NavigationAdapter.java
3.35 KB
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
131
package com.studymachine.www.ui.adapter;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.hjq.base.BaseAdapter;
import com.studymachine.www.R;
import com.studymachine.www.app.AppAdapter;
/**
* time : 2021/02/28
* desc : 导航栏适配器
*/
public final class NavigationAdapter extends AppAdapter<NavigationAdapter.MenuItem>
implements BaseAdapter.OnItemClickListener {
/** 当前选中条目位置 */
private int mSelectedPosition = 0;
/** 导航栏点击监听 */
@Nullable
private OnNavigationListener mListener;
public NavigationAdapter(Context context) {
super(context);
setOnItemClickListener(this);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder();
}
@Override
protected RecyclerView.LayoutManager generateDefaultLayoutManager(Context context) {
return new GridLayoutManager(context, getCount(), RecyclerView.VERTICAL, false);
}
public int getSelectedPosition() {
return mSelectedPosition;
}
public void setSelectedPosition(int position) {
mSelectedPosition = position;
notifyDataSetChanged();
}
/**
* 设置导航栏监听
*/
public void setOnNavigationListener(@Nullable OnNavigationListener listener) {
mListener = listener;
}
/**
* {@link BaseAdapter.OnItemClickListener}
*/
@Override
public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
if (mSelectedPosition == position) {
return;
}
if (mListener == null) {
mSelectedPosition = position;
notifyDataSetChanged();
return;
}
if (mListener.onNavigationItemSelected(position)) {
mSelectedPosition = position;
notifyDataSetChanged();
}
}
private final class ViewHolder extends AppAdapter<?>.ViewHolder {
private final ImageView mIconView;
private ViewHolder() {
super(R.layout.home_navigation_item);
mIconView = findViewById(R.id.iv_home_navigation_icon);
}
@Override
public void onBindView(int position) {
MenuItem item = getItem(position);
mIconView.setImageDrawable(item.getDrawable());
mIconView.setSelected(mSelectedPosition == position);
}
}
public static class MenuItem {
private final String mText;
private final Drawable mDrawable;
public MenuItem(String text, Drawable drawable) {
mText = text;
mDrawable = drawable;
}
public MenuItem( Drawable drawable) {
mDrawable = drawable;
mText = "";
}
public String getText() {
return mText;
}
public Drawable getDrawable() {
return mDrawable;
}
}
public interface OnNavigationListener {
boolean onNavigationItemSelected(int position);
}
}