1

我需要让一个列表视图保持选定的视图突出显示,直到选择另一个项目,在这种情况下,前一个视图删除突出显示,最后点击的获得突出显示。

我试图不这样做并使用单选按钮,但它不起作用。

4

2 回答 2

2

其实很简单:

在单个列表视图项的自定义布局中,确保它具有以下属性:

android:background="?android:attr/activatedBackgroundIndicator" >

例如,以下是 SDK 附带的HoneycombGallery 示例中的列表视图项布局(位于title_list_item.xml文件中:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:gravity="bottom"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:background="?android:attr/activatedBackgroundIndicator" >
</TextView>

该属性将导致背景颜色根据单个列表项是否被选中而改变。

于 2012-04-27T21:35:11.187 回答
1

我是 android 新手,所以我发现这个问题很有趣,最后我在 1 个半小时后成功了;-)

这是代码:
main.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/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>


listinflater.xml

    android:id="@+id/tvItem"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="TextView" />


PreservelistitemActivity.java

package com.mehuljoisar;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class PreservelistitemActivity extends Activity {

//declaring variable
ListView listView1;
MyAdapter adapter1;
Integer length,i;
String[] data = {"Mehul","Milind","Aamir","Amitabh","Mehul","Milind","Aamir","Amitabh","Mehul","Milind","Aamir","Amitabh","Mehul","Milind","Aamir","Amitabh","Mehul","Milind","Aamir","Amitabh","Mehul","Milind","Aamir","Amitabh","Mehul","Milind","Aamir","Amitabh","Mehul","Milind","Aamir","Amitabh","Mehul","Milind","Aamir","Amitabh"};


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //binding to the view
    listView1 = (ListView)findViewById(R.id.listView1);

    //set adapter
    adapter1=new MyAdapter(PreservelistitemActivity.this,data);
    listView1.setAdapter(adapter1);

}

public class MyAdapter extends BaseAdapter{
    private final Activity context;
    private final String[] data;

    public MyAdapter(Activity context,
            String[] data) {
        // TODO Auto-generated constructor stub
        super();
        this.context=context;
        this.data=data;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.length;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = context.getLayoutInflater();
        convertView = inflater.inflate(R.layout.listinflater, null);

        final TextView tvItem = (TextView)convertView.findViewById(R.id.tvItem);

        tvItem.setText(data[position]);

        listView1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

/*          length=parent.getAdapter().getCount();

                for(i=0;i<length;i++)
                {
                    //clear
                    if(i==position)
                    {
                    //set
                    }
                }
*/          

                v.setSelected(true);    
            }
        });

        return convertView;
    }


}
}
于 2012-04-27T23:06:57.390 回答