Search code examples
androidpagerslidingtabstrip

Android SlidingTabLayout position issue


I am using SlidingTabLayout in my application. And i was made custom tab populate method in this class. And also made custom Listener to get position of tab. But when i click on tab its giving me last position every time. I know that here my mistake. Because i am using for loop to set position. But i do not get how to solve that issue.

My code is here,

public void populateTabStrip(List<StoreType> st) {
    mTabStrip.removeAllViews();
    for (i = 0; i < st.size(); i++) {
        pos = i;
        View tabView = null;
        TextView tabTitleView = null;
        if (mTabViewLayoutId != 0) {
            tabView = LayoutInflater.from(getContext()).inflate(
                    mTabViewLayoutId, mTabStrip, false);
            tabTitleView = (TextView) tabView
                    .findViewById(mTabViewTextViewId);
        }

        if (tabView == null) {
            tabView = createDefaultTabView(getContext());
        }

        if (tabTitleView == null && TextView.class.isInstance(tabView)) {
            tabTitleView = (TextView) tabView;
        }

        if (mDistributeEvenly) {
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tabView
                    .getLayoutParams();
            lp.width = 0;
            lp.weight = 1;
        }

        tabTitleView.setText(st.get(i).getType());
        tabView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CategoryListner.onSelect(pos);
            }
        });
        String desc = mContentDescriptions.get(i, null);
        if (desc != null) {
            tabView.setContentDescription(desc);
        }
        mTabStrip.addView(tabView);

        tabTitleView.setTextColor(getResources()
                .getColorStateList(selector));
        tabTitleView.setTextSize(14);
    }
}

Listerer is below,

 public static interface categorySelctListenr {
    public void onSelect(int i);
}

And Origin code is here

And also i am not using viewpager with tab. I just use tab with below list view. But actual problem is listener. Because i want a position of particular tab. Thank you.


Solution

  • I don't think this is right way to implement SlidingTabLayout with custom view. But for your solution i would fit a jack.

      tabView.setTag(Integet.toString(pos)); //**here i set Tag of positon**
      tabView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int posClicked = Integet.parseInt(v.getTag().toString());// get tag that contain position 
                    CategoryListner.onSelect(posClicked);
                }
            }); 
    

    Hope this will work fine for you.