1
+ package com .dds .anyui ;
2
+
3
+ import android .content .Context ;
4
+ import android .content .Intent ;
5
+ import android .os .Bundle ;
6
+
7
+ import androidx .annotation .NonNull ;
8
+ import androidx .fragment .app .Fragment ;
9
+ import androidx .fragment .app .FragmentActivity ;
10
+ import androidx .recyclerview .widget .GridLayoutManager ;
11
+ import androidx .recyclerview .widget .LinearLayoutManager ;
12
+ import androidx .recyclerview .widget .RecyclerView ;
13
+
14
+ import android .view .LayoutInflater ;
15
+ import android .view .View ;
16
+ import android .view .ViewGroup ;
17
+ import android .widget .TextView ;
18
+
19
+ import com .dds .recyclerview .RecyclerActivity ;
20
+
21
+ import java .util .ArrayList ;
22
+ import java .util .List ;
23
+
24
+ /**
25
+ * A fragment representing a list of Items.
26
+ */
27
+ public class ItemFragment extends Fragment {
28
+
29
+ private static final String ARG_COLUMN_COUNT = "column-count" ;
30
+ private int mColumnCount = 1 ;
31
+
32
+ static {
33
+ addItem ("测试1" , RecyclerActivity .class );
34
+ addItem ("测试2" , RecyclerActivity .class );
35
+ addItem ("测试3" , RecyclerActivity .class );
36
+ addItem ("测试4" , RecyclerActivity .class );
37
+
38
+ }
39
+
40
+ public static void addItem (String content , Class <?> clazz ) {
41
+ PlaceholderContent .addItem (new PlaceholderContent .PlaceholderItem (content , clazz ));
42
+ }
43
+
44
+ public ItemFragment () {
45
+ }
46
+
47
+ public static ItemFragment newInstance (int columnCount ) {
48
+ ItemFragment fragment = new ItemFragment ();
49
+ Bundle args = new Bundle ();
50
+ args .putInt (ARG_COLUMN_COUNT , columnCount );
51
+ fragment .setArguments (args );
52
+ return fragment ;
53
+ }
54
+
55
+ @ Override
56
+ public void onCreate (Bundle savedInstanceState ) {
57
+ super .onCreate (savedInstanceState );
58
+
59
+ if (getArguments () != null ) {
60
+ mColumnCount = getArguments ().getInt (ARG_COLUMN_COUNT );
61
+ }
62
+ }
63
+
64
+ @ Override
65
+ public View onCreateView (LayoutInflater inflater , ViewGroup container ,
66
+ Bundle savedInstanceState ) {
67
+ View view = inflater .inflate (R .layout .fragment_item_list , container , false );
68
+
69
+ // Set the adapter
70
+ if (view instanceof RecyclerView ) {
71
+ Context context = view .getContext ();
72
+ RecyclerView recyclerView = (RecyclerView ) view ;
73
+ if (mColumnCount <= 1 ) {
74
+ recyclerView .setLayoutManager (new LinearLayoutManager (context ));
75
+ } else {
76
+ recyclerView .setLayoutManager (new GridLayoutManager (context , mColumnCount ));
77
+ }
78
+ MyItemRecyclerViewAdapter adapter = new MyItemRecyclerViewAdapter (PlaceholderContent .ITEMS );
79
+ adapter .setOnItemClickListener ((view1 , placeholderItem , position ) -> {
80
+ FragmentActivity activity = getActivity ();
81
+ if (activity != null ) {
82
+ activity .startActivity (new Intent (activity , placeholderItem .clazz ));
83
+ }
84
+ });
85
+ recyclerView .setAdapter (adapter );
86
+ }
87
+ return view ;
88
+ }
89
+
90
+
91
+ public static class MyItemRecyclerViewAdapter extends RecyclerView .Adapter <MyItemRecyclerViewAdapter .ViewHolder > {
92
+
93
+ private final List <PlaceholderContent .PlaceholderItem > mValues ;
94
+ private OnItemClickListener mOnItemClickListener ;
95
+
96
+ public MyItemRecyclerViewAdapter (List <PlaceholderContent .PlaceholderItem > items ) {
97
+ mValues = items ;
98
+ }
99
+
100
+ @ NonNull
101
+ @ Override
102
+ public ViewHolder onCreateViewHolder (ViewGroup parent , int viewType ) {
103
+ ViewHolder viewHolder = new ViewHolder (LayoutInflater .from (parent .getContext ()).inflate (R .layout .fragment_item , parent , false ));
104
+ bindViewClickListener (viewHolder );
105
+ return viewHolder ;
106
+ }
107
+
108
+ private void bindViewClickListener (ViewHolder viewHolder ) {
109
+ bindViewItemClickListener (viewHolder );
110
+ }
111
+
112
+ private void bindViewItemClickListener (ViewHolder viewHolder ) {
113
+ viewHolder .itemView .setOnClickListener (v -> {
114
+ if (mOnItemClickListener != null ) {
115
+ int position = viewHolder .getAbsoluteAdapterPosition ();
116
+ PlaceholderContent .PlaceholderItem placeholderItem = mValues .get (position );
117
+ mOnItemClickListener .onItemClick (viewHolder .itemView , placeholderItem , position );
118
+ }
119
+
120
+
121
+ });
122
+ }
123
+
124
+ @ Override
125
+ public void onBindViewHolder (final ViewHolder holder , int position ) {
126
+ holder .mItem = mValues .get (position );
127
+ holder .mContentView .setText (mValues .get (position ).content );
128
+ }
129
+
130
+ @ Override
131
+ public int getItemCount () {
132
+ return mValues .size ();
133
+ }
134
+
135
+ public void setOnItemClickListener (OnItemClickListener l ) {
136
+ mOnItemClickListener = l ;
137
+ }
138
+
139
+ public static class ViewHolder extends RecyclerView .ViewHolder {
140
+ public final TextView mContentView ;
141
+ public PlaceholderContent .PlaceholderItem mItem ;
142
+
143
+ public ViewHolder (View view ) {
144
+ super (view );
145
+ mContentView = view .findViewById (R .id .content );
146
+ }
147
+
148
+ @ NonNull
149
+ @ Override
150
+ public String toString () {
151
+ return super .toString () + " '" + mContentView .getText () + "'" ;
152
+ }
153
+
154
+ }
155
+
156
+ public interface OnItemClickListener {
157
+ void onItemClick (View view , PlaceholderContent .PlaceholderItem placeholderItem , int position );
158
+ }
159
+ }
160
+
161
+ public static class PlaceholderContent {
162
+
163
+ public static final List <PlaceholderContent .PlaceholderItem > ITEMS = new ArrayList <>();
164
+
165
+ public static void addItem (PlaceholderContent .PlaceholderItem item ) {
166
+ ITEMS .add (item );
167
+ }
168
+
169
+ /**
170
+ * A placeholder item representing a piece of content.
171
+ */
172
+ public static class PlaceholderItem {
173
+ public final String content ;
174
+ public final Class <?> clazz ;
175
+
176
+ public PlaceholderItem (String content , Class <?> clazz ) {
177
+ this .content = content ;
178
+ this .clazz = clazz ;
179
+ }
180
+
181
+ @ NonNull
182
+ @ Override
183
+ public String toString () {
184
+ return content ;
185
+ }
186
+ }
187
+ }
188
+
189
+
190
+ }
0 commit comments