【发布时间】:2016-03-16 11:08:19
【问题描述】:
我有一个列表视图,它使用基本适配器和两个布局来显示项目。我已经在这个列表视图上实现了 multichoicemodellistener。
问题是当我长按列表视图项目时,有些项目被选中,而另一些则没有。我已经意识到被选中的那些正在使用与未被选中的不同的行布局。
被选中的列表视图行的布局。 >>>> LAYOUT 1
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:activatedBackgroundIndicator">
<LinearLayout
android:id="@+id/chat_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:orientation="vertical">
<com.x.y.SquareImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:layout_gravity="center" />
<Textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#020202"
android:textSize="17sp"
android:textIsSelectable="false" />
</LinearLayout>
</RelativeLayout>
未选中的列表视图行的布局 >>>> LAYOUT 2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:activatedBackgroundIndicator">
<LinearLayout
android:id="@+id/chat_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="15dp">
<Textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#020202"
android:textSize="17sp"
android:textIsSelectable="true" />
</LinearLayout>
</RelativeLayout>
我是如何实现多选模式监听器的
list_View.setMultiChoiceModeListener(new Selector());
选择器类
private class Selector implements AbsListView.MultiChoiceModeListener {
@Override
public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.chat_activity_menu, menu);
return true;
}
@Override
public void onItemCheckedStateChanged(android.view.ActionMode mode, int position, long id, boolean checked) {
int checkedCount = list_View.getCheckedItemCount();
Log.d("Selector","checked items are "+checkedCount);
mode.setSubtitle(checkedCount+" selected");
}
@Override
public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(android.view.ActionMode mode, MenuItem item) {
return true;
}
@Override
public void onDestroyActionMode(android.view.ActionMode mode) {
adapter.removeSelection();
}
}
为什么我的一些使用 LAYOUT 2 的列表视图项目没有被选中?每当我单击使用该布局的任何项目时,都不会发生选择,甚至不会打印出调试信息。
【问题讨论】:
-
试试这个可能对你有帮助。 stackoverflow.com/questions/35765399/…
-
@AnandKumarJha 这是一个好方法,但在我的情况下是不可接受的。如果你能帮助解决其中的错误,我正在使用的方法是最好和合适的。
-
@AnandKumarJha 我已经意识到了这个问题。看看我下面的回答
标签: android android-layout listview