code cleanup
[fanfix.git] / src / be / nikiroo / fanfix / reader / android / AndroidReaderGroup.java
1 package be.nikiroo.fanfix.reader.android;
2
3 import android.app.Fragment;
4 import android.app.FragmentTransaction;
5 import android.content.Context;
6 import android.os.Bundle;
7 import android.view.LayoutInflater;
8 import android.view.View;
9 import android.view.ViewGroup;
10 import android.widget.BaseAdapter;
11 import android.widget.ListView;
12
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.List;
16 import java.util.Map;
17
18 import be.nikiroo.fanfix.data.MetaData;
19 import be.nikiroo.fanfix.reader.Reader;
20
21 /**
22 * A simple {@link Fragment} subclass. Activities that contain this fragment
23 * must implement the {@link AndroidReaderGroup.OnFragmentInteractionListener}
24 * interface to handle interaction events.
25 */
26 public class AndroidReaderGroup extends Fragment {
27 private OnFragmentInteractionListener listener;
28 private Map<View, AndroidReaderBook> books = new HashMap<View, AndroidReaderBook>();
29
30 public interface OnFragmentInteractionListener {
31 void onFragmentInteraction(MetaData meta);
32 }
33
34 public AndroidReaderGroup() {
35 // Required empty public constructor
36 }
37
38 @Override
39 public View onCreateView(LayoutInflater inflater, ViewGroup container,
40 Bundle savedInstanceState) {
41 return inflater.inflate(R.layout.fragment_android_reader_group,
42 container, false);
43 }
44
45 @Override
46 public void onAttach(Context context) {
47 super.onAttach(context);
48 if (context instanceof OnFragmentInteractionListener) {
49 listener = (OnFragmentInteractionListener) context;
50 }
51 }
52
53 @Override
54 public void onDetach() {
55 super.onDetach();
56 listener = null;
57 }
58
59 public void fill(final List<MetaData> metas, final Reader reader) {
60 final List<MetaData> datas = new ArrayList<MetaData>(metas);
61
62 ListView list = getView().findViewById(R.id.Group_root);
63 list.setAdapter(new BaseAdapter() {
64 @Override
65 public int getCount() {
66 return datas.size();
67 }
68
69 @Override
70 public long getItemId(int position) {
71 return -1; // TODO: what is a "row id" in this context?
72 }
73
74 @Override
75 public Object getItem(int position) {
76 return datas.get(position);
77 }
78
79 @Override
80 public View getView(int position, View convertView, ViewGroup parent) {
81 AndroidReaderBook book = books.get(convertView);
82 if (book == null) {
83 book = new AndroidReaderBook();
84
85 FragmentTransaction trans = getFragmentManager()
86 .beginTransaction();
87 trans.add(book, null);
88 trans.commit();
89 getFragmentManager().executePendingTransactions();
90
91 books.put(book.getView(), book);
92 }
93
94 MetaData meta = (MetaData) getItem(position);
95 book.fill(meta, reader);
96
97 return book.getView();
98 }
99 });
100 }
101 }