3

我正在尝试为 Android TV 应用创建 android.support.v17.leanback.widget.DetailsOverviewRow 的略微修改版本。我试图完成的布局与谷歌的默认版本大致相同,但在操作面板上方增加了一个进度条。我现在的样子是这样的:

当前版本

我想要创建的内容大致是这样的:

目标版本

如果可能的话,我想动态添加它,而不必从头开始创建一个全新的布局。这是我目前使用的代码,对 Android Studio 中的默认 Android TV 应用稍作修改:

public class VideoDetailsFragment extends DetailsFragment {
private static final String TAG = "VideoDetailsFragment";

private static final int ACTION_UPDATE = 1;
private static final int ACTION_CHANGELOG = 2;
private static final int ACTION_INSTALLED = 3;

private static final int DETAIL_THUMB_WIDTH = 274;
private static final int DETAIL_THUMB_HEIGHT = 274;

private static final int NUM_COLS = 10;

private static final String UPDATE = "Update";

private Update mSelectedUpdate;

private Drawable mDefaultBackground;
private Target mBackgroundTarget;
private DisplayMetrics mMetrics;
private DetailsOverviewRowPresenter mDorPresenter;
private DetailRowBuilderTask mDetailRowBuilderTask;

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "onCreate DetailsFragment");
    super.onCreate(savedInstanceState);

    mDorPresenter =
            new DetailsOverviewRowPresenter(new DetailsDescriptionPresenter());

    BackgroundManager backgroundManager = BackgroundManager.getInstance(getActivity());
    backgroundManager.attach(getActivity().getWindow());
    mBackgroundTarget = new PicassoBackgroundManagerTarget(backgroundManager);

    mDefaultBackground = getResources().getDrawable(R.drawable.default_background);

    mMetrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(mMetrics);

    List<Update> list = UpdateList.setupMovies();
    mSelectedUpdate = list.get(0);//(Update) getActivity().getIntent().getSerializableExtra(MOVIE);
    mDetailRowBuilderTask = (DetailRowBuilderTask) new DetailRowBuilderTask().execute(list);
    mDorPresenter.setSharedElementEnterTransition(getActivity(),
            DetailsActivity.SHARED_ELEMENT_NAME);

    setOnItemViewClickedListener(new ItemViewClickedListener());

}

@Override
public void onStop() {
    mDetailRowBuilderTask.cancel(true);
    super.onStop();
}

private class DetailRowBuilderTask extends AsyncTask<List<Update>, Integer, List<DetailsOverviewRow>> {
    @Override
    protected List<DetailsOverviewRow> doInBackground(List<Update>... movies) {
        List<DetailsOverviewRow> rows = new ArrayList<DetailsOverviewRow>();

        int exampleUpdate = 0;

        for (Update m: movies[0]) {
            DetailsOverviewRow row = new DetailsOverviewRow(m);
            try {
                //String test = mSelectedUpdate.getCardImageUrl();
                Bitmap poster = Picasso.with(getActivity())
                        .load(m.getCardImageUrl())
                        .resize(Utils.convertDpToPixel(getActivity().getApplicationContext(), DETAIL_THUMB_WIDTH),
                                Utils.convertDpToPixel(getActivity().getApplicationContext(), DETAIL_THUMB_HEIGHT))
                        .centerCrop()
                        .get();
                row.setImageBitmap(getActivity(), poster);
            } catch (IOException e) {
                e.printStackTrace();
                Log.e(TAG, e.toString(), e);
            }

            SparseArrayObjectAdapter adapter = new SparseArrayObjectAdapter();
            if (exampleUpdate++ == 0) {
                Action a1 = new Action(ACTION_UPDATE, getResources().getString(R.string.download));
                Action a2 = new Action(ACTION_CHANGELOG, getResources().getString(R.string.release_notes));
                adapter.set(ACTION_UPDATE, a1);
                adapter.set(ACTION_CHANGELOG, a2);
                row.setActionsAdapter(adapter);
            }else{
                Action a1 = new Action(ACTION_INSTALLED, getResources().getString(
                        R.string.installed));
                Action a2 = new Action(ACTION_CHANGELOG, getResources().getString(R.string.release_notes));
                //ProgressBar p1 = new ProgressBar(getActivity(),null, android.R.attr.progressBarStyleHorizontal);
                adapter.set(1, a1);
                adapter.set(2, a2);
                //adapter.set(3, p1);
                row.setActionsAdapter(adapter);
            }
            rows.add(row);
        }
        return rows;
    }

    @Override
    protected void onPostExecute(List<DetailsOverviewRow> detailRows) {
        ClassPresenterSelector ps = new ClassPresenterSelector();
        // set detail background and style

        mDorPresenter.setBackgroundColor(getResources().getColor(R.color.black_opaque));
        Log.i("TEST", "Setting BG Color of Detail View");
        mDorPresenter.setStyleLarge(true);
        mDorPresenter.setOnActionClickedListener(new OnActionClickedListener() {
            @Override
            public void onActionClicked(Action action) {
                if (action.getId() == ACTION_UPDATE) {
                    /*Intent intent = new Intent(getActivity(), PlaybackOverlayActivity.class);
                    intent.putExtra(getResources().getString(R.string.movie), mSelectedUpdate);
                    intent.putExtra(getResources().getString(R.string.should_start), true);
                    startActivity(intent);*/
                    Toast.makeText(getActivity(), "Download Update", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getActivity(), action.toString(), Toast.LENGTH_SHORT).show();
                }
            }
        });

        ps.addClassPresenter(DetailsOverviewRow.class, mDorPresenter);
        ps.addClassPresenter(ListRow.class,
                new ListRowPresenter());


        ArrayObjectAdapter adapter = new ArrayObjectAdapter(ps);

        for(DetailsOverviewRow detailRow : detailRows){
            adapter.add(detailRow);
        }

        String subcategories[] = {
                getString(R.string.related_movies)
        };

        //HeaderItem header = new HeaderItem(0, subcategories[0]);
        //adapter.add(new ListRow(header, listRowAdapter));

        setAdapter(adapter);
    }

}

private final class ItemViewClickedListener implements OnItemViewClickedListener {
    @Override
    public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
                              RowPresenter.ViewHolder rowViewHolder, Row row) {

        if (item instanceof Update) {
            Update update = (Update) item;
            Log.d(TAG, "Item: " + item.toString());
            Intent intent = new Intent(getActivity(), DetailsActivity.class);
            intent.putExtra(DetailsActivity.MOVIE, update);

            Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(
                    getActivity(),
                    ((ImageCardView) itemViewHolder.view).getMainImageView(),
                    DetailsActivity.SHARED_ELEMENT_NAME).toBundle();
            getActivity().startActivity(intent, bundle);
        }
    }
}


}

有没有一种简单的方法来添加 ProgressBar 或者我需要创建一个全新的 xml 布局来做到这一点?

4

1 回答 1

3

我想出了一个办法。我最终创建了 lb_details_overview.xml 的修改版本,在创建 DetailOverviewRow 时由 support.v17.leanback 库使用。我基本上只是添加了一个带有 ProgressBar 的 RelativeLayout。我还必须在leanback 库中制作DetailsOverviewRowPresenter.java 的修改版本,以便扩展新的xml 布局而不是现有的布局。这可能不是最好或最简单的方法,但它确实有效。

于 2015-06-05T00:21:37.660 回答