Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – ListView,突出显示所选项目大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的活动中创建了一个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://scheR_176_11845@as.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;
    privat@R_618_9473@ 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://scheR_176_11845@as.android.com/apk/res/android"
    xmlns:chess="http://scheR_176_11845@as.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://scheR_176_11845@as.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

                }

            })
@H_502_56@

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。