1

我正在为Android TV. 在应用程序中有一个片段布局,它ImageViewLinearLayout水平方向上有四个,一个RecyclerViewLinearLayout. 当我启动应用程序并通过DPAD控件RecyclerView导航时,然后向上和向下导航。要求是当片段启动时默认选择第ImageView一个LinearLayout。当我们按下 DPAD 的右键后,下一步ImageView将被选中。当我们按下键时,焦点将继续移动RecyclerView。当焦点将放在RecyclerView第一个孩子上时,我们按向上键,然后焦点移到第Imageview一个LinearLayout

我也使用了 imageview.requestFocus() 但它不起作用。请帮我。

下面是我的布局文件

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout 
       android:id="@+id/home_fragment_menu_container"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:layout_marginLeft="30dp"
       android:layout_marginRight="30dp"
       android:layout_marginBottom="10dp">


       <ImageView 
           android:id="@+id/img_playall"
           style="@style/home_fragment_menu"
           android:src="@drawable/play_all"
           android:focusable="true"
           android:focusableInTouchMode="true"/>

       <ImageView 
           android:id="@+id/img_trending"
           style="@style/home_fragment_menu"
           android:src="@drawable/trending"/>

       <ImageView 
           android:id="@+id/img_search"
           style="@style/home_fragment_menu"
           android:src="@drawable/search"/>

       <ImageView 
           android:id="@+id/img_setting"
           style="@style/home_fragment_menu"
           android:src="@drawable/settings"/>    
   </LinearLayout>


   <LinearLayout 
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_below="@+id/home_fragment_menu_container"
       android:layout_marginLeft="45dp"
       >

     <android.support.v7.widget.RecyclerView
           android:id="@+id/vertical_recyclerview"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           />

   </LinearLayout>
</RelativeLayout>

下面是 fragment.java 文件

public class HomeFragment extends Fragment {

    private static final String TAG = "HomeFragment";

    ImageView playAllImage, trendingImage, searchImage, settingImage;



    Context context;
    RecyclerView verticalList;
    ArrayList<ArrayList<VideoContentModel>> categoriesVideoList;
    ArrayList<CategoryModel> categoryDataList;

    public static HomeFragment newInstance(Context context, ArrayList<ArrayList<VideoContentModel>> categoriesVideoList,
            ArrayList<CategoryModel> categoryDataList){

        HomeFragment fragment = new HomeFragment();
        fragment.context = context;
        fragment.categoriesVideoList = categoriesVideoList;
        fragment.categoryDataList = categoryDataList;

        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment_layout, container, false);

        playAllImage = (ImageView)view.findViewById(R.id.img_playall);
        trendingImage = (ImageView)view.findViewById(R.id.img_trending);
        searchImage = (ImageView)view.findViewById(R.id.img_search);
        settingImage = (ImageView)view.findViewById(R.id.img_setting);

        playAllImage.requestFocus();


        verticalList = (RecyclerView)view.findViewById(R.id.vertical_recyclerview);
        verticalList.setHasFixedSize(true);
        verticalList.setLayoutManager(new LinearLayoutManager(context));
        CatListVerticalAdapter adapter = new CatListVerticalAdapter(context,categoriesVideoList,categoryDataList);
        verticalList.setAdapter(adapter);

        verticalList.clearFocus();



        //


        if(playAllImage.hasFocus())
            Log.i(TAG,"play all has focus");
        else
            Log.i(TAG,"===playall has not focus");

        return view;
    }

我们如何通过上述要求的 DPAD 导航?请帮我..

4

0 回答 0