OSDN Git Service

rename refactor
[android-x86/external-koush-Widgets.git] / Widgets / src / com / koushikdutta / widgets / ListContentFragment.java
1 package com.koushikdutta.widgets;
2
3 import android.content.res.Configuration;
4 import android.os.Bundle;
5 import android.support.v4.app.Fragment;
6 import android.support.v4.app.FragmentTransaction;
7 import android.view.KeyEvent;
8 import android.view.LayoutInflater;
9 import android.view.View;
10 import android.view.ViewGroup;
11 import android.widget.ListView;
12 import android.widget.ViewSwitcher;
13
14 public class ListContentFragment extends BetterListFragment {
15     ViewGroup mContent;
16     ViewGroup mContainer;
17     
18     @Override
19     protected int getListHeaderResource() {
20         return R.layout.list_content_header;
21     }
22     
23     private void setPadding() {
24         float hor = getResources().getDimension(R.dimen.activity_horizontal_margin);
25         float ver = getResources().getDimension(R.dimen.activity_vertical_margin);
26         getListView().setPadding(0, 0, 0, 0);
27         mContainer.setPadding((int)hor, (int)ver, (int)hor, (int)ver);
28     }
29     
30     Fragment mCurrentContent;
31     
32     @Override
33     protected void onCreate(Bundle savedInstanceState, View ret) {
34         mContent = (ViewGroup)ret.findViewById(R.id.content);
35         mContainer = (ViewGroup)ret.findViewById(R.id.list_content_container);
36
37         setPadding();
38         getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
39         
40         super.onCreate(savedInstanceState, ret);
41     }
42     
43     public boolean isPaged() {
44         return mContainer instanceof ViewSwitcher;
45     }
46     
47     public void setContent(Fragment content, boolean clearChoices) {
48         Fragment last = mCurrentContent;
49         mCurrentContent = content;
50         FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
51         if (last != null)
52             ft.replace(R.id.content, mCurrentContent);
53         else
54             ft.add(R.id.content, mCurrentContent);
55         ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
56         if (mContainer instanceof ViewSwitcher) {
57             ViewSwitcher switcher = (ViewSwitcher)mContainer;
58             if (mContent != switcher.getCurrentView())
59                 switcher.showNext();
60         }
61         ft.commit();
62         if (clearChoices)
63             getListView().clearChoices();
64     }
65
66     public boolean onBackPressed() {
67         if (mCurrentContent == null)
68             return false;
69         if (mContainer instanceof ViewSwitcher) {
70             ((ViewSwitcher)mContainer).showPrevious();
71             FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
72             ft.remove(mCurrentContent);
73             ft.commit();
74             mCurrentContent = null;
75             return true;
76         }
77         return false;
78     }
79     
80     @Override
81     void onListItemClick(ListItem li) {
82         super.onListItemClick(li);
83 //        if (mContentAdapter == null)
84 //            return;
85 //        
86 //        setContent(mContentAdapter.getFragment(li, mCurrentContent));
87     }
88
89     @Override
90     protected int getListItemResource() {
91         return R.layout.list_item_selectable;
92     }
93
94     @Override
95     protected int getListFragmentResource() {
96         return R.layout.list_content;
97     }
98     
99     public ViewGroup getContent() {
100         return mContent;
101     }
102     
103     @Override
104     public void onConfigurationChanged(Configuration newConfig) {
105         super.onConfigurationChanged(newConfig);
106         setPadding();
107     }
108 //    
109 //    ListContentAdapter mContentAdapter;
110 //    public ListContentAdapter getContentAdapter() {
111 //        return mContentAdapter;
112 //    }
113 //    
114 //    public void setContentAdapter(ListContentAdapter adapter) {
115 //        mContentAdapter = adapter;
116 //    }
117 }