我需要制作一个列表视图,保持所选视图突出显示,直到选择了另一个项目,在这种情况下,前一个视图会删除突出显示,最后点击会获得突出显示。
我尝试不使用单选按钮这样做,但它无法正常工作。
答案 0 :(得分:2)
实际上非常简单:
在单个列表视图项的自定义布局中,确保它具有以下属性:
android:background="?android:attr/activatedBackgroundIndicator" >
举例来说,这是SDK随附的HoneycombGallery sample列表视图项目布局(位于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>
该属性将根据是否选择单个列表项来更改背景颜色。
答案 1 :(得分: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;
}
}
}