-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoaderListFragment.java
76 lines (63 loc) · 2.36 KB
/
LoaderListFragment.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
package cheng.app.nga.fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import cheng.app.nga.R;
import java.util.List;
public abstract class LoaderListFragment<T> extends LoaderFragment<T> implements OnItemClickListener {
static final String TAG = "LoaderListFragment";
private BaseAdapter mAdapter;
private AbsListView mListView;
private TextView mEmptyView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list_content, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mListView = (AbsListView) view.findViewById(android.R.id.list);
mEmptyView = (TextView) view.findViewById(android.R.id.empty);
if (mListView == null) {
throw new IllegalStateException("mast have a AbsListView with id(android.R.id.list)");
}
mListView.setEmptyView(mEmptyView);
mListView.setOnItemClickListener(this);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mAdapter = createListAdapter(getActivity(), getData());
}
protected abstract BaseAdapter createListAdapter(Context context, List<T> list);
@Override
public void onLoadFinished(Loader<List<T>> arg0, List<T> arg1) {
super.onLoadFinished(arg0, arg1);
mAdapter.notifyDataSetChanged();
}
@Override
public void onLoaderReset(Loader<List<T>> arg0) {
super.onLoaderReset(arg0);
mAdapter.notifyDataSetChanged();
}
public AbsListView getListView() {
return mListView;
}
public BaseAdapter getAdapter() {
return mAdapter;
}
public void setEmptyText(CharSequence text) {
if (mEmptyView == null) {
throw new IllegalStateException("Can't be used with a custom content view");
}
mEmptyView.setText(text);
}
}