tabIndicator没有正确显示(它以相反的方式显示)

时间:2017-08-08 08:57:56

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

我在我的活动中添加了标签布局。一切正常,但标签指示器没有按预期显示。我正在添加下面的照片,以便您可以获得一个想法。我的意思是说,当我点击Overview(标签指示器显示在详细信息片段下方)时,反之亦然。

click to see the image

以下是XML代码(Activity_inventory_detail):

14:01:32 [http-bio-8085-exec-7] UsernamePasswordAuthenticationFilter - An internal error occurred while trying to authenticate the user.
org.springframework.security.authentication.InternalAuthenticationServiceException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903A8, comment: AcceptSecurityContext error, data 52e, v1db1

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

public class Inventory_detail_Activity extends AbstractKActivity实现了DetailsDataListener,View.OnClickListener,                                 SimpleGestureFilter.SimpleGestureListener,                                 ContentManager.PickContentListener {

<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>  

这是我初始化Tablayout的地方。

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;  

以下是ViewPagerAdapter的代码

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) {

        }
    });  

[已编辑]:

这是AbstractActivity代码:

公共类AbstractKActivity扩展了AppCompatActivity {

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();

    }  

}

1 个答案:

答案 0 :(得分:1)

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

<强>唐&#39;吨

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();
        }