-
Notifications
You must be signed in to change notification settings - Fork 22
/
MyListFragment.java
112 lines (94 loc) · 3.08 KB
/
MyListFragment.java
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
package de.afarber.mylist;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
public class MyListFragment extends ListFragment implements MyConstants {
static class ViewHolder {
public CheckedTextView text1;
}
public interface ListListener {
public void itemSelected(int index);
}
private ListListener mListener;
private ArrayAdapter<String> mAdapter;
private ArrayList<String> mItems = new ArrayList<String>(Arrays.asList(
"Item 01",
"Item 02",
"Item 03",
"Item 04",
"Item 05",
"Item 06",
"Item 07",
"Item 08",
"Item 09",
"Item 10",
"Item 11",
"Item 12"
));
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof ListListener) {
mListener = (ListListener) activity;
} else {
throw new ClassCastException(activity.toString() +
" must implement MyListFragment.ListListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public static MyListFragment newInstance(int index) {
MyListFragment fragment = new MyListFragment();
Bundle args = new Bundle();
args.putInt(INDEX, index);
fragment.setArguments(args);
return fragment;
}
public void onViewCreated(View v, Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
mAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_checked,
android.R.id.text1,
mItems) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = getActivity().getLayoutInflater();
convertView = inflater.inflate(android.R.layout.simple_list_item_checked, null);
holder = new ViewHolder();
holder.text1 = (CheckedTextView) convertView.findViewById(android.R.id.text1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String str = (String) getItem(position);
holder.text1.setText(str);
return convertView;
}
};
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
setListAdapter(mAdapter);
}
@Override
public void onResume() {
super.onResume();
int index = getArguments().getInt(INDEX);
getListView().setItemChecked(index, true);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
mListener.itemSelected(position);
}
}