2016-02-02 77 views
1

下面是我的代碼在getView()目前的snipset。在ListView中的開關自動獲取檢查Android滾動時

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

    final ViewHolder holder; 

    final InterestItems temp; 
    temp = getItem(position); 
    if (convertView == null) { 
     LayoutInflater inflater = LayoutInflater.from(context); 
     convertView = inflater.inflate(layoutResource, null, false); 

     holder = new ViewHolder(); 
     holder.tv_interest_name = (TextView) convertView.findViewById(R.id.tv_interestName_custom_row); 
     holder.switchCompat = (SwitchCompat) convertView.findViewById(R.id.switch_custom_row); 

     convertView.setTag(holder); 
     if(temp.isInterested) 
     { 
      holder.switchCompat.setChecked(true); 
     } 

    } 
    else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 


    holder.tv_interest_name.setText(temp.getInterestName()); 



    holder.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      String name = temp.getInterestName(); 
      if (isChecked) { 
       if (interfaceObject != null) { 
        interfaceObject.addedInterest(name, position); 
        list.get(position).setIsInterested(true); 
       } 
      } else { 
       if (interfaceObject != null) { 

        interfaceObject.removedInterest(name, position); 
        list.get(position).setIsInterested(false); 

       } 
      } 
     } 
    }); 

    return convertView; 
} 

當我自動滾動某些開關得到檢查,但沒有值插入到數組中,也沒有調試器被擊中在特定點。我不明白爲什麼會發生。任何來自你方的幫助將不勝感激。

回答

0

將最終設置爲View,ViewGroup和LayoutInflater。希望它有幫助。

0

使用下面的代碼:

listView.setOnScrollListener(lv_onScrollListener); 

private OnScrollListener lv_onScrollListener = new OnScrollListener() { 
    public void onScroll(AbsListView view, int firstVisibleItem, 
      int visibleItemCount, int totalItemCount) { 
    } 

    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) { 
      adapter.setScroll(false); 
      adapter.notifyDataSetChanged(); 
     } else{ 
      adapter.setScroll(true); 
     } 
    } 
}; 

在適配器類:

Boolean isScroll = false; 

public void setScroll(Boolean status) { 
    isScroll = status; 
} 
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, 
      boolean isChecked) { 
     if (!isScroll) 
     { 
     //Do it here 
     } 
    } 
}); 
+0

正如我在上面的問題setOnCheckedChangeListener滾動過程中,從來沒有所謂的陳述我還是嘗試瞭解決方案,但它沒有幫助。還有什麼我可以嘗試。 – Sam

+0

你想在列表視圖滾動期間做什麼? –