我是Android的新用户和使用列表视图的新手,我想在选择时更改列表项颜色,但它不起作用。
我已检查堆栈溢出的几乎所有链接并尝试了所有但没有运气...请检查我的代码...并告诉我问题出在哪里。
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;
}
}
}
这是我的带有列表视图的
<?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_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包含正在膨胀的textview
<?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" />
答案 0 :(得分:1)
而不是
convertView = mInflater.inflate(R.layout.main_list, null);
尝试
convertView = mInflater.inflate(R.layout.main_list, parent, false);
修改强>:
我看到你现在要做的事情。您不希望在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" >
。所以:
{{1}}
您需要将它应用于每个单独的视图,以便在按下时更改颜色等。
答案 1 :(得分:0)