Adding ProgressBar to DetailsOverviewRow on Android TV

痴心易碎 提交于 2019-12-10 18:15:51

问题


I'm trying to create a slightly modified version of the android.support.v17.leanback.widget.DetailsOverviewRow for an Android TV app. The layout I'm trying to accomplish is roughly the same as Google's default version, but with the addition of a progress bar above the actions panel. What I have now looks like this:

Current Version

What I want to create is something roughly like this:

Goal Version

I would like to add this in dynamically without having to create an entirely new layout from scratch if possible. Here's the code I'm using currently, slightly modified from the default Android TV app in Android Studio:

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);
        }
    }
}


}

Is there a simple way to add the ProgressBar or do I need to create a completely new xml layout to do this?


回答1:


I figured out a way to do it. I ended up creating a modified version of lb_details_overview.xml, which is used by the support.v17.leanback library when creating a DetailOverviewRow. I basically just added a RelativeLayout with a ProgressBar. I also had to make a modified version of DetailsOverviewRowPresenter.java in the leanback library in order to inflate the new xml layout instead of the existing one. It might not be the best or the easiest way to do this, but it works.



来源:https://stackoverflow.com/questions/30653555/adding-progressbar-to-detailsoverviewrow-on-android-tv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!