Bhavik Mehta
Bhavik Mehta

Reputation: 499

tabIndicator is not showing up properly(it is showing in the reverse way)

I have added tab layout in my activity.Everything is working fine, but the tab Indicators are not showing up as expected. I am adding the photo below so that you can get an idea.I mean to say that when I click on Overview(tab indicator is shown below Detail fragment) and vice versa.

click to see the image

Given below is the XML code(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 Code(Inventory_deatil_Activity.java) is given below(can not post full code as it is having many lines), only adding Tablayout and the related part. This is code in which I declared the Tablayout .

public class Inventory_detail_Activity extends AbstractKActivity implements 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;  

this is where I have initialized the 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) {

        }
    });  

Given below is the code for 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();

    }  

[Edited]:

this is the AbstractActivity code:

public class AbstractKActivity extends 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);
}

}

Upvotes: 1

Views: 271

Answers (1)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

At first Modify your CODE. Don't return null; . You should return your Default Fragment .

Don't

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

Do

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

Secondly

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

EDIT

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

Upvotes: 1

Related Questions