2013-09-23 20 views
3

在我的應用程序中,我有一個自定義列表視圖,包含一些文本視圖和圖像。默認圖像視圖包含灰色箭頭標記image.I起來到這裏。我的問題是當用戶點擊列表中的某個項目時,箭頭標記將變成藍色箭頭標記圖像而僅剩下灰色。選定的清單項目圖像將呈藍色。我可以做到這一點。請任何人幫助我。如何添加不同的圖像列表視圖在android中的不同選擇?

感謝提前。

回答

2

您可以通過將整數tag設置爲您的列表項目來創建它們,從而實現此功能。當你做出一個選擇,那麼只得到tag,並與項目index比較,如果條件滿足,根據您的要求,然後作出更改爲index,改變所有其它項扭轉

+0

你能提供任何示例代碼嗎? – user2681425

+0

您可以將標記設置爲項目索引,以將箭頭標記的圖像視圖更改爲圖像,現在在選擇時只檢查選定的項目索引,並將其與圖像標記進行比較,如果相等,則將顏色更改爲藍色,其餘全部更改爲灰色希望你明白! –

1

在適配器中的getView()方法,實現View.onclickLister並點擊一個特定的項目,將該箭頭的可繪圖變爲藍色。

private int resource; 
private Context mContext; 
public int mPosition; 
public static int curSelected = -1; 
static String time ; 


public CustomDateRowAdapter(Context context, int _resource, 
     List<DayPickerTo> objects,) { 
    super(context, _resource, objects); 
    allItems = objects; 
    resource = _resource; 
    mContext = context; 


} 

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

    RelativeLayout rel = null; 
    if (null == convertView) { 

     String inflater = Context.LAYOUT_INFLATER_SERVICE; 
     LayoutInflater vi; 
     vi = (LayoutInflater) getContext().getSystemService(inflater); 
     rel = (RelativeLayout) vi.inflate(resource, rel, true); 

    } else { 
     rel = (RelativeLayout) convertView; 

    } 

    rel.setTag("Txt:" + position); 
    final ImageView checkBox = (ImageView) rel 
      .findViewById(R.id.checkedIcon); 
    if (position == curSelected) { 
     checkBox.setVisibility(View.VISIBLE); 
     mPosition = curSelected; 
    } else { 
     checkBox.setVisibility(View.INVISIBLE); 

    } 

    checkBox.setTag(position); 
    dateText.setTag("text:" + position); 

    final RelativeLayout rel1 = rel; 

    // click Listener of day list 
    rel.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      int checkslot=0; 
      String seletedPosition = (String) v.getTag(); 
      String[] splitted = seletedPosition.split(":"); 
      int tag = Integer.parseInt(splitted[1]); 
      mPosition = tag; 


      clearChecked(v, parent); 

      ImageView checked = (ImageView) rel1.findViewWithTag(tag); 

      if (null != checked) { 
       checked.setVisibility(ImageView.VISIBLE); 
       checked.setTag("Checked"); 
       notifyDataSetChanged(); 
      } 


     } 
    }); 

    return rel; 
} 



void clearChecked(View view, View parent) { 
    // clearing current selecting 
    ImageView checkedBox = (ImageView) parent.findViewWithTag(curSelected); 
    if (checkedBox != null) { 
     checkedBox.setVisibility(View.INVISIBLE); 
     checkedBox.invalidate(); 
    } 
    // getting new current selection 
    String tag = view.getTag().toString(); 
    String[] splittedTag = tag.split(":"); 
    Logging.i(TAG, "[clearChecked]", "Selected: " + tag); 
    curSelected = Integer.parseInt(splittedTag[1]); 
    // setting new current selection 
    ImageView newChecked = (ImageView) parent.findViewWithTag(curSelected); 
    if (newChecked != null) { 
     newChecked.setVisibility(View.VISIBLE); 
     newChecked.invalidate(); 
    } 

} 


} 

我正在切換我的imageview可見性。喲必須改變那些地方的drawable。

+0

但是當我點擊另一個項目時,以前的項目也是藍色的,但它變成了灰色。 – user2681425

+0

您必須通過將標記設置到您點擊的視圖來使用標誌。並使用箭頭的布爾值(按鈕/複選框)進行設置。當你重繪時,你要確保你只改變了你所設置的選定布爾值爲true的特定箭頭的drawable。 – Ritaban

+0

看到我編輯的答案。希望這可能有助於我有similla要求切換基於點擊的圖像的可見性。你可以找到邏輯,你必須實現一些你自己的代碼。 – Ritaban

相關問題