opening html files was not OK anymore
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / android / AndroidReaderGroup.java
CommitLineData
64968418
NR
1package be.nikiroo.fanfix.reader.android;
2
3import android.app.Fragment;
4import android.app.FragmentTransaction;
5import android.content.Context;
64968418
NR
6import android.os.Bundle;
7import android.view.LayoutInflater;
8import android.view.View;
9import android.view.ViewGroup;
a18ba536
NR
10import android.widget.BaseAdapter;
11import android.widget.ListView;
64968418 12
a18ba536
NR
13import java.util.ArrayList;
14import java.util.HashMap;
64968418 15import java.util.List;
a18ba536 16import java.util.Map;
64968418
NR
17
18import be.nikiroo.fanfix.data.MetaData;
19import 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 */
26public class AndroidReaderGroup extends Fragment {
27 private OnFragmentInteractionListener listener;
a18ba536 28 private Map<View, AndroidReaderBook> books = new HashMap<View, AndroidReaderBook>();
64968418
NR
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) {
64968418
NR
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
a18ba536
NR
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
64968418 69 @Override
a18ba536
NR
70 public long getItemId(int position) {
71 return -1; // TODO: what is a "row id" in this context?
64968418
NR
72 }
73
74 @Override
a18ba536
NR
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);
64968418 92 }
a18ba536
NR
93
94 MetaData meta = (MetaData) getItem(position);
95 book.fill(meta, reader);
96
97 return book.getView();
64968418 98 }
a18ba536 99 });
64968418
NR
100 }
101}