简体   繁体   English

如何使用这样的自定义标签制作Viewpager?

[英]How can I make a Viewpager with custom tabs like this?

How can I achieve a Tab/Viewpager style like this ? 如何实现像这样的Tab / Viewpager样式?

So its not looking like the normal Sliding Tab Layout like here . 所以它看起来不像这里的普通滑动标签布局。

Are there some guides to follow out there ? 那里有一些指南吗? It looks very custom to me 它看起来非常习惯

在此输入图像描述

First define the slidingTabLayout with your view pager as: 首先使用您的视图寻呼机将slidingTabLayout定义为:

  <com.forthcode.androidoze.Utils.SlidingTabLayout
    android:id="@+id/sliding_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFFFF"/>

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

</android.support.v4.view.ViewPager>

In above code I have taken the sliding tab color as transparent, you can take any as per your need. 在上面的代码中,我将滑动标签颜色视为透明,您可以根据需要选择。 Now define a custom view thatrepresent your single tab. 现在定义一个代表单个标签的自定义视图。

customtab.xml customtab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:padding="10dp">
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:layout_gravity="center"
    android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>

Now create a selector.xml inside the drawable folder that defines the text color of the tab when it will be selected and not selected. 现在在drawable文件夹中创建一个selector.xml,它定义选项卡的文本颜色,而不是选中它。

selector.xml selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#ccc" />
<item android:state_focused="true" android:color="#ccc" />
<item android:state_pressed="true" android:color="#ccc" />
<item android:color="#fff" />
</selector>

In the last line of selector.xml is the default tab text color when the tab is not selected. 在SELECT.xml的最后一行是未选中选项卡时的默认选项卡文本颜色。

Now finally in your void populateTabStrip() method of SlidingTabLayout class just add the code to implement the selector with the tab as shown below: 现在最后在SlidingTabLayout类的void populateTabStrip()方法中添加代码以使用选项卡实现选择器,如下所示:

tabTitleView.setTextColor(getResources().getColorStateList(R.drawable.selector));

Remember this line should be inside the populateTabStrip() inside the for loop, just to make it easy i'm writing the complete method populateTabStrip(){} of slidingTabLayout, so your populateTabStrip() should look like this: 记住这行应该在for循环中的populateTabStrip()内部,只是为了让我很容易编写slidingTabLayout的完整方法populateTabStrip(){},所以你的populateTabStrip()应该如下所示:

private void populateTabStrip() {
    final PagerAdapter adapter = mViewPager.getAdapter();
    final OnClickListener tabClickListener = new TabClickListener();



    for (int i = 0; i < adapter.getCount(); i++) {
        View tabView = null;
        TextView tabTitleView = null;

        if (mTabViewLayoutId != 0) {
            // If there is a custom tab view layout id set, try and inflate it
            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(adapter.getPageTitle(i));
        tabView.setOnClickListener(tabClickListener);
                    tabTitleView.setTextColor(getResources().getColorStateList(R.drawable.selector));
        String desc = mContentDescriptions.get(i, null);
        if (desc != null) {
            tabView.setContentDescription(desc);
        }

        mTabStrip.addView(tabView);
        if (i == mViewPager.getCurrentItem()) {
            tabView.setSelected(true);
        }
    }
}

Now in the Activity or Fragment where you are using the tab define the viewpager,slidingtablayout and the viewpager adapter, and set the adapter with view pager and set the slidingtab with view pager as shown below(Note i have used inside fragment, if you are using Activity then modify as per your need): 现在在您使用选项卡的Activity或Fragment中定义viewpager,slidingtablayout和viewpager适配器,并使用view pager设置适配器并使用view pager设置slidingtab,如下所示(注意我使用了内部片段,如果你是使用Activity然后根据需要修改):

    SlidingTabLayout mSlidingTabLayout;
ViewPager mViewPager;
PagerAdapter mPagerAdapter;
SharedPreferences mySharedpref;

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_home, container, false);

    mPagerAdapter=new PagerAdapter(getChildFragmentManager());
    mViewPager= (ViewPager) view.findViewById(R.id.vpPager);
    mSlidingTabLayout= (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
    mViewPager.setAdapter(mPagerAdapter);
    mSlidingTabLayout.setCustomTabView(R.layout.customtab, R.id.textView1);
    mSlidingTabLayout.setSelectedIndicatorColors(R.color.tabIndicator);
    mSlidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {//change the color of the tab indcator

        @Override
        public int getIndicatorColor(int position) {
            // TODO Auto-generated method stub
            return getResources().getColor(R.color.tabIndicator);
        }
    });

    mSlidingTabLayout.setViewPager(mViewPager);
    mViewPager.setCurrentItem(tabPosition);
return view;
}

This should work fine. 这应该工作正常。 if you find any issue then comment, i will try to reply 如果您发现任何问题然后发表评论,我会尽力回复

You can have a top layout in your activity, in top of the view pager. 您可以在视图分页器顶部的活动中使用顶部布局。 In the OnPageSelected of the ViewPager you can change the text color of the top layout to simulate tab selection 在ViewPager的OnPageSelected中,您可以更改顶部布局的文本颜色以模拟选项卡选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM