Android ListView列表:选择器未工作
我对android和我的应用程序是一个新的列表视图,我想在选择时更改列表项的颜色,但它不起作用 我已经检查了几乎所有的堆栈溢出链接,并尝试了他们所有,但没有运气。。。请检查我的密码。。。告诉我问题出在哪里Android ListView列表:选择器未工作,android,android-layout,android-listview,Android,Android Layout,Android Listview,我对android和我的应用程序是一个新的列表视图,我想在选择时更改列表项的颜色,但它不起作用 我已经检查了几乎所有的堆栈溢出链接,并尝试了他们所有,但没有运气。。。请检查我的密码。。。告诉我问题出在哪里 public class Main extends Activity { private ListView listView; private String[] menuArray; private List<String> menuList = new
public class Main extends Activity {
private ListView listView;
private String[] menuArray;
private List<String> menuList = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
menuArray = getResources().getStringArray(R.array.Menu);
menuList = Arrays.asList(menuArray);
listView = (ListView) findViewById(R.id.listView_main_menu);
listView.setAdapter(new EfficientAdapter(this,menuList));
}
/******************************** EfficientAdapter ************************************/
public class EfficientAdapter extends BaseAdapter{
private LayoutInflater mInflater;
@SuppressWarnings("unused")
private String TAG=EfficientAdapter.class.getSimpleName();
@SuppressWarnings("unused")
private Context context;
private List<String> data= new ArrayList<String>();
public EfficientAdapter(Context context,List<String> data) {
super();
mInflater = LayoutInflater.from(context);
this.context=context;
this.data.addAll(data);
}
public EfficientAdapter(Context context) {
super();
mInflater = LayoutInflater.from(context);
this.context=context;
}
public View getView(final int position, View convertView, final ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = mInflater.inflate(R.layout.main_list, null);
holder = new ViewHolder();
holder.textLine = (TextView) convertView.findViewById(R.id.textView_mainlist_item);
convertView.setTag(holder);
}
holder =(ViewHolder) convertView.getTag();
holder.textLine.setText(getItem(position));
convertView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
return convertView;
}
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
public String getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
class ViewHolder {
TextView textLine;
}
}
}
public类主扩展活动{
私有列表视图列表视图;
私有字符串[]menuArray;
private List menuList=new ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
menuArray=getResources().getStringArray(R.array.Menu);
menuList=Arrays.asList(menuArray);
listView=(listView)findViewById(R.id.listView\u主菜单);
setAdapter(新的EfficientAdapter(this,menuList));
}
/********************************效率捕捉器************************************/
公共类EfficientAdapter扩展BaseAdapter{
私人停车场;
@抑制警告(“未使用”)
私有字符串标记=EfficientAdapter.class.getSimpleName();
@抑制警告(“未使用”)
私人语境;
私有列表数据=新的ArrayList();
公共效率适配器(上下文、列表数据){
超级();
mInflater=LayoutInflater.from(上下文);
this.context=context;
this.data.addAll(数据);
}
公共效率适配器(上下文){
超级();
mInflater=LayoutInflater.from(上下文);
this.context=context;
}
公共视图getView(最终整型位置、视图转换视图、最终视图组父视图){
视窗座;
if(convertView==null){
convertView=mInflater.充气(R.layout.main\u列表,空);
holder=新的ViewHolder();
holder.textLine=(TextView)convertView.findViewById(R.id.TextView\u mainlist\u项);
convertView.setTag(支架);
}
holder=(ViewHolder)convertView.getTag();
holder.textLine.setText(getItem(position));
setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
}
});
返回视图;
}
public int getCount(){
//TODO自动生成的方法存根
返回data.size();
}
公共字符串getItem(int位置){
//TODO自动生成的方法存根
返回数据。获取(位置);
}
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回0;
}
类视图持有者{
文本视图文本行;
}
}
}
这是我的带有列表视图的xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/Gainsboro"
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:id="@+id/listView_main_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice"
android:divider="@color/DarkGray"
android:dividerHeight="1dip"
android:fadingEdge="none"
android:listSelector="@drawable/row_color"
android:scrollbars="none" >
</ListView>
</RelativeLayout>
row\u color.xml
<item android:state_pressed="false" android:state_focused="false"
android:drawable="@color/Green" />
<item android:state_focused="true"
android:drawable="@color/Blue" />
<item android:state_pressed="true"
android:drawable="@color/Brown" />
<item
android:state_selected="true" android:drawable="@color/Cyan" />
main_list.xml;此xml包含正在膨胀的文本视图
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView_mainlist_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:text="TextView"
android:textColor="@color/DarkSlateGray"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/TextView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right|center"
android:paddingRight="30dp"
android:paddingTop="6dp"
android:text=">"
android:textColor="@color/DarkSlateGray"
android:textSize="28sp"
android:textStyle="bold" />
</FrameLayout>
刚刚发现只有这部分工作正常
<item android:state_pressed="false" android:state_focused="false"
android:drawable="@color/Green" />
而不是
convertView = mInflater.inflate(R.layout.main_list, null);
试一试
编辑:
我明白你现在想做什么了。您不希望在ListView上有android:ListSelector
,而是在每个FrameLayout
上。因此:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:listSelector="@drawable/row_color" >
您需要将其应用于每个单独的视图,以便在按下时使其更改颜色,等等。访问
希望它对您有用我只是在您的代码中提出了一个潜在问题。但是你想在哪里改变物品的颜色呢?我已经编辑了我的问题并添加了xml文件代码。我也做了你在回答中建议的改变,但什么也没发生。我想更改列表项的颜色,请参见以下代码行android:listSelector=“@drawable/row_color”您是否试图更改单个项目的颜色,使其唯一、颜色相同,或使实际列表本身具有可绘制的颜色?我只想在单击/选中列表项时更改其颜色。例如,如果我有一个国家列表,当我选择日本时,它应该突出显示。在框架布局的main_list.xml中添加android:background=“@drawable/row_color”,并从列表视图中删除。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:listSelector="@drawable/row_color" >