使用PagerSlidingTabStrip为片段内的许多功能扩展自定义TabLayout

时间:2015-10-28 07:50:26

标签: android android-fragments android-tablayout

我在这里使用TabLayout库在片段中使用viewPager实现com.astuetz.PagerSlidingTabStrip。一切都很好,但现在我想添加自定义Tab和更多功能。

test_tab_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.astuetz.PagerSlidingTabStrip
    android:id="@+id/mainTabLayout"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#111"
    app:pstsDividerColor="@color/text_color"
    app:pstsIndicatorColor="@color/green"
    app:pstsIndicatorHeight="2dp"
    app:pstsShouldExpand="true"
    app:pstsTextAllCaps="true"
    app:tabGravity="fill"
    app:tabMode="fixed" />

<com.cws.widget.NonSiwpablePager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

TestTabFragment.java

public class TestTabFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.test_tab_fragment, container, false);
    PagerSlidingTabStrip myTabLayout = (PagerSlidingTabStrip) view.findViewById(R.id.mainTabLayout);
    NonSiwpablePager pager = (NonSiwpablePager) view.findViewById(R.id.pager);
    ViewPagerAdapter mAdapter = getViewPagerAdapter();
    pager.setAdapter(mAdapter);
    pager.setOffscreenPageLimit(4);
    myTabLayout.setViewPager(pager);
    for (int i = 0; i < mAdapter.getCount(); i++) {
        View tabView = mAdapter.getCustomeView(getActivity(), i);;

        tabView = LayoutInflater.from(getContext()).inflate(R.layout.custom_tab_view, myTabLayout,
                false);
        String title_arr[] = {"ADVISORY", "CALENDER", "TOP ADVISORS", "EXPERT VIEW"};
        int[] drawables = new int[]{R.drawable.advisory_normal, R.drawable.calender_normal, R.drawable.topadvisor_normal, R.drawable.expertview_normal};
        TextView mTextView = (TextView) tabView.findViewById(R.id.textView);
        ImageView mImageView = (ImageView) tabView.findViewById(R.id.imageView2);
        mImageView.setImageResource(drawables[i]);
        mTextView.setText(title_arr[i]);
        myTabLayout.addView(tabView);
    }
    return view;
}

和custom_tab_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="@dimen/et_topPadding"
android:paddingTop="@dimen/et_topPadding">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="3dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="@color/text_color"
        android:textSize="@dimen/custom_tab_tSize" />
</LinearLayout>

</LinearLayout>

如果有人这样做了,请帮我解决。

0 个答案:

没有答案