【问题标题】:Disable a PreferenceScreen's Scrolling禁用 PreferenceScreen 的滚动
【发布时间】:2015-07-24 02:14:44
【问题描述】:

如何禁用 PreferenceScreen 的滚动以使其占据整个空间?

这里是设置.. 我有一个片段,它有一个 ScrollView 作为它的主(根)视图,里面是一个包含两个片段(FrameLayout)的 LinearLayout。第一个是 PreferenceFragment,另一个只是一个 Fragment,包含一个 View 和一个 ListView。我希望 PreferenceScreen 占据整个空间,以便整个内容滚动。

这是代码

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:orientation="vertical">
        <FrameLayout
            android:id="@+id/preferences_frame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">
        </FrameLayout>

        <FrameLayout
            android:id="@+id/services_edit_frame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1">

        </FrameLayout>
    </LinearLayout>
</ScrollView>

非常感谢!

【问题讨论】:

  • 我无法理解你真正想要什么
  • 我想禁用 PreferenceScreen 的滚动
  • 因为滚动视图而滚动,对吗?为什么不将 LinearLayout 设为根视图?
  • 由于滚动视图,它不滚动。滚动视图在那里,它可以与另一个片段 (services_edit_frame) 一起滚动,但显然,它自己滚动,这就是我想要禁用的。
  • 是的,它是可滚动的,这就是我要禁用 D:

标签: android android-fragments


【解决方案1】:

为时已晚,但对于寻找正确答案的人来说。在您的 PreferenceFragmentCompat 类中,只需覆盖 onAvtivityCreated 方法,如下所示。

@Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getListView().setOverScrollMode(View.OVER_SCROLL_NEVER);
    }

【讨论】:

    【解决方案2】:

    我已经回答了一个类似的问题here。希望对您有所帮助!

    在您的PreferenceFragment 类中执行以下操作:

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    
        if (getView() != null) {
    
            ListView listView = (ListView) getView().findViewById(android.R.id.list);
            Adapter adapter = listView.getAdapter();
    
            if (adapter != null) {
                int height = 0;
                //int height = listView.getPaddingTop() + listView.getPaddingBottom();
    
                for (int i = 0; i < adapter.getCount(); i++) {
                    View item = adapter.getView(i, null, listView);
    
                    item.measure(0, 0);
                    height += item.getMeasuredHeight();
                }
    
                FrameLayout frame = (FrameLayout) getActivity().findViewById(R.id.content_frame);
    
                ViewGroup.LayoutParams param = frame.getLayoutParams();
                param.height = height + (listView.getDividerHeight() * (adapter.getCount());
                frame.setLayoutParams(param);
            }
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      我也在努力解决这个问题。经过调查,我发现 ScrollView 不支持这种行为。但是只需将其更改为 NestedScrollView 即可解决问题。

      <?xml version="1.0" encoding="utf-8"?>
      <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:descendantFocusability="beforeDescendants"
          android:focusableInTouchMode="true"
          android:layout_marginHorizontal="@dimen/activity_horizontal_margin"
          android:layout_marginVertical="@dimen/activity_vertical_margin"
          >
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:gravity="top">
      
      ...
      
              <FrameLayout
                  android:id="@+id/preferences_container"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  />
      ...
      
          </LinearLayout>
      
      </androidx.core.widget.NestedScrollView>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-24
        • 1970-01-01
        • 1970-01-01
        • 2012-02-08
        • 2018-11-19
        • 2021-10-15
        • 2011-11-12
        相关资源
        最近更新 更多