将焦点从ViewPager / TabLayout中的选项卡上移开

时间:2018-01-12 19:58:46

标签: android android-fragments android-viewpager android-tablayout fragmentpageradapter

我在Android TV上使用ViewPager / TabLayout,我有2个问题。

1)目前,如果按下DPAD_UP,焦点将转到选项卡,但在按下DPAD_CENTER之前,页面实际上不会更改为与焦点选项卡对应的页面。因此,聚焦(在选项卡文本周围显示为灰色框)和选中(显示为下划线)之间存在差异。我想知道是否可以有任何专注于选择的东西?

2)我的ViewPager中有3个选项卡,其中tab1中的片段需要具有焦点才能正确显示内容。当我从tab3切换到tab1时,焦点不会给予片段,而是保留在制表符中。这会导致我的片段无法正常显示。我想知道是否有办法将焦点从标签上移开?我深入研究了ViewPager和TabLayout的源代码,我知道TabLayout的重点是,但无论我尝试什么,我都无法带走TabView的焦点状态(即标签文本周围的灰色框不会出现直到我使用DPAD_DOWN“向下”到片段)。

尝试((ViewGroup)tabLayout.getChildAt(0))。getChildAt(desiredPosition).clearFocus()并没有帮助。 我不想使用viewPager.setOffscreenPageLimit(2);

// This is my viewpager activity class
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_pager);

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
    adapterViewPager = new MyPagerAdapter(getFragmentManager());
    viewPager.setAdapter(adapterViewPager);

    // Give the TabLayout the ViewPager
    TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
    tabLayout.setupWithViewPager(viewPager);

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            // Tried to clear focus of tab here, didn't help
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}


public static class MyPagerAdapter extends FragmentPagerAdapter {
    private static int NUM_ITEMS = 3;

    public MyPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    // Returns total number of pages.
    @Override
    public int getCount() {
        return NUM_ITEMS;
    }

    // Returns the fragment to display for a particular page.
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new OneFragment; // This is the fragment that needs focus
            case 1:
                return new TwoFragment();
            case 2:
                return new ThreeFragment();
            default:
                return null;
        }
    }

    // Returns the page title for the top indicator
    @Override
    public CharSequence getPageTitle(int position) {
        if (position == 0) {
            return "Fragment 1";
        } else if (position == 1) {
            return "Fragment 2";
        } else {
            return "Fragment 3";
        }
    }

}


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.design.widget.TabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        android:theme="@style/AppThemeTab" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>

</RelativeLayout>

感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

一种衬管解决方案:)。要抑制对Tablayout子视图的关注,请使用以下代码段,假设该选项卡是可滚动的-也就是说,选项卡的选择是通过digipad滚动的:

tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);        
tabLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);