我有一个电视应用程序。我看到频道列表,我就像一排。但现在我想把它重写成一个网格。我正在尝试设置网格展示器,但它不起作用它说我必须实现MainFragmentAdapterProvider。并且教程中的所有提示也不起作用,我无法弄清楚什么是问题。也许还有另一位主持人呢?
public class ChannelsFragment extends RowsFragment implements OnItemViewClickedListener {
private ArrayObjectAdapter mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
//private ArrayObjectAdapter mRowsAdapter;
private IServiceControl mServiceControl;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mServiceControl = ((A4TVTestApp) getActivity().getApplication()).getServiceControl();
setAdapter(mRowsAdapter);
setOnItemViewClickedListener(this);
createRows();
}
private void createRows() {
// VerticalGridPresenter presenter = new VerticalGridPresenter();
// presenter.setNumberOfColumns(4);
// setGridPresenter(presenter);
// setTitle(getString(R.string.scanned_channels));
//
// setOnItemViewClickedListener(this);
//
// try {
// for (int i = 0; i < mServiceControl.getServiceListCount(0); i++) {
// mRowsAdapter.add(mServiceControl.getServiceDescriptor(0, i).getName());
// }
// } catch (RemoteException e) {
// e.printStackTrace();
// }
ChannelsRowItemPresenter presenter = new ChannelsRowItemPresenter();
ArrayObjectAdapter firstRowAdapter = new ArrayObjectAdapter(presenter);
ArrayObjectAdapter secondRowAdapter = new ArrayObjectAdapter(presenter);
ArrayObjectAdapter thirdRowAdapter = new ArrayObjectAdapter(presenter);
ArrayObjectAdapter fourthRowAdapter = new ArrayObjectAdapter(presenter);
try {
int count = mServiceControl.getServiceListCount(0);
for (int i = 0; i < count; i += 4) {
firstRowAdapter.add(mServiceControl.getServiceDescriptor(0, i));
secondRowAdapter.add(mServiceControl.getServiceDescriptor(0, i + 1));
thirdRowAdapter.add(mServiceControl.getServiceDescriptor(0, i + 2));
fourthRowAdapter.add(mServiceControl.getServiceDescriptor(0, i + 3));
}
} catch (RemoteException e) {
e.printStackTrace();
}
mRowsAdapter.add(new ListRow(new HeaderItem(getString(R.string.scanned_channels)),
firstRowAdapter));
mRowsAdapter.add(new ListRow(secondRowAdapter));
mRowsAdapter.add(new ListRow(thirdRowAdapter));
mRowsAdapter.add(new ListRow(fourthRowAdapter));
}
@Override
public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
RowPresenter.ViewHolder rowViewHolder, Row row) {
ServiceDescriptor serviceDescriptor = (ServiceDescriptor) item;
ChannelActivity.startActivity(getActivity(), serviceDescriptor.getLCN());
}
}
答案 0 :(得分:1)
您是否尝试过扩展VerticalGridFragment?
leanback样本中有一个例子: https://github.com/googlesamples/androidtv-Leanback/blob/master/app/src/main/java/com/example/android/tvleanback/ui/VerticalGridFragment.java
您的实施应类似于以下内容。 MyPresenter
应该是Presenter的子类,例如leanback中的CardPresenter,它通过扩展BaseCardView来委托/管理像ImageCardView或您的自定义卡视图这样的视图。如果您有更多动态内容,请改为使用PresenterSelector,以便每张卡可以不同。
public class ChannelFragment extends android.support.v17.leanback.app.VerticalGridFragment
implements OnItemViewClickedListener {
private static final int NUM_COLUMNS = 5;
private final ArrayObjectAdapter mRowsAdapter;
private IServiceControl mServiceControl;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mRowsAdapter = new ArrayObjectAdapter(new MyPresenter());
mServiceControl = ((A4TVTestApp) getActivity().getApplication()).getServiceControl();
setAdapter(mRowsAdapter);
setOnItemViewClickedListener(this);
createRows();
}
private void createRows() {
VerticalGridPresenter gridPresenter = new VerticalGridPresenter();
gridPresenter.setNumberOfColumns(NUM_COLUMNS);
// This is the method that will help you get a grid layout.
setGridPresenter(gridPresenter);
setOnItemViewClickedListener(this);
try {
for (int i = 0; i < mServiceControl.getServiceListCount(0); i++) {
mRowsAdapter.add(i);
}
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
RowPresenter.ViewHolder rowViewHolder, Row row) {
ChannelActivity.startActivity(getActivity(), (int) item);
}
}
答案 1 :(得分:1)
还有一个method on ListRowAdapter可以启用每行多行。
ListRowPresenter presenter = new ListRowPresenter()
presenter.setNumRows(4)
ArrayObjectAdapter mRowsAdapter = new ArrayObjectAdapter(presenter);
这比使用VerticalGridFragment更好,具体取决于您是否仍需要类别标题。