【问题标题】:change ListView row xml layout resource when onListItemClick() is used使用 onListItemClick() 时更改 ListView 行 xml 布局资源
【发布时间】:2015-07-02 15:32:55
【问题描述】:

我有这个自定义布局,row.xml 在我的ListView 行中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal"
    android:weightSum="10">

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="9"
        android:id="@+id/rowTextView"
        android:typeface="serif"
        android:gravity="center_vertical"
        android:textSize="15sp"
        android:layout_marginLeft="5dp" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/swipeImageView"
        android:src="@mipmap/swipe"
        android:alpha="0.5"
        android:padding="11dp" />

</LinearLayout>

现在我想在使用onListItemClick()时为这个布局clicked_row.xml更改row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal"
    android:weightSum="10">

    <EditText
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="9"
        android:id="@+id/rowTextView"
        android:typeface="serif"
        android:gravity="center_vertical"
        android:textSize="15sp"
        android:layout_marginLeft="5dp" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/saveImageView"
        android:src="@mipmap/trash"
        android:alpha="0.5"
        android:padding="11dp" />

</LinearLayout>

我试图使用this answer;但他们似乎在操纵布局内视图的可见性,而不是用一种布局替换另一种布局。我不确定应该用什么来替换 row.xmlclicked_row.xml

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);


     //Code for replacing row.xml with clicked_row.xml


}

感谢任何帮助!

【问题讨论】:

  • 我认为改变可见性本身就足以解决问题。切换资源文件的具体需求是什么?
  • @SadeshkumarPeriyasamy,问题是我计划在 clicked_row.xml 上包含更多视图,所以我认为替换 xmls 可能比设置不可见的 2 个视图然后可见的其他视图更容易10 次观看。
  • 在这种情况下,您可以覆盖 Adapter 类的 getItemViewTypeCount()getItemViewType(int position) 以获得两个不同的视图。在onListItemClick中可以切换modal的类型并刷新listview。

标签: android android-layout android-activity view android-studio


【解决方案1】:

我认为更好的方法是合并这两个文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal"
    android:weightSum="10">

<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="9"
    android:id="@+id/rowTextView"
    android:typeface="serif"
    android:gravity="center_vertical"
    android:textSize="15sp"
    android:layout_marginLeft="5dp" />

<EditText
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="9"
    android:id="@+id/rowEditText"
    android:typeface="serif"
    android:gravity="center_vertical"
    android:textSize="15sp"
    android:layout_marginLeft="5dp"
    android:visibility="gone"/>

<ImageView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:id="@+id/swipeImageView"
    android:src="@mipmap/swipe"
    android:alpha="0.5"
    android:padding="11dp" />

</LinearLayout>

然后:

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        ((ViewGroup)v).findViewById(R.id.rowTextView).setVisibility(View.GONE);
        ((ViewGroup)v).findViewById(R.id.rowEditText).setVisibility(View.VISIBLE);
        ImageView imageView = (ImageView)((ViewGroup)v).findViewById(R.id.swipeImageView);
        imageView.setImageDrawable(imageView.getContext().getResources().getDrawable(R.mipmap.trash));



        //Code for replacing row.xml with clicked_row.xml


    }

【讨论】:

  • 感谢这个例子,这也是我一开始的想法。如果没有其他人给出更好的答案,我会再给它 1 或 2 天我会接受你的答案,谢谢并有你的 +1
【解决方案2】:

您可以将 TextViewImageView 添加到同一布局中。您可以在 onListItemClick

上隐藏一个的可见性

但这会在您滚动列表时导致问题,因为 Android 会重用列表项视图。因此,不要直接访问 ListItemView,而是在模型类中添加一个类似 selected 的属性。

Adapater.getView()中根据这个selected字段值隐藏/显示视图元素。

这篇文章将帮助您了解 Android ListView 的工作原理: How ListView's recycling mechanism works

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多