how to always show headers in RowsFragment
Asked Answered
B

2

8

I'm trying to create an Android tv application similar to the Youtube application. and I am using the Sofa library to do that. Now the problem comes when I am trying to show the headers for individual items in the RowsFragment even when the RowsFragment doesn't have a focus. following is the code for loading the dataset into browseFragment

private void loadRowsCustom() {
        adapter = new ArrayObjectAdapter();

        int split = 3;
        int rowsFragmentCount = videoList.size() / split + (videoList.size() % split != 0 ? 1 : 0);

        for (int i = 0; i < rowsFragmentCount; i++) {

            ListRowPresenter lrp = new ListRowPresenter(FocusHighlight.ZOOM_FACTOR_LARGE);
            ArrayObjectAdapter rowsAdapter = new ArrayObjectAdapter(lrp);

            String category = "Category";

            for (int j = 0; j < split && (i * split + j) < videoList.size(); j++) {
                Video[] videos = videoList.get(i * split + j);
                ArrayObjectAdapter rowCollAdapter = new ArrayObjectAdapter(new VideoPresenter());
                rowCollAdapter.addAll(0, Arrays.asList(videos));
                ListRow row = new ListRow(new HeaderItem(videos[0].category),rowCollAdapter);
                rowsAdapter.add(row);
                category = videos[0].category;
            }

            ArrayObjectAdapter fragmentAdapter = new ArrayObjectAdapter();
            RowsSupportFragment rowsSupportFragment = new RowsSupportFragment();
            rowsSupportFragment.setAdapter(rowsAdapter);
            rowsSupportFragment.enableRowScaling(false);
            fragmentAdapter.add(rowsSupportFragment);
            adapter.add(new ListRow(new HeaderItem(category), fragmentAdapter));
        }

        browseSupportFragment.setAdapter(adapter);
        browseSupportFragment.setHeadersState(BrowseSupportFragment.HEADERS_ENABLED);
        browseSupportFragment.setOnSearchClickedListener(searchClickListener);
        browseSupportFragment.setOnItemViewClickedListener(browseClickListener);
        browseSupportFragment.setTitle("Google Videos");
    }

The required and the current implementation is described as in the screenshots below:

enter image description here

enter image description here

Bakeman answered 2/2, 2016 at 11:5 Comment(0)
A
6

As Mayur Kaul already mentioned, u just need to call

mRowsFragment.setExpand(true)

But in this case everytime when onAttachedToWindow(...) method would be called (see android.support.v17.leanback.app.RowsFragment - ItemBridgeAdapter.AdapterListener mBridgeAdapterListener), your header titles anyway would be hidden.

So final solution, witch worked for me it's just simply override setExpand() method in your RowsFragment implementation like below:

@Override
    public void setExpand(boolean expand) {
        super.setExpand(true);
    }
Acceptation answered 27/5, 2017 at 12:18 Comment(1)
For those whose eyes glazed over this like mine did before reading it properly - this isn't just a pointless override, true is always passed to the superclass' setExpand method. This should be marked as the correct answer.Ceasar
B
2

For those who are facing a similar problem, i finally managed to find a hack which will give the desired results. But for that you might need to get the source of BrowseSupportFragment or in Sofa and modify the following line of code. I know its a bit of hack, but it get's the desired output.

For BrowseFragment: Notice the mRowsFragment.setExpand(true)

private void showHeaders(boolean show) {
        if (DEBUG) Log.v(TAG, "showHeaders " + show);
        mHeadersFragment.setHeadersEnabled(show);
        setHeadersOnScreen(show);
        setRowsAlignedLeft(!show);
        if (mRowsFragment != null) {
            mRowsFragment.setExpand(true);
        } else if (mCurrentFragment != null && mCurrentFragment instanceof RowsFragment) {
            ((RowsFragment) mCurrentFragment).setExpand(true);
        }
    }

and same goes for the BrowseSupportFragment as well

private void showHeaders(boolean show) {
        if (DEBUG) Log.v(TAG, "showHeaders " + show);
        mHeadersSupportFragment.setHeadersEnabled(show);
        setHeadersOnScreen(show);
        setRowsAlignedLeft(!show);
        if (mRowsSupportFragment != null) {
            mRowsSupportFragment.setExpand(true);
        } else if (mCurrentFragment != null && mCurrentFragment instanceof RowsSupportFragment) {
            ((RowsSupportFragment) mCurrentFragment).setExpand(true);
        }
    }
Bakeman answered 3/2, 2016 at 10:4 Comment(3)
how you have solved this problem ? please can u explain because i want to do similar thing in my TV app.Panhellenism
It's not working in my case.. Can you put your code on Github?Incision
as the method is package-private and not accessible, how can we use it?Trilbi

© 2022 - 2024 — McMap. All rights reserved.