java 使用自定义适配器在列表视图中选择/突出显示多个项目 - Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26645041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selecting/highlighting multiple items in listview with custom adapter - Android
提问by Willis
I followed a good tutorial, with some changes, to create a custom ListView that will allow me to display multiple images for each row in my ListView. What I would like to do is highlight by selecting multiple items in the list and then perform some sort of action to all of those list items by selecting an option on my Options Menu. However, the problem I seem to be running into is that I cannot select multiple items, even though I have added android:choiceMode="multipleChoice"
to the ListView in the .xml file. I realize that this can be done using check boxes or radio buttons, but I would prefer to avoid that. I have attached my source code below. Any help would be appreciated. Lastly, thanks to http://custom-android-dn.blogspot.com/for a great tutorial. Thanks.
我遵循了一个很好的教程,做了一些更改,创建了一个自定义 ListView,它允许我为 ListView 中的每一行显示多个图像。我想要做的是通过在列表中选择多个项目来突出显示,然后通过在我的选项菜单上选择一个选项对所有这些列表项目执行某种操作。但是,我似乎遇到的问题是我无法选择多个项目,即使我已添加android:choiceMode="multipleChoice"
到 .xml 文件中的 ListView。我意识到这可以使用复选框或单选按钮来完成,但我宁愿避免这种情况。我在下面附上了我的源代码。任何帮助,将不胜感激。最后,感谢http://custom-android-dn.blogspot.com/的精彩教程。谢谢。
CustomlistviewActivity.java
自定义listviewActivity.java
package DucNguyen.example.customlistview;
public class CustomlistviewActivity extends Activity {
private ListView listViewFootballLegend;
private Context ctx;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customlistview);
ctx=this;
List<FootballLegend> legendList= new ArrayList<FootballLegend>();
legendList.add(new FootballLegend("Pele","October 23, 1940 (age 72)","pele","brazil"));
legendList.add(new FootballLegend("Diego Maradona","October 30, 1960 (age 52)","maradona","argentina"));
legendList.add(new FootballLegend("Johan Cruyff","April 25, 1947 (age 65)","cruyff","netherlands"));
legendList.add(new FootballLegend("Franz Beckenbauer","September 11, 1945 (age 67)","beckenbauer","germany"));
legendList.add(new FootballLegend("Michel Platini","June 21, 1955 (age 57)","platini","france"));
legendList.add(new FootballLegend("Ronaldo De Lima","September 22, 1976 (age 36)","ronaldo","brazil"));
listViewFootballLegend = ( ListView ) findViewById( R.id.FootballLegend_list);
listViewFootballLegend.setAdapter( new FootballLegendListAdapter(ctx, R.layout.legend_row_item, legendList ) );
// Click event for single list row
listViewFootballLegend.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FootballLegend o = (FootballLegend) parent.getItemAtPosition(position);
Toast.makeText(CustomlistviewActivity.this, o.getName().toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
FootballLegendListAdapter.java
FootballLegendListAdapter.java
package DucNguyen.example.customlistview;
public class FootballLegendListAdapter extends ArrayAdapter<FootballLegend> {
private int resource;
private LayoutInflater inflater;
private Context context;
public FootballLegendListAdapter ( Context ctx, int resourceId, List<FootballLegend> objects) {
super( ctx, resourceId, objects );
resource = resourceId;
inflater = LayoutInflater.from( ctx );
context=ctx;
}
@Override
public View getView ( int position, View convertView, ViewGroup parent ) {
convertView = ( RelativeLayout ) inflater.inflate( resource, null );
FootballLegend Legend = getItem( position );
TextView legendName = (TextView) convertView.findViewById(R.id.legendName);
legendName.setText(Legend.getName());
TextView legendBorn = (TextView) convertView.findViewById(R.id.legendBorn);
legendBorn.setText(Legend.getNick());
ImageView legendImage = (ImageView) convertView.findViewById(R.id.legendImage);
String uri = "drawable/" + Legend.getImage();
int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
Drawable image = context.getResources().getDrawable(imageResource);
legendImage.setImageDrawable(image);
ImageView NationImage = (ImageView) convertView.findViewById(R.id.Nation);
uri = "drawable/" + Legend.getNation();
imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
image = context.getResources().getDrawable(imageResource);
NationImage.setImageDrawable(image);
return convertView;
}
}
FootballLegend.java
足球传奇.java
package DucNguyen.example.customlistview;
public class FootballLegend {
public FootballLegend(String name, String born, String image, String nation) {
super();
this.name = name;
this.born = born;
this.image = image;
this.nation = nation;
}
private String name;
private String born;
private String image;
private String nation;
public String getName() {
return name;
}
public void setName(String nameText) {
name = nameText;
}
public String getNick() {
return born;
}
public void setNick(String born) {
this.born = born;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getNation() {
return nation;
}
public void setNation(String image) {
this.image = nation;
}
}
legend_row_item.xml
Legend_row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left side Thumbnail image -->
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/legendImage"
android:layout_width="50dip"
android:layout_height="50dip" />
</LinearLayout>
<TextView
android:id="@+id/legendName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
<TextView
android:id="@+id/legendBorn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/legendName"
android:textColor="#343434"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail" />
<ImageView
android:id="@+id/Nation"
android:layout_width="45dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:layout_centerVertical="true" />
</RelativeLayout>
activity_customlistview.xml
activity_customlistview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/FootballLegend_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="5dp"
android:choiceMode="multipleChoice" >
</ListView>
</LinearLayout>
回答by S.D.
The root
View
of each item you will display in list must implementCheckable
. When implementing this interface also update the drawable state of View. See this answerfor how to do that.Set a
<selector>
drawable as background for this rootView
. Which different color/drawables for checked state and normal states.Set
ListView
's choice mode to single choice or multi-choice.In list adapter, provide item views with above created View as parent layout.
View
您将在列表中显示的每个项目的根必须实现Checkable
. 在实现这个接口时,也会更新 View 的 drawable 状态。请参阅此答案以了解如何执行此操作。<selector>
为这个 root设置一个drawable 作为背景View
。选中状态和正常状态的不同颜色/可绘制对象。将
ListView
的选择模式设置为单选或多选。在列表适配器中,将上面创建的视图作为父布局提供项目视图。
Now, ListView
will take care of setting checked/unchecked state on its item views. Also you can call getCheckedItemIds()
or getCheckedItemPositions()
on ListView
to get currently selected items.
现在,ListView
将负责在其项目视图上设置选中/未选中状态。您也可以调用getCheckedItemIds()
或getCheckedItemPositions()
onListView
以获取当前选定的项目。