Add VerticalGridActivity to the leanback demo.

Change-Id: I38e8a85d5a7ae30b1c0ee71cff979456bdac0665
This commit is contained in:
Craig Stout
2014-04-08 16:39:56 -07:00
committed by Tim Kilbourn
parent 29830054ff
commit 3d2e6fb968
5 changed files with 167 additions and 0 deletions

View File

@@ -22,5 +22,9 @@
<activity android:name="DetailsActivity" <activity android:name="DetailsActivity"
android:exported="true" /> android:exported="true" />
<activity android:name="VerticalGridActivity"
android:exported="true" />
</application> </application>
</manifest> </manifest>

Binary file not shown.

After

Width:  |  Height:  |  Size: 391 KiB

View 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.VerticalGridFragment"
android:id="@+id/vertical_grid_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

View File

@@ -0,0 +1,29 @@
/*
* 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 VerticalGridActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.vertical_grid);
getWindow().setBackgroundDrawableResource(R.drawable.grid_bg);
}
}

View File

@@ -0,0 +1,111 @@
/*
* 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.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v17.leanback.widget.ArrayObjectAdapter;
import android.support.v17.leanback.widget.Presenter;
import android.support.v17.leanback.widget.VerticalGridPresenter;
import android.support.v17.leanback.widget.Row;
import android.support.v17.leanback.widget.OnItemClickedListener;
import android.support.v17.leanback.widget.OnItemSelectedListener;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.Random;
public class VerticalGridFragment extends android.support.v17.leanback.app.VerticalGridFragment {
private static final String TAG = "leanback.VerticalGridFragment";
private static final int NUM_COLUMNS = 3;
private static final int NUM_ITEMS = 50;
private static final int HEIGHT = 200;
private ArrayObjectAdapter mAdapter;
private Random mRandom;
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "onCreate");
super.onCreate(savedInstanceState);
mRandom = new Random();
Params p = new Params();
p.setBadgeImage(getActivity().getResources().getDrawable(R.drawable.ic_title));
p.setTitle("Leanback Vertical Grid Demo");
setParams(p);
setupFragment();
}
private void setupFragment() {
VerticalGridPresenter gridPresenter = new VerticalGridPresenter();
gridPresenter.setNumberOfColumns(NUM_COLUMNS);
setGridPresenter(gridPresenter);
mAdapter = new ArrayObjectAdapter(new GridItemPresenter());
for (int i = 0; i < NUM_ITEMS; i++) {
mAdapter.add(new MyItem(i));
}
setAdapter(mAdapter);
setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(Object item, Row row) {
Log.i(TAG, "item selected: " + ((MyItem) item).id);
}
});
setOnItemClickedListener(new OnItemClickedListener() {
@Override
public void onItemClicked(Object item, Row row) {
Log.i(TAG, "item clicked: " + ((MyItem) item).id);
}
});
}
private class GridItemPresenter extends Presenter {
public ViewHolder onCreateViewHolder(ViewGroup parent) {
TextView view = new TextView(parent.getContext());
// Choose a random height between HEIGHT and 1.5 * HEIGHT to
// demonstrate the staggered nature of the grid.
final int height = HEIGHT + mRandom.nextInt(HEIGHT / 2);
view.setLayoutParams(new ViewGroup.LayoutParams(200, height));
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.setBackgroundColor(Color.DKGRAY);
view.setGravity(Gravity.CENTER);
return new ViewHolder(view);
}
public void onBindViewHolder(ViewHolder viewHolder, Object item) {
((TextView) viewHolder.view).setText(Integer.toString(((MyItem) item).id));
}
public void onUnbindViewHolder(ViewHolder viewHolder) {}
}
static class MyItem {
int id;
MyItem(int id) {
this.id = id;
}
}
}