【问题标题】:List view not scrolling in fragment using view pager inside NestedScrollView使用 NestedScrollView 中的视图寻呼机,列表视图未在片段中滚动
【发布时间】:2016-05-16 04:37:37
【问题描述】:

您好,我需要创建类似 whatsapp 的布局,它可以滚动操作栏而不是 tabLayout。我使用 viewpager 在其中加载片段。 在片段中,我添加了 listview,但在 Listview 上滚动操作栏不在那里滚动。 使用stackoverflow上的一些教程,我知道为此需要使用nestedScrollView,它对我有用。 但它停止滚动列表视图。 我为此使用了此代码。

activity_my.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"></android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_gravity="bottom"
    app:layout_anchor="@+id/appbar"
    app:layout_anchorGravity="bottom"
    app:layout_collapseMode="none">

    <ImageView
        android:id="@+id/tabBg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/primary"
        android:scaleType="fitXY" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="bottom"
        android:background="#00000000"
        app:layout_anchor="@+id/appbar"
        app:layout_anchorGravity="bottom"
        app:layout_collapseMode="none"
        app:tabGravity="fill"
        app:tabIndicatorColor="#fff"
        app:tabIndicatorHeight="2dp"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="#fff"
        app:tabTextColor="#fff" />

</FrameLayout>
<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="1000dp"
        android:orientation="vertical">

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

<!--<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />-->
</android.support.design.widget.CoordinatorLayout>

MyActivity.java

public class MyActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
ArrayList<Fragment> fragmentArrayList;
TabAdapter tabAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    /*Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);*/

    viewPager = (ViewPager) findViewById(R.id.pager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

private void setupViewPager(ViewPager viewPager) {
    TabAdapter adapter = new TabAdapter(getSupportFragmentManager());
    adapter.addFragment(new OneFragment(), "ONE");
    adapter.addFragment(new OneFragment(), "TWO");
    adapter.addFragment(new OneFragment(), "THREE");
    viewPager.setAdapter(adapter);
   }

 }

FragmentOne.java

public class OneFragment extends Fragment {
ListView listTest;
ArrayList<String> itemList;
ArrayAdapter<String> adapter;

public OneFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_one, container, false);
    listTest = (ListView) view.findViewById(R.id.listTest);
    itemList = new ArrayList<>();
    for (int i = 0; i < 50; i++) {
        itemList.add("Item : " + i);
    }
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, itemList);
    listTest.setAdapter(adapter);
    return view;
   }
}

fragment_one.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/listTest"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</RelativeLayout>

我使用了 LinearLayout monHeight = 1000dp,因为如果我在 nestedScrollView 中使用 android:fillViewPort="true" 并从 LinearLayout 中删除 minHeight,那么 listview 会显示但不会滚动。 现在它会滚动,但直到 1000dp 才会显示其他项目。

【问题讨论】:

  • 尝试使用recyclerview而不是listview
  • 它对我也不起作用,我也用过...

标签: android listview android-viewpager android-nestedscrollview


【解决方案1】:

好吧,首先你在NestedScrollView中扭曲了ViewPager,这没有必要。

所以改变,

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="1000dp"
        android:orientation="vertical">

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

到,

<android.support.v4.view.ViewPager
     android:id="@+id/pager"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_weight="1"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

现在你正在使用ListView,这与AppBarLayout 不兼容,所以我建议你改用RecyclerView

注意:如果您仍想使用ListView,可以使用NonScrollListview 将其包裹在NestedScrollView 下。

NonScrollListView

public class NonScrollListView extends ListView {

    public NonScrollListView(Context context) {
         super(context);
    }

    public NonScrollListView(Context context, AttributeSet attrs) {
         super(context, attrs);
    }

    public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
         super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
         ViewGroup.LayoutParams params = getLayoutParams();
         params.height = getMeasuredHeight();
    }
}

只需更改您的 fragment_one 就可以了。

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <packagename.NonScrollListView
        android:id="@+id/listTest"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></packagename.NonScrollListView>
</android.support.v4.widget.NestedScrollView>

我希望这会对你有所帮助。 快乐编码..

【讨论】:

  • 不客气..如果对您有帮助..您可以接受;)
  • 这确实有效。但是,对于创建 heightMeasureSpec_custom 的这种更改实际上做了什么,有什么解释吗?
  • 当然,只要您的列表创建或修改onMeasure 被调用。默认情况下,它将 ListView 包装为 2 个项目。所以我们正在做的是计算列表视图可以采用的最大高度,Integer.MAX_VALUE &gt;&gt; 2 可以解决问题。因此,通过写int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE &gt;&gt; 2, MeasureSpec.AT_MOST); 这一行,我们得到实际高度(直到列表视图的最后一项),我们只需通过写super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);heightMeasureSpec_custom 覆盖默认高度。我希望这会有所帮助
【解决方案2】:

你可以使用 Observable 滚动视图Refer the link

【讨论】:

  • 你应该评论这个..!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多