1 /* This file is auto-generated from SearchFragment.java. DO NOT MODIFY. */ 2 3 package com.example.android.leanback; 4 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.os.Handler; 8 import android.support.v4.app.ActivityOptionsCompat; 9 import android.support.v17.leanback.widget.ArrayObjectAdapter; 10 import android.support.v17.leanback.widget.HeaderItem; 11 import android.support.v17.leanback.widget.ImageCardView; 12 import android.support.v17.leanback.widget.ListRow; 13 import android.support.v17.leanback.widget.ListRowPresenter; 14 import android.support.v17.leanback.widget.ObjectAdapter; 15 import android.support.v17.leanback.widget.OnItemViewClickedListener; 16 import android.support.v17.leanback.widget.Presenter; 17 import android.support.v17.leanback.widget.Row; 18 import android.support.v17.leanback.widget.RowPresenter; 19 import android.text.TextUtils; 20 import android.util.Log; 21 22 public class SearchSupportFragment extends android.support.v17.leanback.app.SearchSupportFragment 23 implements android.support.v17.leanback.app.SearchSupportFragment.SearchResultProvider { 24 private static final String TAG = "leanback.SearchSupportFragment"; 25 private static final int NUM_ROWS = 3; 26 private static final int SEARCH_DELAY_MS = 1000; 27 28 private ArrayObjectAdapter mRowsAdapter; 29 private Handler mHandler = new Handler(); 30 private String mQuery; 31 32 @Override onCreate(Bundle savedInstanceState)33 public void onCreate(Bundle savedInstanceState) { 34 super.onCreate(savedInstanceState); 35 36 mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter()); 37 38 setBadgeDrawable(getActivity().getResources().getDrawable(R.drawable.ic_title)); 39 setTitle("Leanback Sample App"); 40 setSearchResultProvider(this); 41 setOnItemViewClickedListener(new ItemViewClickedListener()); 42 } 43 44 @Override getResultsAdapter()45 public ObjectAdapter getResultsAdapter() { 46 return mRowsAdapter; 47 } 48 49 @Override onQueryTextChange(String newQuery)50 public boolean onQueryTextChange(String newQuery) { 51 Log.i(TAG, String.format("Search Query Text Change %s", newQuery)); 52 mRowsAdapter.clear(); 53 loadQuery(newQuery); 54 return true; 55 } 56 57 @Override onQueryTextSubmit(String query)58 public boolean onQueryTextSubmit(String query) { 59 Log.i(TAG, String.format("Search Query Text Submit %s", query)); 60 mRowsAdapter.clear(); 61 loadQuery(query); 62 return true; 63 } 64 loadQuery(String query)65 private void loadQuery(String query) { 66 mQuery = query; 67 mHandler.removeCallbacks(mDelayedLoad); 68 if (!TextUtils.isEmpty(query) && !query.equals("nil")) { 69 mHandler.postDelayed(mDelayedLoad, SEARCH_DELAY_MS); 70 } 71 } 72 loadRows()73 private void loadRows() { 74 for (int i = 0; i < NUM_ROWS; ++i) { 75 ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new CardPresenter()); 76 listRowAdapter.add(new PhotoItem("Hello world", R.drawable.gallery_photo_1)); 77 listRowAdapter.add(new PhotoItem("This is a test", R.drawable.gallery_photo_2)); 78 HeaderItem header = new HeaderItem(i, mQuery + " results row " + i); 79 mRowsAdapter.add(new ListRow(header, listRowAdapter)); 80 } 81 } 82 83 private Runnable mDelayedLoad = new Runnable() { 84 @Override 85 public void run() { 86 loadRows(); 87 } 88 }; 89 90 private final class ItemViewClickedListener implements OnItemViewClickedListener { 91 @Override onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, RowPresenter.ViewHolder rowViewHolder, Row row)92 public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item, 93 RowPresenter.ViewHolder rowViewHolder, Row row) { 94 Intent intent = new Intent(getActivity(), DetailsSupportActivity.class); 95 intent.putExtra(DetailsSupportActivity.EXTRA_ITEM, (PhotoItem) item); 96 97 Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation( 98 getActivity(), 99 ((ImageCardView)itemViewHolder.view).getMainImageView(), 100 DetailsSupportActivity.SHARED_ELEMENT_NAME).toBundle(); 101 getActivity().startActivity(intent, bundle); 102 } 103 } 104 } 105