Android:如何填充 SwipeyTabs?

标签 android

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

有没有例子或教程?

任何支持都会很棒。

最佳答案

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

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

关于Android:如何填充 SwipeyTabs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11756204/

相关文章:

android - 无法获取适用于 Android 的 Facebook SDK 4.8.1 的访问 token (或)未调用 FacebookCallback(或)requestCode 在 Facebook 集成中不同

java - null 对象引用上的 childEventListerer,Firebase 数据库

Android - 让 GMailSender 工作有问题吗?

android - 如何在 TextView 中显示卢比符号?

Android WebView 未加载博客

CMake 构建工具链中 "strip"命令的 Android NDK 路径变量

android - 检查应用程序是否在平板电脑上运行

java - 与 UI 线程通信 Android : User Inactivity

android - 断点不适用于 Android 中的服务

java - RecyclerView不显示任何项目