gpt4 book ai didi

Android:更改所有 ListView 行中项目的可见性

转载 作者:行者123 更新时间:2023-11-29 18:02:58 26 4
gpt4 key购买 nike

我的应用程序中有一个 ListView,在用户请求后,它可以通过在每个 ListView 行的开头拖动 ImageView 来重新排列。我在一个小窗口中向用户展示 ListView ,为了最大化空间,我将拖动 View 的可见性设置为 GONE,并计划仅在用户选择调用编辑列表的菜单项时将其设置为可见项目。

这是行项目的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="horizontal" >

<ImageView
android:id="@id/drag_handle"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_margin="8dp"
android:visibility="gone"
android:src="@drawable/ic_drag_dots_holo_dark" />

<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:background="?android:attr/dividerVertical" />

<TextView
android:id="@+id/drag_n_drop_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:maxLines="1"
android:minHeight="24dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="?android:attr/textColorPrimary" />
</LinearLayout>

这是菜单选择的代码:

    @Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_edit_playlist:
if (inEditMode) {
//If we are already showing the drag view, hide it.
inEditMode = false;
findViewById(R.id.drag_handle).setVisibility(View.GONE);
}
else {
//If the drag view is hidden, make it visible.
inEditMode = true;
findViewById(R.id.drag_handle).setVisibility(View.VISIBLE);
}
return true;
default:
return super.onContextItemSelected(item);
}
}

此代码的问题在于,当我调用 setVisibility() 时,它只会更改 ListView 第一个项目上拖动 View 的可见性,而所有其他行上的拖动 View 保持隐藏状态.

在这种情况下,我该怎么做才能使所有行上的所有 dragView 可见?

编辑这是一个 image我希望它在按下编辑后看起来如何,这里是 image它现在的样子,上面的代码。

谢谢!

最佳答案

The problem with this code, is that when I call setVisibility() it only changes the visibility of the drag view on the firstmost item of the listview, and the dragview on all other rows remains hidden.

这种行为是正常的,因为您尝试使用 findViewById() 方法从一行中更改 View 的可见性,该方法将首先返回 在布局中出现具有该 ID 的 View。第一次出现将位于列表的第一个可见行(无论如何 findViewById 方法将不起作用,因为您可能会看到 ListView 中的 View 行都将具有相同的 ID)。

What would I do to make ALL dragViews on all rows visible in this case?

正确的方法是让 ListView 的适配器知道它当前处于哪个状态(inEditMode),并在 中显示/隐藏拖动 handle 基于此的 getView 方法。完成此操作后,您在 onContextItemSelected 中要做的就是更新 inEditMode 状态变量并调用适配器上的 notifyDataSetChanged() 方法。

关于Android:更改所有 ListView 行中项目的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14862455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com