android – ListView,突出显示所选项目

android – ListView,突出显示所选项目,第1张

概述我在我的活动中创建了一个ListView,另外两个按钮(上一个,下一个)在ListView上移动一个高亮显示.单击两个按钮时,我调用setSelection(pos),但列表视图中没有显示突出显示. 我也尝试使用布局文件自定义列表项,并在其上注册选择器,如下所述: http://android-codes-examples.blogspot.com/2011/03/customized-listv 我在我的活动中创建了一个ListVIEw,另外两个按钮(上一个,下一个)在ListVIEw上移动一个高亮显示.单击两个按钮时,我调用setSelection(pos),但列表视图中没有显示突出显示.

我也尝试使用布局文件自定义列表项,并在其上注册选择器,如下所述:
http://android-codes-examples.blogspot.com/2011/03/customized-listview-items-selection.html

不幸的是,它没有按预期工作.当我触摸列表项时,此方法确实改变了颜色,但是当我调用setSelection()时没有显示突出显示.

layout / main.xml(主要布局):

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:orIEntation="horizontal"    androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent"    androID:weightSum="4"    >    <ListVIEw        androID:ID="@+ID/List_vIEw"        androID:layout_wIDth="match_parent"        androID:layout_height="fill_parent"        androID:layout_weight="1" />    <linearLayout        androID:orIEntation="vertical"        androID:weightSum="2"        androID:layout_wIDth="match_parent"        androID:layout_height="fill_parent"        androID:layout_weight="3">        <button            androID:ID="@+ID/btn_prev"            androID:text="prev"            androID:layout_wIDth="fill_parent"            androID:layout_height="match_parent"            androID:layout_weight="1" />        <button            androID:ID="@+ID/btn_next"            androID:text="next"            androID:layout_wIDth="fill_parent"            androID:layout_height="match_parent"            androID:layout_weight="1" />    </linearLayout></linearLayout>

List.java(活动):

package com.androID.List;import androID.app.Activity;import androID.os.Bundle;import androID.Widget.button;import androID.Widget.ListVIEw;import androID.Widget.TextVIEw;import androID.Widget.tableRow;import androID.Widget.SimpleAdapter;import androID.Widget.SimpleAdapter.VIEwBinder;import androID.util.Log;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.graphics.Point;import androID.graphics.color;import java.util.ArrayList;import java.util.HashMap;public class List extends Activity    implements VIEw.OnClickListener{    private ArrayList<HashMap<String,String>> mList;    private button mPrev;    private button mNext;    private ListVIEw mListVIEw;    private int mposition;    /** Called when the activity is first created. */    @OverrIDe    public voID onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);        mList = new ArrayList<HashMap<String,String>>();        HashMap<String,String> map;        String[] ent = { "USA","India","England","Russia","Europe","Canada","Srilanka","Singapore","Thailand","Australia"};        for (int i=0; i<ent.length; i++)        {            map = new HashMap<String,String>();            map.put("content",ent[i]);            mList.add(map);        }        SimpleAdapter adapter = new SimpleAdapter(this,mList,R.layout.List_item,new String[] {"content"},new int[] {R.ID.List_text});        mListVIEw = (ListVIEw) findVIEwByID(R.ID.List_vIEw);        mListVIEw.setAdapter(adapter);        mposition = 0;        mListVIEw.setSelection(mposition);        mPrev = (button) findVIEwByID(R.ID.btn_prev);        mPrev.setonClickListener(this);        mNext = (button) findVIEwByID(R.ID.btn_next);        mNext.setonClickListener(this);    }    public voID onClick(VIEw vIEw)    {        if (vIEw == mPrev && mposition >= 0)        {            mListVIEw.setSelection(mposition);            mposition--;        }        else if (vIEw == mNext && mposition < mList.size())        {            mListVIEw.setSelection(mposition);            mposition++;        }    }}

layout / List_item.xml(列表项布局):

<?xml version="1.0" enCoding="utf-8"?><linearLayout     xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:chess="http://schemas.androID.com/apk/res/org.pengguang.chess"    androID:orIEntation="vertical"    androID:background="@color/List_bg"    androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent"><TextVIEw    androID:ID="@+ID/List_text"    androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent" /></linearLayout>

color / List_bg.xml(列表项选择器):

<?xml version="1.0" enCoding="utf-8"?><selector xmlns:androID="http://schemas.androID.com/apk/res/androID"><item      androID:state_selected="false"    androID:state_pressed="false"     androID:drawable="@color/grey" /><item     androID:state_pressed="true"     androID:drawable="@color/blue" /><item     androID:state_selected="true"    androID:state_pressed="false"     androID:drawable="@color/red" /></selector>

values / colors.xml(颜色文件):

<?xml version="1.0" enCoding="UTF-8"?><resources>    <color name="blue">#0303ff</color>    <color name="grey">#f7f7f7</color>    <color name="red">#ff0000</color></resources>
解决方法 我没试过.

ListVIEw.setonItemSelectedListener(new OnItemSelectedListener() {                public voID onItemSelected(AdapterVIEw<?> arg0,VIEw arg1,int arg2,long arg3) {                    // Todo auto-generated method stub                    arg1.setSelected(true);                }                public voID onnothingSelected(AdapterVIEw<?> arg0) {                    // Todo auto-generated method stub                }            })
总结

以上是内存溢出为你收集整理的android – ListView,突出显示所选项目全部内容,希望文章能够帮你解决android – ListView,突出显示所选项目所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1126513.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存