2010-12-10 43 views
11

tôi gặp vấn đề sau.Cách đánh dấu ListView-Items

tôi có một ListView với các hàng tùy chỉnh bao gồm một lần xem hình ảnh và một lần xem văn bản. mã XML của TextView là

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="26px" 
    android:layout_marginLeft="3px" 
    android:singleLine="true" 
    android:ellipsize="end" 
    android:textColorHighlight="#FEC403" 
/> 

sau đó tôi có một itemclicklistener rằng hoạt động tốt và tôi muốn làm nổi bật TextView đã được nhấp bằng cách làm như sau.

public void onItemClick(AdapterView<?> adaptview, View clickedview, int position, 
       long id) { 
      //TODO: ACTIONS 
      String pathtofile = (String) adaptview.getItemAtPosition(position); 
      View rowview = (View) adaptview.getChildAt(position); 
      rowview.setSelected(true);} 

tôi muốn màu đánh dấu là "# FEC403" trong xml (có màu cam nhạt) nhưng màu đánh dấu vẫn có màu xám. Vậy làm thế nào để thiết lập highlightcolor một cách chính xác?

cảm ơn trước

EDIT:

đây là cách tôi đã làm nó cuối cùng:

đây là ListView tập tin mục xml của tôi:

<?xml version="1.0" encoding="UTF-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/rowselector" 
> 

<ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/musicicon" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:src="@drawable/musicicon" 
    android:paddingLeft="3px" 
/> 


<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="26px" 
    android:layout_marginLeft="3px" 
    android:singleLine="true" 
    android:ellipsize="end" 
    android:focusable="false" 
/> 

và rowselector.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_selected="true" 
    android:drawable="@color/orange" /> 
</selector> 

và cuối cùng OnItemClick của tôi là rất ngắn bây giờ:

@Override 
public void onItemClick(AdapterView<?> adaptview, View clickedview, int position, 
       long id) { 
      //TODO: ACTIONS 
      clickedview.setSelected(true); 
} 
+0

bản sao có thể có của [Màu bộ chọn danh sách Android] (http: // stackoverflow.com/questions/2038040/android-listview-selector-color) – MKJParekh

+0

Thật không may, kỹ thuật này không thể được sử dụng để đánh dấu nhiều mục trong listview, bởi vì chỉ có một mục có thể được chọn tại một thời điểm. – faizal

Trả lời

8

Bạn nên sử dụng một Selector.

This câu hỏi và câu trả lời của mình có thể giúp đỡ ....

10
@Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) 
     { 
      for(int a = 0; a < parent.getChildCount(); a++) 
      { 
       parent.getChildAt(a).setBackgroundColor(Color.BLACK); 
      } 

      view.setBackgroundColor(Color.RED); 

     } 
0

Trong hoạt động của bạn:

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long itemid) { 
      itemOnclickList(position, view); 
     } 
    }); 

    public void itemOnclickList(int position, View view) {  
    if (listview.isItemChecked(position)) {       
     view.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_checked));    
    } else {  
    view.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_uncheck));   
    } 
    adapter.notifyDataSetChanged(); 

} 

Trong Adaptor của bạn:

public View getView(int position, View view, ViewGroup parent) { 
    View convertView = inflater.inflate(R.layout.listdocument_item, null);  

     ListView lvDocument = (ListView) parent; 
     if (lvDocument.isItemChecked(position)) { 
      convertView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.image_checked));    
     } else { 
      convertView.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.image_uncheck));    
     } 

    return convertView; 
} 

Chúc may mắn!

0

Đây là cách nó nên tìm sử dụng selector:

Tạo một file trong drawable gọi selector_listview_item.xml, trông giống như (thay đổi màu sắc cho riêng bạn):

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- pressed --> 
    <item android:drawable="@color/md_grey_200" android:state_pressed="true" /> 
    <!-- default --> 
    <item android:drawable="@color/white" /> 
</selector> 

Bây giờ trong cách bố trí của bạn trong đó mô tả hàng danh sách (ví dụ: layout_list_row.xml), Trên bố cục gốc thêm android:background="@drawable/selector_listview_item", Do đó bố cục sẽ trông giống như sau:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:background="@drawable/selector_listview_item" <!-- this is the important line --> 
    android:orientation="vertical"> 

    <!-- more stuff here --> 
</LinearLayout> 

(Lưu ý: bạn có thể muốn thêm android:focusable="false" vào các mục của bạn trong hàng listview đó, để làm cho hàng có thể nhấp, như đã đề cập here)

Xong.

Các vấn đề liên quan