我有一个CardView on list_song.xml文件,例如:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:background="@drawable/mxhbg"
    android:layout_marginTop="8dp">

    <RelativeLayout
        android:id="@+id/txtitemsong"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/listsongbg"
        android:padding="8dp">

        <ImageView
            android:id="@+id/imgmusic"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="5dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/listsongbg"
            android:src="@drawable/ic_music" />

        <TextView
            android:id="@+id/txtsongname"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_alignParentEnd="true"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"
            android:layout_toEndOf="@+id/imgmusic"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit="marquee_forever"
            android:padding="6dp"
            android:scrollHorizontally="true"
            android:singleLine="true"
            android:text="Name"
            android:textColor="#ffff"
            android:textSize="15sp" />


    </RelativeLayout>


</androidx.cardview.widget.CardView>

我用我的Activity上的BaseAdapter来绑定:

public class PlaylistAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return songsList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = getLayoutInflater().inflate(R.layout.list_song, parent, false);

        RelativeLayout txtitemsong = view.findViewById(R.id.txtitemsong);
        if (position < 2 || position > 5) {
            txtitemsong.setVisibility(View.GONE);
        }

        return view;
    }
}

由于某些限制(在这种情况下,2/5作为例子),我想完全隐藏大约txtitemsong个项目,这是垂直堆叠.

但是一旦我用txtitemsong.setVisibility(View.GONE)隐藏它,似乎每个txtitemsong的边缘被保留(所以显示的项目上方/下方有很多空间).

如何隐藏txtitemsong个堆叠的物品?

我可以在CardView上设置android:layout_marginTop="0dp",但似乎没有效果.android:layout_marginTop="0dp"txtitemsong,没有任何变化.

以下是原始视图:

enter image description here

这就是txtItemSong.setVisibility(View.GONE)隐藏前两项和最后一项的视图:

enter image description here

您可以看到,在项目列表开始之前,顶部有页边距/填充.

如果我隐藏了CardView,并且我确实隐藏了:

if (position < 2 || position > 5) {
    txtItemSong.setVisibility(View.GONE);
    ViewGroup.LayoutParams params = txtItemSong.getLayoutParams();
    params.height = 0;
    txtItemSong.setLayoutParams(params);
}

以下是结果(甚至奇怪):

enter image description here

推荐答案

try 使用下面的代码,在迭代之前,我们在其中过滤了Songlist

public class PlaylistAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<String> items;

    public PlaylistAdapter(Context context, ArrayList<String> items) {
        this.context = context;
        this.items = filterItems(items);
    }

    private ArrayList<String> filterItems(ArrayList<String> originalItems) {
        ArrayList<String> filteredItems = new ArrayList<>();
        for (int i = 2; i <= 5 && i < originalItems.size(); i++) {
            filteredItems.add(originalItems.get(i));
        }
        return filteredItems;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = getLayoutInflater().inflate(R.layout.list_song, parent, false);

        CardView cardView = view.findViewById(R.id.cardview);

        //use this incase filterItems doesn't works
        // if (position < 2 || position > 5) {
        //     cardView.setVisibility(View.GONE);
        //     cardView.setPadding(0, 0, 0, 0);
        //     cardView.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
        // }

        return view;
    }
}

一个名为filterItems()的新方法,它获取原始项目列表,并返回包含从索引2到索引5(包括索引5)的项目的筛选列表.

参考链接获取更多建议

Android set View.GONE does not "release" space in listview

Android相关问答推荐

未解析的引用:背景 colored颜色

当我的Log.i()不在主线程中时,如何在我的logcat中显示它?

Kotlin多平台向导,不兼容版本(ANP 8.2.0)ANP 8.1.2

使用不同的Google帐户登录

在Jetpack Compose中,material 3 Textfield上的底部边框 colored颜色 是如何更改的?

无法将项目添加到文件室数据库

无法加载类';com.android.build.api.extension.AndroidComponentsExtension';

如何共享没有';t是否存在?(仅信息)在Android?

是否可以附加事件处理程序,如onClick,拖动到Canvas Composable中绘制的内容,或使用drawBehind修饰符?

在Jetpack Compose中,我可以配置动画以恒定的速度而不是恒定的时间运行吗?

是否可以在 compose 中使用三次贝塞尔曲线进行动画?

如何避免多次调用 Jetpack Compose 的 onClick 回调

Jetpack Compose 如何使一个项目相对于另一个项目垂直居中

在单元测试下断言协程未完成

如何从 firebase 实时数据库中检索最后一个值

从 Jetpack Compose 中的 IconButton 中删除黑色色调

可组合的可见性不随状态变化而变化

compose 更改列表元素但lazyColumn不更改

Android - 水平(从右到左)圆形背景 colored颜色 过渡

如何从构建的流对象中发出值