2012-07-26 52 views
0

我是新来的Android和我的使用列表视图,我想改变选择列表项的颜色,但它无法正常工作。Android的ListView控件列表:selecter不沃金

我检查堆栈溢出几乎所有环节,尝试所有这些,但没有运气...请检查我的代码......并且告诉我哪里出了问题。

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文件

<?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> 

就发现,只有这部分的

convertView = mInflater.inflate(R.layout.main_list, null); 

做工精细

<item android:state_pressed="false" android:state_focused="false" 
     android:drawable="@color/Green" /> 
+0

我只是把你的代码潜在的问题。但你在哪里试图改变项目的颜色?会很高兴地告诉我们,正是那就是:) – Andy 2012-07-26 19:32:34

+0

我已经编辑我的问题,并添加XML文件中的代码。我也做出了你在回答中提出的改变,但没有发生任何事情。我想要更改列表项颜色,请参阅此代码行android:listSelector =“@ drawable/row_color” – Romio 2012-07-26 19:46:12

+0

您是否尝试更改单个项目的颜色,以使它们是唯一的,使它们具有相同的颜色,还是具有实际列表本身具有可绘制的颜色? – Andy 2012-07-26 19:53:45

回答

1

而是试图

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" > 

您需要将其应用到每一个人观点,才能有按压时他们改变颜色等