DetailsSupportFragment.java revision ee73a5bfd4d2460a93a9afb9d1bfce10ff875300
1/* This file is auto-generated from DetailsFragment.java.  DO NOT MODIFY. */
2
3/*
4 * Copyright (C) 2014 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7 * in compliance with the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software distributed under the License
12 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing permissions and limitations under
14 * the License.
15 */
16package android.support.v17.leanback.app;
17
18import android.support.v17.leanback.R;
19import android.support.v17.leanback.widget.ObjectAdapter;
20import android.support.v17.leanback.widget.OnItemClickedListener;
21import android.support.v17.leanback.widget.OnItemSelectedListener;
22import android.support.v17.leanback.widget.OnItemViewClickedListener;
23import android.support.v17.leanback.widget.OnItemViewSelectedListener;
24import android.support.v17.leanback.widget.Presenter;
25import android.support.v17.leanback.widget.Row;
26import android.support.v17.leanback.widget.RowPresenter;
27import android.support.v17.leanback.widget.VerticalGridView;
28import android.support.v4.app.Fragment;
29import android.os.Bundle;
30import android.view.LayoutInflater;
31import android.view.View;
32import android.view.ViewGroup;
33
34/**
35 * Wrapper fragment for leanback details screens.
36 */
37public class DetailsSupportFragment extends BaseSupportFragment {
38    private static final String TAG = "DetailsSupportFragment";
39    private static boolean DEBUG = false;
40
41    private class SetSelectionRunnable implements Runnable {
42        int mPosition;
43        boolean mSmooth = true;
44        @Override
45        public void run() {
46            mRowsSupportFragment.setSelectedPosition(mPosition, mSmooth);
47        }
48    }
49
50    private RowsSupportFragment mRowsSupportFragment;
51
52    private ObjectAdapter mAdapter;
53    private int mContainerListAlignTop;
54    private OnItemSelectedListener mOnItemSelectedListener;
55    private OnItemClickedListener mOnItemClickedListener;
56    private OnItemViewSelectedListener mOnItemViewSelectedListener;
57    private OnItemViewClickedListener mOnItemViewClickedListener;
58    private int mSelectedPosition = -1;
59
60    private Object mSceneAfterEntranceTransition;
61
62    private final SetSelectionRunnable mSetSelectionRunnable = new SetSelectionRunnable();
63
64    /**
65     * Sets the list of rows for the fragment.
66     */
67    public void setAdapter(ObjectAdapter adapter) {
68        mAdapter = adapter;
69        if (mRowsSupportFragment != null) {
70            mRowsSupportFragment.setAdapter(adapter);
71        }
72    }
73
74    /**
75     * Returns the list of rows.
76     */
77    public ObjectAdapter getAdapter() {
78        return mAdapter;
79    }
80
81    /**
82     * Sets an item selection listener.
83     * @deprecated Use {@link #setOnItemViewSelectedListener(OnItemViewSelectedListener)}
84     */
85    public void setOnItemSelectedListener(OnItemSelectedListener listener) {
86        if (mOnItemSelectedListener != null) {
87            mOnItemSelectedListener = listener;
88            if (mRowsSupportFragment != null) {
89                mRowsSupportFragment.setOnItemSelectedListener(mOnItemSelectedListener);
90            }
91        }
92    }
93
94    /**
95     * Sets an item Clicked listener.
96     * @deprecated Use {@link #setOnItemViewClickedListener(OnItemViewClickedListener)}
97     */
98    public void setOnItemClickedListener(OnItemClickedListener listener) {
99        if (mOnItemClickedListener != listener) {
100            mOnItemClickedListener = listener;
101            if (mRowsSupportFragment != null) {
102                mRowsSupportFragment.setOnItemClickedListener(listener);
103            }
104        }
105    }
106
107    /**
108     * Sets an item selection listener.
109     */
110    public void setOnItemViewSelectedListener(OnItemViewSelectedListener listener) {
111        if (mOnItemViewSelectedListener != listener) {
112            mOnItemViewSelectedListener = listener;
113            if (mRowsSupportFragment != null) {
114                mRowsSupportFragment.setOnItemViewSelectedListener(mOnItemViewSelectedListener);
115            }
116        }
117    }
118
119    /**
120     * Sets an item Clicked listener.
121     */
122    public void setOnItemViewClickedListener(OnItemViewClickedListener listener) {
123        if (mOnItemViewClickedListener != listener) {
124            mOnItemViewClickedListener = listener;
125            if (mRowsSupportFragment != null) {
126                mRowsSupportFragment.setOnItemViewClickedListener(listener);
127            }
128        }
129    }
130
131    /**
132     * Returns the item Clicked listener.
133     * @deprecated Use {@link #getOnItemViewClickedListener()}
134     */
135    public OnItemClickedListener getOnItemClickedListener() {
136        return mOnItemClickedListener;
137    }
138
139    /**
140     * Returns the item Clicked listener.
141     */
142    public OnItemViewClickedListener getOnItemViewClickedListener() {
143        return mOnItemViewClickedListener;
144    }
145
146    @Override
147    public void onCreate(Bundle savedInstanceState) {
148        super.onCreate(savedInstanceState);
149
150        mContainerListAlignTop =
151            getResources().getDimensionPixelSize(R.dimen.lb_details_rows_align_top);
152    }
153
154    @Override
155    public View onCreateView(LayoutInflater inflater, ViewGroup container,
156            Bundle savedInstanceState) {
157        View view = inflater.inflate(R.layout.lb_details_fragment, container, false);
158        mRowsSupportFragment = (RowsSupportFragment) getChildFragmentManager().findFragmentById(
159                R.id.fragment_dock);
160        if (mRowsSupportFragment == null) {
161            mRowsSupportFragment = new RowsSupportFragment();
162            getChildFragmentManager().beginTransaction()
163                    .replace(R.id.fragment_dock, mRowsSupportFragment).commit();
164        }
165        mRowsSupportFragment.setAdapter(mAdapter);
166        mRowsSupportFragment.setOnItemSelectedListener(mOnItemSelectedListener);
167        mRowsSupportFragment.setOnItemViewSelectedListener(mOnItemViewSelectedListener);
168        mRowsSupportFragment.setOnItemClickedListener(mOnItemClickedListener);
169        mRowsSupportFragment.setOnItemViewClickedListener(mOnItemViewClickedListener);
170        mSceneAfterEntranceTransition = sTransitionHelper.createScene((ViewGroup) view,
171                new Runnable() {
172            @Override
173            public void run() {
174                mRowsSupportFragment.setEntranceTransitionState(true);
175            }
176        });
177        return view;
178    }
179
180    void setVerticalGridViewLayout(VerticalGridView listview) {
181        // align the top edge of item to a fixed position
182        listview.setItemAlignmentOffset(0);
183        listview.setItemAlignmentOffsetPercent(VerticalGridView.ITEM_ALIGN_OFFSET_PERCENT_DISABLED);
184        listview.setWindowAlignmentOffset(mContainerListAlignTop);
185        listview.setWindowAlignmentOffsetPercent(VerticalGridView.WINDOW_ALIGN_OFFSET_PERCENT_DISABLED);
186        listview.setWindowAlignment(VerticalGridView.WINDOW_ALIGN_NO_EDGE);
187    }
188
189    VerticalGridView getVerticalGridView() {
190        return mRowsSupportFragment == null ? null : mRowsSupportFragment.getVerticalGridView();
191    }
192
193    RowsSupportFragment getRowsSupportFragment() {
194        return mRowsSupportFragment;
195    }
196
197    /**
198     * Setup dimensions that are only meaningful when the child Fragments are inside
199     * DetailsSupportFragment.
200     */
201    private void setupChildFragmentLayout() {
202        setVerticalGridViewLayout(mRowsSupportFragment.getVerticalGridView());
203    }
204
205    /**
206     * Sets the selected row position with smooth animation.
207     */
208    public void setSelectedPosition(int position) {
209        setSelectedPosition(position, true);
210    }
211
212    /**
213     * Sets the selected row position.
214     */
215    public void setSelectedPosition(int position, boolean smooth) {
216        mSetSelectionRunnable.mPosition = position;
217        mSetSelectionRunnable.mSmooth = smooth;
218        if (getView() != null && getView().getHandler() != null) {
219            getView().getHandler().post(mSetSelectionRunnable);
220        }
221    }
222
223    @Override
224    public void onStart() {
225        super.onStart();
226        setupChildFragmentLayout();
227        mRowsSupportFragment.getView().requestFocus();
228        if (isEntranceTransitionEnabled()) {
229            // make sure recycler view animation is disabled
230            mRowsSupportFragment.onTransitionStart();
231            mRowsSupportFragment.setEntranceTransitionState(false);
232        }
233    }
234
235    @Override
236    protected Object createEntranceTransition() {
237        return sTransitionHelper.loadTransition(getActivity(),
238                R.transition.lb_details_enter_transition);
239    }
240
241    @Override
242    protected void runEntranceTransition(Object entranceTransition) {
243        sTransitionHelper.runTransition(mSceneAfterEntranceTransition,
244                entranceTransition);
245    }
246
247    @Override
248    protected void onEntranceTransitionEnd() {
249        mRowsSupportFragment.onTransitionEnd();
250    }
251
252}
253