【发布时间】: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.xml 为 clicked_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