【问题标题】:How to make a scrollable layout that includes a listview + imageview如何制作包含 listview + imageview 的可滚动布局
【发布时间】:2018-07-12 07:54:38
【问题描述】:

所以,我在布局文件中得到了一个带有列表视图的片段。此列表视图上方是带有徽标的图像视图。

我面临的问题是我无法将滚动设置为整个布局。 我在其他帖子上有红色,你不应该这样做,但如果我必须以另一种方式来做,我必须重做很多代码。

我还从 API 获取数据,并且元素的数量可能会发生变化。这就是我使用列表视图的原因。

我没有关于布局类型的偏好。目前我正在使用 ScrollView 布局。

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.HomeFragment"
android:fillViewport="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:src="@drawable/logo"/>

    <ListView
        android:id="@+id/listFemArrangementer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:listSelector="@color/white"
        android:scrollbars="none">

    </ListView>

</LinearLayout>

谁有解决这个问题的好办法?

【问题讨论】:

  • 请将您的代码添加为文本而不是图像
  • 使用 android.support.v4.widget.NestedScrollView 而不是 ScrollView
  • @Subzero 将滚动视图更改为嵌套滚动视图,但没有任何变化。列表自行滚动,但图像不随列表滚动。我想要一种同时滚动图像和列表视图的静态布局
  • 如果你不需要滚动,为什么不使用 LinearLayout 而不是 ListView?
  • 因为元素的数量没有确定,可能是2个,也可能是5个。我不知道有什么更好的方法来做到这一点。 @Subzero

标签: android listview android-fragments imageview


【解决方案1】:

尝试将布局xml更改为:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.HomeFragment"
    android:fillViewport="true">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:src="@drawable/logo"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ListView
            android:id="@+id/listFemArrangementer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:listSelector="@color/white"
            android:scrollbars="none"/>
    </ScrollView>

</LinearLayout>

【讨论】:

  • ListView 里面的 ScrollView 很“不错”
  • 问题不在于列表视图的滚动。我想在滚动时包含图像视图,以便整个布局内容正在移动。
  • 在这种情况下,请查看此链接:stackoverflow.com/questions/7611085/…
  • 是的,我昨天搜索时看到了那个帖子。我设法停止了列表视图中的滚动,但下部元素不会显示。我想我只需要找到一种方法来一次显示整个列表。 @SusmitAgrawal
【解决方案2】:

为我的问题找到了解决方案。

我创建了一个 listviewclass 并禁用了滚动等。

public class ScrollDisabledListView extends ListView {

private int mPosition;

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

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

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

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;

    if (actionMasked == MotionEvent.ACTION_DOWN) {
        // Record the position the list the touch landed on
        mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
        return super.dispatchTouchEvent(ev);
    }

    if (actionMasked == MotionEvent.ACTION_MOVE) {
        // Ignore move events
        return true;
    }

    if (actionMasked == MotionEvent.ACTION_UP) {
        // Check if we are still within the same view
        if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) {
            super.dispatchTouchEvent(ev);
        } else {
            // Clear pressed state, cancel the action
            setPressed(false);
            invalidate();
            return true;
        }
    }

    return super.dispatchTouchEvent(ev);
}

然后我在布局中使用了这个类(滚动视图)

<no.packagename.ScrollDisabledListView
            android:id="@+id/listFemArrangementer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:listSelector="@color/white"
            android:scrollbars="none"
            android:padding="0px"
            >
        </no.packagename.ScrollDisabledListView>

然后我从适配器中找到并使用了列表的高度

listView.setAdapter(adapter);


    if(adapter != null){
        int totalHeight = 0;
        for (int i = 0; i < adapter.getCount(); i++) {
            View listItem = adapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }

【讨论】:

    【解决方案3】:

    可以使用NestedScrollView,但首先最好使用RecyclerView 而不是ListView

    下面是一个简单的xml展示这个布局

    <android.support.v4.widget.NestedScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".fragments.HomeFragment"
        android:fillViewport="true">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="25dp"
                android:layout_gravity="center"
                android:adjustViewBounds="true"
                android:src="@drawable/logo"/>
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/listFemArrangementer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollbars="none"/>
    
        </LinearLayout>
    
    </android.support.v4.widget.NestedScrollView>
    

    【讨论】:

    • 为什么使用 RecyclerView 而不是 ListView 更好?
    • @Subzero 抱歉是个错误
    • @Toby 我建议您阅读文档或阅读此 SO stackoverflow.com/questions/28525112/…
    • 谢谢! @seyedJafari
    • 如果回答有帮助,请考虑支持或接受它
    猜你喜欢
    • 2019-03-31
    • 2013-04-26
    • 2011-05-17
    • 2020-07-10
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多