diff options
Diffstat (limited to 'samples/SupportLeanbackDemos')
5 files changed, 148 insertions, 0 deletions
diff --git a/samples/SupportLeanbackDemos/AndroidManifest.xml b/samples/SupportLeanbackDemos/AndroidManifest.xml index 5eeaae53b..8cf8236c5 100644 --- a/samples/SupportLeanbackDemos/AndroidManifest.xml +++ b/samples/SupportLeanbackDemos/AndroidManifest.xml @@ -26,5 +26,8 @@ <activity android:name="VerticalGridActivity" android:exported="true" /> + <activity android:name="SearchActivity" + android:exported="true" /> + </application> </manifest> diff --git a/samples/SupportLeanbackDemos/res/layout/search.xml b/samples/SupportLeanbackDemos/res/layout/search.xml new file mode 100644 index 000000000..b65600ca5 --- /dev/null +++ b/samples/SupportLeanbackDemos/res/layout/search.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2014 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + +<fragment xmlns:android="http://schemas.android.com/apk/res/android" + android:name="com.example.android.leanback.SearchFragment" + android:id="@+id/search_fragment" + android:layout_width="match_parent" + android:layout_height="match_parent" + />
\ No newline at end of file diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java index ec547ecba..2345ed09b 100644 --- a/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/BrowseFragment.java @@ -22,6 +22,7 @@ import android.support.v17.leanback.widget.ListRowPresenter; import android.support.v17.leanback.widget.OnItemClickedListener; import android.support.v17.leanback.widget.Row; import android.util.Log; +import android.view.View; public class BrowseFragment extends android.support.v17.leanback.app.BrowseFragment { private static final String TAG = "leanback.BrowseFragment"; @@ -40,6 +41,14 @@ public class BrowseFragment extends android.support.v17.leanback.app.BrowseFragm p.setHeadersState(HEADERS_ENABLED); setBrowseParams(p); + setOnSearchClickedListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent = new Intent(getActivity(), SearchActivity.class); + startActivity(intent); + } + }); + setupRows(); setOnItemClickedListener(new ItemClickedListener()); } diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/SearchActivity.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/SearchActivity.java new file mode 100644 index 000000000..437bd7267 --- /dev/null +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/SearchActivity.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package com.example.android.leanback; + +import android.app.Activity; +import android.os.Bundle; + +public class SearchActivity extends Activity +{ + /** Called when the activity is first created. */ + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + setContentView(R.layout.search); + } +} diff --git a/samples/SupportLeanbackDemos/src/com/example/android/leanback/SearchFragment.java b/samples/SupportLeanbackDemos/src/com/example/android/leanback/SearchFragment.java new file mode 100644 index 000000000..0489196b0 --- /dev/null +++ b/samples/SupportLeanbackDemos/src/com/example/android/leanback/SearchFragment.java @@ -0,0 +1,85 @@ +package com.example.android.leanback; + +import android.content.Intent; +import android.os.Bundle; +import android.os.Handler; +import android.support.v17.leanback.widget.ArrayObjectAdapter; +import android.support.v17.leanback.widget.HeaderItem; +import android.support.v17.leanback.widget.ListRow; +import android.support.v17.leanback.widget.ListRowPresenter; +import android.support.v17.leanback.widget.ObjectAdapter; +import android.support.v17.leanback.widget.OnItemClickedListener; +import android.support.v17.leanback.widget.Row; +import android.text.TextUtils; +import android.util.Log; + +public class SearchFragment extends android.support.v17.leanback.app.SearchFragment + implements android.support.v17.leanback.app.SearchFragment.SearchResultProvider { + private static final String TAG = "leanback.SearchFragment"; + private static final int NUM_ROWS = 3; + private static final int SEARCH_DELAY_MS = 300; + + private ArrayObjectAdapter mRowsAdapter; + private Handler mHandler = new Handler(); + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter()); + setSearchResultProvider(this); + setOnItemClickedListener(new ItemClickedListener()); + } + + @Override + public ObjectAdapter getResultsAdapter() { + return mRowsAdapter; + } + + @Override + public boolean onQueryTextChange(String newQuery) { + Log.i(TAG, String.format("Search Query Text Change %s", newQuery)); + mRowsAdapter.clear(); + if (!TextUtils.isEmpty(newQuery)) { + mHandler.removeCallbacks(mDelayedLoad); + mHandler.postDelayed(mDelayedLoad, SEARCH_DELAY_MS); + } + return true; + } + + @Override + public boolean onQueryTextSubmit(String query) { + Log.i(TAG, String.format("Search Query Text Submit %s", query)); + mRowsAdapter.clear(); + if (!TextUtils.isEmpty(query)) { + mHandler.removeCallbacks(mDelayedLoad); + mHandler.postDelayed(mDelayedLoad, SEARCH_DELAY_MS); + } + return true; + } + + private void loadRows() { + for (int i = 0; i < NUM_ROWS; ++i) { + ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new StringPresenter()); + listRowAdapter.add("Hello world"); + listRowAdapter.add("This is a test"); + HeaderItem header = new HeaderItem(i, "Row " + i, null); + mRowsAdapter.add(new ListRow(header, listRowAdapter)); + } + } + + private Runnable mDelayedLoad = new Runnable() { + @Override + public void run() { + loadRows(); + } + }; + private final class ItemClickedListener implements OnItemClickedListener { + public void onItemClicked(Object item, Row row) { + // TODO: use a fragment transaction instead of launching a new + // activity + Intent intent = new Intent(getActivity(), DetailsActivity.class); + startActivity(intent); + } + } +} |