带有 D-PAD 的异构列导航的二维列表(网格)

Two dimensional list(Grid) with heterogeneous column navigation with D-PAD

我想创建一个二维网格,其中每一行都有不同的列表计数,这意味着第 1 行有 n1 个项目,第 2 行有 n2 个,第 3 行有 n3 个等等....

这是一个电视应用,因此可以通过方向键导航到每个项目。

我试过嵌套的recyclerview(垂直recyclerview 内的水平Recyclerview),但是当我尝试刷新数据时它会导致焦点问题。 任何帮助将不胜感激。

谢谢

package com.fusioni.demo;

import java.util.ArrayList;

import com.calkins.edutopia.MyStaticClass;
import com.calkins.edutopia.R;
import com.calkins.edutopia.SearchAvtivity;
import com.calkins.edutopia.SettingPageActivity;
import com.calkins.edutopia.TrendActivity;
import com.calkins.edutopia.UtilityClass;
import com.calkins.edutopia.VideoPlayingPageVisualOn;
import com.calkins.edutopia.WebTrendsMethods;
import com.fusioni.model.CategoryModel;
import com.fusioni.model.MainMenuModel;
import com.fusioni.model.VideoContentModel;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

public class DemoVerticalRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_ITEM = 1;

    private Activity context;
    private ArrayList<ArrayList<VideoContentModel>> categoriesVideoList;
    private ArrayList<CategoryModel> categoryDataList;
    private ArrayList<MainMenuModel> mainMenuDataList;
    private UtilityClass utilityClass;
    private HorizontalThumnailAdapter adapter;
    private CategoryModel model;
    // private LinearLayoutManager layoutManager;
    private int fixed_position = 0;
    private com.calkins.imageloder.ImageLoader imageLoader;
    private WebTrendsMethods webtrendsMethod;

    public DemoVerticalRecyclerAdapter(Activity context, ArrayList<ArrayList<VideoContentModel>> categoriesVideoList,
            ArrayList<CategoryModel> categoryDataList, ArrayList<MainMenuModel> mainMenuDataList) {
        super();
        this.context = context;
        this.categoriesVideoList = categoriesVideoList;
        this.categoryDataList = categoryDataList;
        this.mainMenuDataList = mainMenuDataList;
        utilityClass = new UtilityClass(context);
        imageLoader = new com.calkins.imageloder.ImageLoader(context, 156, 1);
        webtrendsMethod = new WebTrendsMethods(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cat_name_vertical_row, parent, false);
            parent.getChildCount();
            return new VHItem(view, parent.getChildCount());
        } else if (viewType == TYPE_HEADER) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.vertical_list_header, parent, false);
            return new VHHeader(view);
        }

        throw new RuntimeException(
                "there is no type that matches the type " + viewType + " + make sure your using types correctly");
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof VHItem) {
            VHItem item = (VHItem) holder;
            CategoryModel model = categoryDataList.get(position - 1);
            item.cateTitleName.setText(model.getCatTitle());
            item.cateTitleName.setTypeface(utilityClass.gotham_medium);
            item.recyclerView.setTag(position + 1);
            fixed_position = position;

            model = categoryDataList.get(position - 1);
            // adapter = new HorizontalThumnailAdapter(context,
            // categoriesVideoList.get((Demo2Activity.globlePosition)),
            // model.getCatDfpTag(), model.getCatTitle());
            adapter = new HorizontalThumnailAdapter(context, categoriesVideoList.get(position - 1),
                    model.getCatDfpTag(), model.getCatTitle(), imageLoader);
            item.recyclerView.setAdapter(adapter);

        } else if (holder instanceof VHHeader) {
            // cast holder to VHHeader and set data for header.
            // VHHeader headerItem=(VHHeader)holder;

        }
        // Log.i("Position BindViewholder", "" + fixed_position + " " +
        // position);

    }

    public void setVideoList(ArrayList<ArrayList<VideoContentModel>> categoriesVideoList, int pos,
            int hpos) {
        this.categoriesVideoList = categoriesVideoList;
        Log.e("Refresh", hpos+"=H==V="+pos);
        notifyItemRangeChanged(0, getItemCount());
    }

    public void setDATAList(ArrayList<CategoryModel> categoryDataList) {
        this.categoryDataList = categoryDataList;
    }


    @Override
    public int getItemCount() {
        return categoryDataList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (isPositionHeader(position))
            return TYPE_HEADER;

        return TYPE_ITEM;
    }

    private boolean isPositionHeader(int position) {
        return position == 0;
    }

    class VHItem extends RecyclerView.ViewHolder implements View.OnKeyListener {
        TextView cateTitleName;
        RecyclerView recyclerView;

        public VHItem(View itemView, int totalItem) {
            super(itemView);

            recyclerView = (RecyclerView) itemView.findViewById(R.id.horizontal_recyclerview);
            cateTitleName = (TextView) itemView.findViewById(R.id.cat_title_name);
            LinearLayoutManager layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(layoutManager);

        }

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            Log.e("onKey"+fixed_position, v+"=H==keyCode="+keyCode);
            return true;
        }

    }

    class VHHeader extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageButton playAllButton, trendingButton, searchButton, settingButton;

        public VHHeader(View itemView) {
            super(itemView);
            playAllButton = (ImageButton) itemView.findViewById(R.id.img_playall);
            trendingButton = (ImageButton) itemView.findViewById(R.id.img_trending);
            searchButton = (ImageButton) itemView.findViewById(R.id.img_search);
            settingButton = (ImageButton) itemView.findViewById(R.id.img_setting);

            playAllButton.setOnClickListener(this);
            trendingButton.setOnClickListener(this);
            searchButton.setOnClickListener(this);
            settingButton.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Intent i = new Intent();
            Bundle b = new Bundle();
            switch (v.getId()) {
            case R.id.img_playall:
                if (utilityClass.checkInternetConnection()) {

                    try {
                        webtrendsMethod.leftMenuSelectWtCall(context.getString(R.string.play_all), "");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    b.putString(MyStaticClass.PLAY_TYPE_VIDEO, MyStaticClass.PLAY_ALL);
                    b.putParcelableArrayList(MyStaticClass.CATEGORY_MODEL_LIST, categoryDataList);
                    b.putParcelable(MyStaticClass.MAIN_MENU_MODEL, mainMenuDataList.get(2));
                    i.putExtras(b);
                    i.setClass(context, VideoPlayingPageVisualOn.class);
                    context.startActivity(i);
                } else {
                    utilityClass.showCustomDialog(context.getString(R.string.internetErrorMsg));
                }

                break;

            case R.id.img_trending:

                try {
                    webtrendsMethod.leftMenuSelectWtCall(context.getString(R.string.trening_small), "");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                b.putParcelable(MyStaticClass.MAIN_MENU_MODEL, mainMenuDataList.get(1));
                b.putString(MyStaticClass.PAGE_TYPE, MyStaticClass.TRENDING_PAGE);
                i.putExtras(b);
                i.setClass(context, TrendActivity.class);

                context.startActivity(i);

                break;

            case R.id.img_search:

                try {
                    webtrendsMethod.leftMenuSelectWtCall(context.getString(R.string.search), "");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                b.putParcelable(MyStaticClass.MAIN_MENU_MODEL, mainMenuDataList.get(3));
                b.putString(MyStaticClass.PAGE_TYPE, MyStaticClass.SEARCH_PAGE);
                i.putExtras(b);
                i.setClass(context, SearchAvtivity.class);
                context.startActivity(i);

                break;

            case R.id.img_setting:

                try {
                    webtrendsMethod.leftMenuSelectWtCall(context.getString(R.string.action_settings), "");
                } catch (Exception e) {
                    e.printStackTrace();
                }

                context.startActivity(new Intent(context, SettingPageActivity.class));

                break;
            }

        }
    }
}

我已经使用 RowsSupportFragment 解决了 Leanback 支持库的问题。