* commit 'a739ccfb45197c1a30492a4bd99d0664294bab53': Adding search to Leanback demo
This commit is contained in:
@@ -26,5 +26,8 @@
|
||||
<activity android:name="VerticalGridActivity"
|
||||
android:exported="true" />
|
||||
|
||||
<activity android:name="SearchActivity"
|
||||
android:exported="true" />
|
||||
|
||||
</application>
|
||||
</manifest>
|
||||
|
||||
23
samples/SupportLeanbackDemos/res/layout/search.xml
Normal file
23
samples/SupportLeanbackDemos/res/layout/search.xml
Normal file
@@ -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"
|
||||
/>
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user