如何在我的应用中获得Android垂直网格视图?

时间:2016-11-27 13:38:31

标签: android android-layout android-fragments gridview

我有一个Android应用程序,其内容显示在屏幕上的"网格"。

然而,当添加动态内容时,它会将其放在一个不是我想要的长行中。

我在此链接(http://corochann.com/verticalgridfragment-android-tv-application-hands-on-tutorial-19-718.html)中尝试了该教程,但我无法使其正常工作。

我需要帮助了解如何使VerticalGrid viw功能正常工作(我真的想让它工作并学习它以便我可以提高我的Android技能)。

我从MainFragment中包含了我的代码,因为这基本上是它的布局。

public class MainFragment extends BrowseFragment {
    private static final String TAG = MainFragment.class.getSimpleName();

    private ArrayObjectAdapter mRowsAdapter;
    private static final int GRID_ITEM_WIDTH = 300;
    private static final int GRID_ITEM_HEIGHT = 200;

    private static PicassoBackgroundManager picassoBackgroundManager = null;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.i(TAG, "onActivityCreated");
        super.onActivityCreated(savedInstanceState);
        setupUIElements();
        loadRows();
        setupEventListeners();
        picassoBackgroundManager = new PicassoBackgroundManager(getActivity());
        picassoBackgroundManager.updateBackgroundWithDelay("http://darutawheed.co.uk/wp-content/uploads/2014/04/masjid-an-nabawi-the-prophets-mosque-madina.jpg");
    }

    private void setupEventListeners() {
        setOnItemViewSelectedListener(new ItemViewSelectedListener());
        setOnItemViewClickedListener(new ItemViewClickedListener());
    }

    public final class ItemViewClickedListener implements OnItemViewClickedListener {
        @Override
        public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
                                  RowPresenter.ViewHolder rowViewHolder, Row row) {
            // each time the item is clicked, code inside here will be executed.
            if (item instanceof Movie) {
                Movie movie = (Movie) item;
                Log.d(TAG, "Item: " + item.toString());
                Intent intent = new Intent(getActivity(), DetailsActivity.class);
                intent.putExtra(DetailsActivity.MOVIE, movie);

                getActivity().startActivity(intent);
            }
            else if (item instanceof Radio) {
            Radio radio = (Radio) item;
            Log.d(TAG, "Item: " + item.toString());
            Intent intent = new Intent(getActivity(), DetailsActivity.class);
            intent.putExtra(DetailsActivity.RADIO, radio);
            getActivity().startActivity(intent);
        }
        }
    }

    public final class ItemViewSelectedListener implements OnItemViewSelectedListener {
        @Override
        public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
                                   RowPresenter.ViewHolder rowViewHolder, Row row) {
            // each time the item is selected, code inside here will be executed.
            if (item instanceof String) {                    // GridItemPresenter
                picassoBackgroundManager.updateBackgroundWithDelay("http://darutawheed.co.uk/wp-content/uploads/2014/04/masjid-an-nabawi-the-prophets-mosque-madina.jpg");
            } else if (item instanceof Movie) {              // CardPresenter
                picassoBackgroundManager.updateBackgroundWithDelay(((Movie) item).getCardImageUrl());
            } else if (item instanceof Radio){
                picassoBackgroundManager.updateBackgroundWithDelay(((Radio) item).getCardImageUrl());
            }
        }
    }

    private void setupUIElements() {
        // setBadgeDrawable(getActivity().getResources().getDrawable(R.drawable.videos_by_google_banner));
        setTitle("Sunnah Box TV"); // Badge, when set, takes precedent
        // over title
        setHeadersState(HEADERS_ENABLED);
        setHeadersTransitionOnBackEnabled(true);

        // set fastLane (or headers) background color
        setBrandColor(getResources().getColor(R.color.fastlane_background));
        // set search icon color
        setSearchAffordanceColor(getResources().getColor(R.color.search_opaque));
    }

    private void loadRows() {
        mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());

        /* GridItemPresenter */
        HeaderItem gridItemPresenterHeader = new HeaderItem(0, "Channel Categories");

        GridItemPresenter mGridPresenter = new GridItemPresenter();
        ArrayObjectAdapter gridRowAdapter = new ArrayObjectAdapter(mGridPresenter);
        gridRowAdapter.add("Video Lectures");
        gridRowAdapter.add("Audio Lectures");
        gridRowAdapter.add("Special Broadcasts");
        mRowsAdapter.add(new ListRow(gridItemPresenterHeader, gridRowAdapter));

        /* CardPresenter */
        HeaderItem cardPresenterHeader = new HeaderItem(1, "Video Content");
        CardPresenter cardPresenter = new CardPresenter();
        ArrayObjectAdapter videoCardRowAdapter = new ArrayObjectAdapter(cardPresenter);

        /* CardPresenter */
        HeaderItem cardPresenterHeader2 = new HeaderItem(2, "Audio Lectures");
        CardPresenter cardPresenter2 = new CardPresenter();
        ArrayObjectAdapter audioCardRowAdapter = new ArrayObjectAdapter(cardPresenter2);




        ArrayList<Movie> mItems = MovieProvider.getMovieItems();
        for (Movie movie : mItems) {
            videoCardRowAdapter.add(movie);
        }


        ArrayList<Radio> rItems = RadioProvider.getRadioItems();
        for (Radio radio : rItems) {
            audioCardRowAdapter.add(radio);
        }

        try {
            Playlist[] resp = new InformationRetrieverTask().execute("https://xxxxxx.xxxxxx.xxxxx").get();
            List<Playlist> playlists = Arrays.asList(resp);
            for (Playlist playlist: playlists){
                for (Media media: playlist.getMedias()) {
                    if (media instanceof Audio){
                        Radio radio = new Radio();
                        radio.setTitle(media.getTitle());
                        radio.setStudio(media.getPublicationDate());
                        radio.setDescription(media.getDescription());
                        radio.setCardImageUrl("https://masjidtawheed.net/wp-content/uploads/2016/11/masjidlogo1.png");
                        radio.setRadioUrl(media.getMediaUrl());
                        audioCardRowAdapter.add(radio);
                    }else if (media instanceof Video){
                        Movie movie = new Movie();
                        movie.setTitle(media.getTitle());
                        movie.setStudio(media.getPublicationDate());
                        movie.setDescription(media.getDescription());
                        movie.setCardImageUrl("http://thoriqussalaf.com/wp-content/uploads/2015/10/logo_Thariqussalaf_OK.jpg");
                        movie.setVideoUrl(media.getMediaUrl());
                        movie.setyTubeID(((Video) media).getVideoProvider().getVideoId());
                        videoCardRowAdapter.add(movie);
                    }
                }
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


        mRowsAdapter.add(new ListRow(cardPresenterHeader, videoCardRowAdapter));
        mRowsAdapter.add(new ListRow(cardPresenterHeader2, audioCardRowAdapter));

        /* Set */
        setAdapter(mRowsAdapter);
    }

    /**
     * from AOSP sample source code
     * GridItemPresenter class. Show TextView with item type String.
     */
    private class GridItemPresenter extends Presenter {
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent) {
            TextView view = new TextView(parent.getContext());
            view.setLayoutParams(new ViewGroup.LayoutParams(GRID_ITEM_WIDTH, GRID_ITEM_HEIGHT));
            view.setFocusable(true);
            view.setFocusableInTouchMode(true);
            view.setBackgroundColor(getResources().getColor(R.color.default_background));
            view.setTextColor(Color.WHITE);
            view.setGravity(Gravity.CENTER);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder viewHolder, Object item) {
            ((TextView) viewHolder.view).setText((String) item);
        }

        @Override
        public void onUnbindViewHolder(ViewHolder viewHolder) {

        }
    }

}

感谢。

ironmantis7x

1 个答案:

答案 0 :(得分:0)

我会搜索一个解决方法,因为我没有得到回复。