选择/高亮自定义适配器列表视图多个项目 - 机器人 [英] Selecting/highlighting multiple items in listview with custom adapter - Android

查看:145
本文介绍了选择/高亮自定义适配器列表视图多个项目 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着一个很好的教程,有一些变化,以创建一个自定义的ListView,让我在我的ListView中每行显示多个图像。我想这样做是亮点在列表中选择多个项目,然后在我的选项菜单中选择一个选项,执行某种行动的所有列表项。不过,这个问题我似乎运行到的是,我不能选择多个项目,尽管我已经添加了安卓choiceMode =multipleChoice在的.xml ListView控件文件。我意识到,这可以使用复选框或单选按钮来完成,但我会preFER避免。我重视我下面的源$ C ​​$ C。任何帮助将是AP preciated。最后,感谢 http://custom-android-dn.blogspot.com/ 了伟大的教程。谢谢你。

CustomlistviewActivity.java

 包DucNguyen.example.customlistview;

公共类CustomlistviewActivity延伸活动{

    私人的ListView listViewFootballLegend;
    私人上下文CTX;
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_customlistview);
        CTX =这一点;
        名单< FootballLegend> legendList =新的ArrayList< FootballLegend>();
        legendList.add(新FootballLegend(贝利,1940年10月23日(72岁),贝利,巴西));
        legendList.add(新FootballLegend(马拉多纳,1960年10月30日(52岁),马拉多纳,阿根廷));
        legendList.add(新FootballLegend(克鲁伊夫,1947年4月25日(65岁),克鲁伊夫,荷兰));
        legendList.add(新FootballLegend(贝肯鲍尔,1945年9月11日(67岁),贝肯鲍尔,德国));
        legendList.add(新FootballLegend(普拉蒂尼,1955年6月21日(57岁),普拉蒂尼,法国));
        legendList.add(新FootballLegend(罗纳尔多德利马,1976年9月22日(36岁),罗,巴西));

        listViewFootballLegend =(ListView控件)findViewById(R.id.FootballLegend_list);
        listViewFootballLegend.setAdapter(新FootballLegendListAdapter(CTX,R.layout.legend_row_item,legendList));

        // Click事件单列表行
        listViewFootballLegend.setOnItemClickListener(新OnItemClickListener(){

            @覆盖
            公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,长的id){
                FootballLegend O =(FootballLegend)parent.getItemAtPosition(位置);
                Toast.makeText(CustomlistviewActivity.this,o.getName()的toString(),Toast.LENGTH_SHORT。).show();
            }
        });
    }
}
 

FootballLegendListAdapter.java

 包DucNguyen.example.customlistview;

公共类FootballLegendListAdapter扩展ArrayAdapter< FootballLegend> {
    私人诠释的资源;
    私人LayoutInflater充气;
    私人上下文的背景下;
    公共FootballLegendListAdapter(上下文CTX,INT RESOURCEID,名单,其中,FootballLegend>对象){
        超(CTX,RESOURCEID,对象);
        资源= RESOURCEID;
        充气= LayoutInflater.from(CTX);
        上下文= CTX;
    }
    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        convertView =(RelativeLayout的)inflater.inflate(资源,NULL);
        FootballLegend传说=的getItem(位置);
                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);
        字符串URI =绘制/+ Legend.getImage();
        INT imageResource = context.getResources()则getIdentifier(URI,空,context.getPackageName())。
        。绘制的图像= context.getResources()getDrawable(imageResource);
        legendImage.setImageDrawable(图像);

        ImageView的NationImage =(ImageView的)convertView.findViewById(R.id.Nation);
        URI =绘制/+ Legend.getNation();
        imageResource = context.getResources()则getIdentifier(URI,空,context.getPackageName())。
        。图像= context.getResources()getDrawable(imageResource);
        NationImage.setImageDrawable(图像);

        返回convertView;
    }
}
 

FootballLegend.java

 包DucNguyen.example.customlistview;
公共类FootballLegend {
    公共FootballLegend(字符串名称,字符串出生,字符串图像,字符串民族){
        超();
        this.name =名称;
        this.born =出生;
        this.image =图像;
        this.nation =国家;
    }
    私人字符串名称;
    私人字符串出生;
    私人字符串图像;
    私人字符串的国家;

    公共字符串的getName(){
        返回名称;
    }
    公共无效setname可以(字符串nameText){
        NAME = nameText;
    }
    公共字符串getNick(){
        回到出生的;
    }
    公共无效setNick(字符串出生){
        this.born =出生;
    }
    公共字符串的getImage(){
        返回形象;
    }
    公共无效setImage(字符串图像){
        this.image =图像;
    }
        公共字符串getNation(){
        返回的国家;
    }
    公共无效setNation(字符串图像){
        this.image =国家;
    }
}
 

legend_row_item.xml

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=横向
    机器人:填充=5dip>

    <! -  ListRow左侧缩略图 - >
    <的LinearLayout机器人:ID =@ + ID /缩略图
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:填充=3dip
        机器人:layout_alignParentLeft =真
        机器人:layout_marginRight =5dip>
        < ImageView的
            机器人:ID =@ + ID / legendImage
            机器人:layout_width =50dip
            机器人:layout_height =50dip/>
    < / LinearLayout中>

    <的TextView
        机器人:ID =@ + ID / legendName
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignTop =@ + ID /缩略图
        机器人:layout_toRightOf =@ + ID /缩略图
        机器人:文字颜色=#040404
        机器人:字体=SANS
        机器人:TEXTSIZE =15dip
        机器人:TEXTSTYLE =黑体/>

    <的TextView
        机器人:ID =@ + ID / legendBorn
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_below =@ ID / legendName
        机器人:文字颜色=#343434
        机器人:TEXTSIZE =10dip
        机器人:layout_marginTop =1dip
        机器人:layout_toRightOf =@ + ID /缩略图/>

    < ImageView的
        机器人:ID =@ + ID /国家
        机器人:layout_width =45dp
        机器人:layout_height =30dp
        机器人:layout_alignParentRight =真
        机器人:layout_marginRight =5DP
        机器人:layout_centerVertical =真/>

< / RelativeLayout的>
 

activity_customlistview.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>

    <的ListView
        机器人:ID =@ + ID / FootballLegend_list
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:paddingTop =5DP
        机器人:choiceMode =multipleChoice>
    < / ListView控件>

< / LinearLayout中>
 

解决方案
  1. 查看每一个项目,你会在列表中显示,必须落实可检查的。如果实现了这个接口也更新视图的绘制状态。请参见这个答案如何做到这一点。

  2. 设置<选择> 绘制背景为此根查看。为选中状态和正常状态,不同的颜色/可绘制。

  3. 设置的ListView 的选择模式单​​选或多选。

  4. 在列表中的适配器,提供项目的意见与上面创建查看父布局。

现在,的ListView 将负责制定检查其项目的意见/未选中状态。您也可以致电 getCheckedItemIds() getCheckedItemPositions()的ListView 来获取当前选择的项目。

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.

CustomlistviewActivity.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

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

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

<?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

<?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>

解决方案

  1. The root View of each item you will display in list must implement Checkable. When implementing this interface also update the drawable state of View. See this answer for how to do that.

  2. Set a <selector> drawable as background for this root View. Which different color/drawables for checked state and normal states.

  3. Set ListView's choice mode to single choice or multi-choice.

  4. In list adapter, provide item views with above created View as parent layout.

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.

这篇关于选择/高亮自定义适配器列表视图多个项目 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆