Android: 如何填充SwipeyTabs?

7

我在我的应用中使用了SwipeyTabs。我不确定如何填充选项卡的内容,它甚至没有自己的xml文件。

我是这样使用选项卡的:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_swipey_tabs);
    initViewPager(4, 0xFFFFFFFF, 0xFF000000);
    mSwipeyTabs = (SwipeyTabsView) findViewById(R.id.swipey_tabs);
    mSwipeyTabsAdapter = new SwipeyTabsAdapter(this);
    mSwipeyTabs.setAdapter(mSwipeyTabsAdapter);
    mSwipeyTabs.setViewPager(mPager);
}

private void initViewPager(int pageCount, int backgroundColor, int textColor) {
    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ExamplePagerAdapter(this, pageCount, backgroundColor, textColor);
    mPager.setAdapter(mPagerAdapter);
    mPager.setCurrentItem(1);
    mPager.setPageMargin(1);
}

以下是activity_swipey_tabs.xml布局:

<android.ex.com.viewpager.extension.SwipeyTabsView
    android:id="@+id/swipey_tabs"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#3B3B3B" />

<View
    android:id="@+id/colorline"
    android:layout_width="fill_parent"
    android:layout_height="2dip"
    android:layout_below="@+id/swipey_tabs"
    android:background="#FF91A438" />

<View
    android:id="@+id/blackline"
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:layout_below="@+id/colorline"
    android:background="#FF000000" />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/blackline" />

有没有示例或教程?

任何支持都将是极好的。


请查看此链接:https://dev59.com/Ymgv5IYBdhLWcg3wBMIK - Hardik Nadiyapara
2
你确定这就是我要找的吗?如果是的话,例如我如何设置任何TextView或其他控件的监听器?顺便说一句,谢谢。 - Martin
1个回答

6
SwipeyTabs 的选项卡就像虚拟布局一样,也就是说它们没有自己定义的布局。您需要动态定义每个组件。
让我详细解释一下。例如,您需要一个 ListView(自定义)。您可以创建以下布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="70dp"
    android:padding="5dp"
    android:background="#ffffff" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="80dp"
        android:layout_height="60dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/i1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignParentRight="true"
        android:textColor="@android:color/black"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView2"
        android:layout_alignTop="@+id/imageView1"
        android:textColor="@android:color/black" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_toRightOf="@+id/imageView1"
        android:textColor="@android:color/black"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

现在你需要一个`ArrayAdapter`来适配这个自定义的`ListView`,我没有包含`ArrayAdapter`。
所以在你的`PagerAdapter`类中,定义一个`ListView`并使用这个布局。例如:
@Override
public Object instantiateItem(View container, int position) {
    ListView v = new ListView( mContext );
    //test_list_layout which is above
    TestAdapter adapter = new TestAdapter(mContext, R.layout.test_list_layout, objs);

    v.setAdapter( adapter );
    ((ViewPager)container ).addView( v, 0 );
    v.setSelection( scrollPosition[ position ] );
    v.setOnScrollListener( new OnScrollListener() {             
         public void onScrollStateChanged( AbsListView view, int scrollState ){}
         public void onScroll( AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount ) {
        scrollPosition[ pos ] = firstVisibleItem;
         }
    });
    return v;
}

现在它能正常工作。如果您需要更多选项卡,请使用position参数作为分隔符,例如if(position == 0)。您需要添加以下代码:

((ViewPager) container).removeView((View) container);

就这些了。希望很清楚明白。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接