在Android TV上将ProgressBar添加到DetailsOverviewRow

时间:2015-06-04 20:30:15

标签: android android-layout android-tv leanback

我正在尝试为Android TV应用创建一个稍微修改过的android.support.v17.leanback.widget.DetailsOverviewRow版本。我想要完成的布局与Google的默认版本大致相同,但在操作面板上方添加了一个进度条。我现在看起来像这样:

Current Version

我想要创建的内容大致如下:

Goal Version

我想动态添加它,如果可能的话,无需从头开始创建全新的布局。这是我目前正在使用的代码,稍微修改了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布局才能执行此操作?

1 个答案:

答案 0 :(得分:2)

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