java - tabIndicator 显示不正确(它以相反的方式显示)

标签 java android android-tablayout android-design-library

我在我的 Activity 中添加了选项卡布局。一切正常,但选项卡指示器未按预期显示。我在下面添加照片,以便您了解。我的意思是说,当我单击“概述”时(选项卡指示器显示在“详细信息” fragment 下方),反之亦然。

click to see the image

下面是 XML 代码(Activity_inventory_detail):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_inventory_detail_"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.github.fafaldo.fabtoolbar.widget.FABToolbarLayout
        android:id="@+id/fabtoolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:containerId="@+id/fabtoolbar_container"
        app:fabId="@+id/fabtoolbar_fab"
        app:fabToolbarId="@+id/fabtoolbar_toolbar"
        app:fadeInFraction="0.2"
        app:hideDuration="200"
        app:horizontalMargin="16dp"
        app:showDuration="600"
        app:verticalMargin="16dp">

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:background="@color/colorPrimary"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/white"
            app:tabMaxWidth="0dp"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@color/white"
            app:tabTextColor="@color/white" />

        <com.aaryanapps.hub.ui.controls.LockableViewPager
            android:id="@+id/pager_inventory_detail"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/tab_layout"
            android:background="@color/white"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <RelativeLayout
            android:id="@+id/fabtoolbar_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true">

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fabtoolbar_fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/abc_ic_menu_share_mtrl_alpha"
                app:backgroundTint="@color/colorPrimary"
                app:fabSize="mini" />

        </RelativeLayout>

        <LinearLayout
            android:id="@+id/fabtoolbar_toolbar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal"
            app:backgroundTint="@color/colorPrimary">

            <ImageView
                android:id="@+id/one"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:scaleType="centerInside"
                android:src="@drawable/abc_ic_menu_share_mtrl_alpha" />


            <ImageView
                android:id="@+id/two"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:scaleType="centerInside"
                android:src="@drawable/ic_menu_gallery_white" />


            <ImageView
                android:id="@+id/three"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:backgroundTint="@color/tranperent_white"
                android:scaleType="centerInside"
                android:src="@drawable/ic_menu_camera_white" />

        </LinearLayout>

    </com.github.fafaldo.fabtoolbar.widget.FABToolbarLayout>

</RelativeLayout>  

下面给出了Java代码(Inventory_deatil_Activity.java)(不能贴出完整的代码,因为它有很多行),只添加了Tablayout和相关部分。这是我声明 Tablayout 的代码。

公共(public)类 Inventory_detail_Activity extends AbstractKActivity 实现 DetailsDataListener, View.OnClickListener, SimpleGestureFilter.SimpleGestureListener, ContentManager.PickContentListener {

public static final String TAG_LABEL_OVERVIEW = "Overview";
public static final String TAG_LABEL_DETAILS = "Detail";
InventoryItemsList inventoryItems;
protected int current_item_index = 0;
public List<InventoryItem> inventory_items_list = new ArrayList<>();
InventoryItem inventory_item;
private ViewPager viewPager;
private ViewPagerAdapter pagerAdapter;
TabLayout tabLayout;  

这是我初始化 Tablayout 的地方。

protected void init() {
 final TabLayout tabLayout = (TabLayout)findViewById(R.id.tab_layout);

 viewPager = (ViewPager) findViewById(R.id.pager_inventory_detail);
 setupViewPager(viewPager);
 tabLayout.setupWithViewPager(viewPager);

viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

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

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

        }
    });  

下面是ViewPagerAdapter的代码

class ViewPagerAdapter extends SmartFragmentStatePagerAdapter<ItemDetailAbstractFragment> {
        //private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            ItemDetailAbstractFragment caf;
            switch (position) {
                case 0: // Fragment # 0 - This will show FirstFragment
                    caf = new ItemDetailOverviewFragment();
                    break;
                case 1: // Fragment # 0 - This will show FirstFragment different title
                    caf = new ItemDetailDetailsFragment();
                    break;
                default:
                    return null;
            }

            Bundle args = ActivityStateManager.getInstance().getActivityState(getLocalClassName());
            if (args == null) {
                args = new Bundle();
            }

            caf.setFlowItem(inventory_item);

            if(current_item_index == -1) {
                caf.setFlowItem(inventory_item);
                caf.populateData();
            }
            return caf;
        }

        @Override
        public int getCount() {
            return 2;
        }


        public void addFragment(int position, String title) {
            mFragmentTitleList.add(position, title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }

    }

    @Override
    public void onPause() {
        Log.e("DEBUG", "onPause of ClientListTabactivity");
        super.onPause();
//        currentState.putSerializable("flowItem",inventory_item);
//        ActivityStateManager.getInstance().updateActivityState(getLocalClassName(), currentState);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
//        outState.putSerializable("flowItem",inventory_item);
//        ActivityStateManager.getInstance().updateActivityState(getLocalClassName(), outState);

        contentManager.onSaveInstanceState(outState);

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        contentManager.onRestoreInstanceState(savedInstanceState);
    }

    private void setupViewPager(ViewPager viewPager) {

        pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

        pagerAdapter.addFragment(0, TAG_LABEL_OVERVIEW);

        pagerAdapter.addFragment(1, TAG_LABEL_DETAILS);

        viewPager.setAdapter(pagerAdapter);

        //updateFragmentsForFlowItem();

    }  

[已编辑]:

这是 AbstractActivity 代码:

公共(public)类 AbstractKActivity 扩展 AppCompatActivity {

protected int content_view = 0;

protected String mTitle = "";

protected TextView title;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    prepareInit();

    if (content_view != 0) {
        setContentView(content_view);
    }
    initToolbar();


    processIntent();

    init();

}

protected void initToolbar() {
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.custom_actionbar);
    title=(TextView)findViewById(getResources().getIdentifier("action_bar_title", "id", getPackageName()));
    title.setText(mTitle);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
   // getSupportActionBar().setTitle(mTitle);

}


protected void processIntent() {

}

protected void init() {

}

protected void prepareInit() {

}

protected void setToolbarTitleText(String titleText) {
    mTitle = titleText;
    title.setText(titleText);
}

最佳答案

首先修改你的CODE。不要 return null; 。您应该返回您的默认 fragment

不要

case 1: // Fragment # 0 - This will show FirstFragment different title
                caf = new ItemDetailDetailsFragment();
                break;
            default:
                return null;

 @Override
    public Fragment getItem(int position) {
        ItemDetailAbstractFragment caf;
        switch (position) {
            case 0: // Fragment # 0 - This will show FirstFragment
                caf = new ItemDetailOverviewFragment();
                break;
            case 1: // Fragment # 0 - This will show FirstFragment different title
                caf = new ItemDetailDetailsFragment();
                break;
            default:
                return new ItemDetailOverviewFragment();
        }

其次

viewPager = (ViewPager) findViewById(R.id.pager_inventory_detail);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
viewPager.setCurrentItem(0); // Declare this

编辑

 public void onTabSelected(TabLayout.Tab tab) {
            tab.select();
        }

关于java - tabIndicator 显示不正确(它以相反的方式显示),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45563859/

相关文章:

android - 在 TabLayout 中获取自定义 TextView - Android 设计库

java - 类似 "qualified this"但用于访问嵌套类而不是封闭类的成员

java - 订阅时出现 Android Nearby API NullPointerException

java - 使用谷歌应用程序引擎按数据存储中的时间列排序

android - NavigationView 将所选项目设置为已选中

java - 如何调整 tabItem 上的图标大小?

java - java中素数计数器从使用方法改为使用嵌套循环

python - 如何使用 JSON 在服务器的响应中传递图像? Base64?

android - 手势检测器 : why this code is not working?

android - 在 viewpager 上滑动时,选项卡选择器无法在上一个选项卡上单击