【问题标题】:Android: How to Fill SwipeyTabs?Android:如何填写 SwipeyTabs?
【发布时间】:2012-08-02 20:30:00
【问题描述】:

我在我的应用程序中使用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" />

有例子或教程吗?

任何支持都会很棒。

【问题讨论】:

标签: android


【解决方案1】:

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);

就是这样。我希望,这很清楚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    相关资源
    最近更新 更多