【问题标题】:Couple issues with swipeable tabs滑动标签的几个问题
【发布时间】:2015-09-03 02:12:36
【问题描述】:

我正在尝试在他们的应用上复制 Twitter 的个人资料视图。

这是视图的图片:

http://i.imgur.com/otgyxi4.png

我正在努力实现 3 个具体的目标:

  1. 视图顶部的标题布局
  2. 标题下方的 ListView(在 Twitter 应用上,这是帖子)
  3. 用于左右滑动的 ViewPager(在 Twitter 应用上,这是“推文”、“照片”和“收藏夹”选项卡

我已经完成了列表中的第一名。这是标题布局(header.xml):

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    ...

</LinearLayout>

我将它加载到我的 Fragment 的 onCreateView() 方法中:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_one, container, false);
    final ListView listView = (ListView) view.findViewById(R.id.listview);

    View header = inflater.inflate(R.layout.header, listView, false);
    listView.addHeaderView(header, null, false);

    ...

    return view;
}

这是我的第一个片段,fragment_one.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

在我的主要活动布局 (activity_main.xml) 中,我有这个:

<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/tabs"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/black"
        app:pstsIndicatorHeight="2dp"
        app:pstsShouldExpand="true"
        app:pstsTabTextSize="14dp"
        />

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

</LinearLayout>

我的 Activity 的 onCreate() 方法如下所示:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    pager = (ViewPager) findViewById(R.id.pager);
    pagerAdapter = new PagerAdapter(getSupportFragmentManager());
    pager.setAdapter(pagerAdapter);

    PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    tabs.setViewPager(pager);
}

但是,我有两个问题:

  1. 标签不在标题下方,因为它们应该是。
  2. 当我滑动到另一个选项卡时,标题也会移动(即使它是所有 3 个片段上的相同标题)。这是我的意思的图片:http://i.imgur.com/GXWgzkm.png

如何解决这些问题?

【问题讨论】:

    标签: android android-layout android-fragments android-activity android-xml


    【解决方案1】:

    您可以通过将标题从正在滑动的视图中移除并将其放置在包含您的 viewpager 的父视图(例如相对布局或框架布局)中来保持标题静止。只需确保在将 viewpager 添加到父视图之后添加标题视图。

    【讨论】:

    • 问题是,如果我垂直滚动,标题将不再移动,因为 ListView 是可滚动视图并且标题不可滚动。
    • 您可以使用列表视图的 OnScrollListener 来隐藏标题视图或在用户向下滚动列表时执行任何操作。当他们回到顶端时重新出现
    猜你喜欢
    • 2013-10-13
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    相关资源
    最近更新 更多