【问题标题】:How to make whole screen scrollable in Viewpager Fragment如何在 Viewpager Fragment 中使整个屏幕可滚动
【发布时间】:2016-07-04 13:18:45
【问题描述】:

我在滚动视图中有网格视图和列表视图(包括分页)。

这是产生单独的滚动,但我需要整个屏幕可滚动分页。我尝试使用 NestedListView 和 ExpandableHeightListView 但无法正常工作。

任何建议将不胜感激。

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

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clipToPadding="false"
    android:fillViewport="true"
    android:paddingBottom="75dp">

    <RelativeLayout
        android:id="@+id/root_rl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="beforeDescendants"
        android:focusable="true"
        android:focusableInTouchMode="true">

        <TextView
            android:id="@+id/all_tpcs_heading"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/bg_horizantal_magzine_listview"
            android:padding="@dimen/ten_dp"
            android:text="all topics"
            android:textAllCaps="true"
            android:textSize="14sp" />

        <com.healthyliving.live.utils.ExpandableHeightGridView
            android:id="@+id/topicsGrid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/all_tpcs_heading"
            android:layout_marginTop="@dimen/six_dp"
            android:clipChildren="true"
            android:gravity="center"
            android:horizontalSpacing="@dimen/six_dp"
            android:isScrollContainer="false"
            android:numColumns="2"
            android:stretchMode="none"
            android:verticalSpacing="@dimen/six_dp" />

        <TextView
            android:id="@+id/rcnt_artcls_heading"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/topicsGrid"
            android:layout_marginTop="@dimen/six_dp"
            android:paddingBottom="@dimen/ten_dp"
            android:paddingLeft="@dimen/ten_dp"
            android:paddingRight="@dimen/ten_dp"
            android:paddingTop="@dimen/sixteen_dp"
            android:text="Recent articles"
            android:textAllCaps="true"
            android:textSize="14sp" />

        <ListView
            android:id="@+id/recent_articles_listview"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/rcnt_artcls_heading"
            android:dividerHeight="@dimen/list_view_divider" />

        <ProgressBar
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            android:id="@+id/load_progreebar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminateDrawable="@drawable/my_progress_indeterminate"
            android:visibility="gone" />
    </RelativeLayout>
</ScrollView>

片段::

recentArticleAdapter = new ExploreRecentArtilcesAdapter(getActivity(),      featuredArticles);
    mRecentArticles.setAdapter(recentArticleAdapter);

【问题讨论】:

  • ListView 和 Gridview 优化如果嵌套在 Scroll 中将不起作用。它里面的所有视图都是一次绘制的。因此,您可能应该考虑根据您的要求使用带有一列/多列的单个 Gridview。
  • 分页工作正常吗?如果我没记错的话,你需要在滚动视图中都需要可扩展的高度列表视图和网格视图。如果我错了,请纠正我。

标签: android listview android-fragments gridview scrollview


【解决方案1】:

在您的布局 xml 文件中使用下面给定的自定义列表和网格视图,而不是默认的 ListViewGridView

自定义列表视图:-

public class CustomListView extends ListView {

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

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

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

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

自定义 GridView:-

public class CustomGridView extends GridView {

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

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

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

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
}

在这种方法中,如果您发现垂直滚动和水平滑动(因为您有视图寻呼机)有任何困难,请使用下面的自定义 ScrollView 类,而不是布局 xml 文件中的默认滚动视图。

public class VerticalScrollView extends ScrollView {

    private float xDistance, yDistance, lastX, lastY;
    public VerticalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                lastX = ev.getX();
                lastY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();
                xDistance += Math.abs(curX - lastX);
                yDistance += Math.abs(curY - lastY);
                lastX = curX;
                lastY = curY;
                if(xDistance > yDistance)
                    return false;
        }

        return super.onInterceptTouchEvent(ev);
    }
}

使用所有这些自定义视图类的方式与您在当前实现中使用 com.healthyliving.live.utils.ExpandableHeightGridView 自定义视图的方式相同。希望这个答案对您有所帮助。

【讨论】:

  • 我尝试使用所有这些自定义类.. 它的滚动已满但挂起不流畅
猜你喜欢
  • 2021-01-26
  • 1970-01-01
  • 2017-11-24
  • 2021-11-29
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多