2015-04-01 57 views
0

對不起,因爲標題不好。列表視圖更新視圖哪些未觸及

我在運行時添加一個切換按鈕到列表視圖。 當我點擊按鈕時,它的狀態發生了變化。但問題是,當我向下滾動其他項目也得到選擇。

例如,如果點擊第一個切換按鈕,第六個切換按鈕也會被檢查。

這是一個非常奇怪的行爲。

下面是acitivty

 <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <RelativeLayout 
      android:id="@+id/title_bar" 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:background="@color/eros_now_title_bar"> 

      <ImageView 
       android:id="@+id/back_button_image" 
       android:layout_width="@dimen/toolbar_imageview_width_height" 
       android:layout_height="@dimen/toolbar_imageview_width_height" 
       android:layout_centerVertical="true" 
       android:layout_margin="@dimen/back_image_margin" 
       android:src="@drawable/ic_arrow_back" /> 

      <ImageButton 
       android:id="@+id/back_button" 
       android:layout_width="@dimen/toolbar_imagebutton_width_height" 
       android:layout_height="@dimen/toolbar_imagebutton_width_height" 
       android:layout_centerVertical="true" 
       android:layout_marginRight="20dp" 
       android:background="@drawable/npd_back_feedback" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignTop="@+id/back_button_image" 
       android:layout_toRightOf="@+id/back_button" 
       android:fontFamily="sans-serif-regular" 
       android:text="Invite friends" 
       android:textColor="@color/white" 
       android:textSize="18sp" 
       android:textStyle="bold" /> 

     </RelativeLayout> 

     <EditText 
      android:id="@+id/search_text" 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:layout_below="@+id/title_bar" 
      android:layout_margin="15dp" 
      android:hint="Search" 
      android:textColorHint="@color/hint_color" 
      android:drawableRight="@drawable/search_blue_white"/> 

     <ListView 
      android:id="@+id/friends_list" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/search_text" 
      android:layout_above="@+id/invite_count_view" 
      android:layout_marginTop="15dp" 
      android:divider="@color/transparent"> 

     </ListView> 

     <RelativeLayout 
      android:id="@+id/invite_count_view" 
      android:layout_width="match_parent" 
      android:layout_height="56dp" 
      android:layout_alignParentBottom="true"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:fontFamily="sans-serif-light" 
       android:textSize="18sp" 
       android:textColor="@color/eros_now_title_bar" 
       android:text="10 more invites to go" 
       android:layout_centerInParent="true" 
       /> 


      </RelativeLayout> 
    </RelativeLayout> 

這裏的XML代碼是因爲我在適配器

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/card_view" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="10dp" 
    android:paddingBottom="10dp"> 

    <com.makeramen.roundedimageview.RoundedImageView 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/person_pic" 
     android:layout_width="60dp" 
     android:layout_height="60dp" 
     android:layout_marginLeft="26dp" 
     android:layout_centerVertical="true" 
     app:riv_corner_radius="50dip" 
     android:src="@drawable/ic_person_grey600_36dp"/> 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="150dp" 
     android:layout_height="wrap_content" 
     android:singleLine="true" 
     android:layout_toRightOf="@+id/person_pic" 
     android:layout_marginLeft="17dp" 
     android:layout_centerVertical="true" 
     android:textColor="@color/black" 
     android:fontFamily="sans-serif-regular" 
     android:textSize="16sp" 
     android:text="Grumpy brother" 
     android:ellipsize="end"/> 

    <!--<ToggleButton--> 
     <!--android:layout_width="wrap_content"--> 
     <!--android:layout_height="wrap_content"--> 
     <!--android:layout_alignParentRight="true"--> 
     <!--android:layout_marginRight="35dp"--> 
     <!--android:layout_centerVertical="true"--> 
     <!--android:background="@drawable/invite_selector"--> 
     <!--android:textOn=""--> 
     <!--android:textOff=""--> 
     <!--android:text="" />--> 

</RelativeLayout> 

這裏充氣視圖中的XML代碼爲getView

public View getView(int i, View convertView, ViewGroup parent) { 

Log.e("getView", "name = " + contacts.get(i).getName()); 

if (convertView == null) { 
    convertView = LayoutInflater.from(activity).inflate(R.layout.friend_view, parent, false); 
} 
    TextView name = (TextView) convertView.findViewById(R.id.name); 
    RoundedImageView image = (RoundedImageView) convertView.findViewById(R.id.person_pic); 

    ToggleButton selector = new ToggleButton(activity); 
    if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) 
     selector.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.ic_add_black_24dp)); 
    else 
     selector.setBackground(activity.getResources().getDrawable(R.drawable.ic_add_black_24dp)); 

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); 
    params.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE); 
    params.setMargins(0, 0, getPx(35), 0); 
    selector.setLayoutParams(params); 
    selector.setTextOff(""); 
    selector.setText(""); 
    selector.setTextOn(""); 
    selector.setOnCheckedChangeListener(this); 
    selector.setTag(String.valueOf(i)); 
    selector.setChecked(contacts.get(i).getSelectedState()); 
    contacts.get(i).setButton(selector); 
    RelativeLayout card = (RelativeLayout) convertView.findViewById(R.id.card_view); 

    card.addView(selector); 

    try { 
     Bitmap bitmap = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), contacts.get(i).getURI()); 
    } catch (IOException e) { 
     contacts.get(i).setURI(Uri.parse("")); 
    } 

    name.setText(contacts.get(i).getName()); 
    if (!contacts.get(i).getURI().toString().equals("")) 
     image.setImageURI(contacts.get(i).getURI()); 
    else 
     image.setImageDrawable(activity.getResources().getDrawable(R.drawable.ic_person)); 

return convertView; 
代碼

}

回答

0

我看到你用contacts.get(i).getSelectedState()來設置ToggleButton狀態getview

您是否使用Adapter.notifyDatasetChanged()來更新列表視圖的狀態?

如果沒有,請在切換按鈕後添加該代碼。

0

因爲您的項目視圖被重用,所以只有在convertView爲null時才創建ToggleButton。如果convertView不爲null,請從convertView中獲取並設置它的屬性。

使用setTag和findViewWithTag來標識您的ToggleButton。