【问题标题】:Problems with the scrolling in a expandable list view inside a viewpager - Android在 viewpager 内的可扩展列表视图中滚动的问题 - Android
【发布时间】:2016-03-22 18:04:42
【问题描述】:

我在 appcompat v7 中有一个带有工具栏和选项卡布局的应用程序。我使用 View Pager 浏览选项卡,但问题是在其中放置可扩展列表视图的位置不滚动:这是我的主要活动中的 Java 代码

最小 API 为 15:Android Ice Cream Sandwicth 4.0.4

public class Principal extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_principal);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final TabLayout Tabulacion = (TabLayout) findViewById(R.id.Tabulacion);
    final ViewPager viewPager = (ViewPager) findViewById(R.id.Pager);
    final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), Tabulacion.getTabCount());

    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(Tabulacion));
    Tabulacion.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}

PagerAdapter 中的 Java 代码

public class PagerAdapter extends FragmentStatePagerAdapter {
int intNumTabs;

public PagerAdapter(FragmentManager FM, int NumTabs) {
    super(FM);
    this.intNumTabs = NumTabs;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            TabHome Tab1 = new TabHome();
            return Tab1;
        case 1:
            TabLectura Tab2 = new TabLectura();
            return Tab2;
        default:
            return null;
    }
}

@Override
public int getCount() {
    return intNumTabs;
}
}

主要活动的 XML 代码

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".Principal">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:id="@+id/textView">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.design.widget.TabLayout
        android:id="@+id/Tabulacion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.design.widget.TabItem
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Inicio">
        </android.support.design.widget.TabItem>
        <android.support.design.widget.TabItem
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Lectura">
        </android.support.design.widget.TabItem>
    </android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/Pager"
        android:isScrollContainer="true"
        android:nestedScrollingEnabled="true"
        android:background="@color/backgroundColor" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

标签 2 片段中的 XML 代码:

<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ListView">
</ExpandableListView>

Expandable List View的内容是动态生成的

【问题讨论】:

    标签: android android-fragments android-viewpager expandablelistview android-appcompat


    【解决方案1】:

    我解决了!我从 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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Principal">
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay"
            android:id="@+id/textView">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
    
            <android.support.design.widget.TabLayout
                android:id="@+id/Tabulacion"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <android.support.design.widget.TabItem
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Inicio">
                </android.support.design.widget.TabItem>
                <android.support.design.widget.TabItem
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Lectura">
                </android.support.design.widget.TabItem>
            </android.support.design.widget.TabLayout>
        </android.support.design.widget.AppBarLayout>
    </android.support.design.widget.CoordinatorLayout>
    
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/Pager"
        android:background="@color/backgroundColor" />
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      使用回收器视图而不是列表视图。我已经在视图寻呼机中实现了它,并且效果很好。 Recyclerview 几乎可以完成列表视图所做的所有事情,甚至更多。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 1970-01-01
      • 2019-09-23
      相关资源
      最近更新 更多