2014-10-10 47 views
0

我已經搜索了一個解決方案,並且找不到任何可以指向解決方案的內容。ListView onItemClick不能使用自定義適配器

我在使用一個ListView,其中每個項目都有一個TextView和一個Horizo​​ntalScrollView。 Horizo​​ntalScrollView在運行時充滿了幾個TextView。當用戶點擊其中一個TextView時,我在視圖上切換背景,爲此我擴展了TextView類。

問題是ListView onItemClick不會觸發。我玩了一下代碼,出於某種原因,我不明白,點擊事件有時會觸發,但只有當我點擊每個列表項之間的權利。 我認爲這是因爲我的TextView處理單擊事件或因爲其中一個佈局阻止事件。

編輯:

我想要做的是:

  1. 當TextView的點擊,撥動它的一些視覺效果。

  2. 將其值(文本)添加到數據結構。

我的自定義TextView處理單擊事件罰款,但我不能抓住該適配器中的單擊事件。我需要它,因爲我需要知道ListView中的位置。

public class KeywordListAdapter extends ArrayAdapter<String> implements OnClickListener { 

    private final Context context; 
    private final String[] values; 
    private AspectManager aspectManager; 

    static class ViewHolder { 
      protected TextView aspectName; 
      protected HorizontalScrollView horizContainer; 
      protected LinearLayout keyWrapper; 
      protected KeywordTextView[] keyword; 
    } 

    public KeywordListAdapter(Context context, String[] values) { 
      super(context, R.layout.aspect_list_answer, values); 
      this.context = context; 
      this.values = values; 
      aspectManager = AspectManager.getInstance(); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
      Aspect aspect = aspectManager.getAspectByName(values[position]); 
      View row = convertView; 

      // reuse views 
     if (row == null) { 
      Log.w(Constants.TAG, "row == null"); 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = inflater.inflate(R.layout.aspect_list_answer, parent, false); 

        ViewHolder viewHolder = new ViewHolder(); 

        viewHolder.horizContainer = (HorizontalScrollView) row.findViewById(R.id.horizontalRow); 
        viewHolder.horizContainer.setHorizontalScrollBarEnabled(false); 

        viewHolder.keyWrapper = new LinearLayout(context); 
        viewHolder.keyWrapper.setOrientation(LinearLayout.HORIZONTAL); 
        viewHolder.keyWrapper.setId(0); 

        viewHolder.keyword = new KeywordTextView[aspect.getKeywordCount()]; 
        viewHolder.aspectName = (TextView) row.findViewById(R.id.lblAspect); 

        String[] keywords = aspect.getAspectKeys(); 
        for (int i=0; i< keywords.length; i++){ 
          viewHolder.keyword[i] = new KeywordTextView(context); 
          viewHolder.keyword[i].setOnClickListener(this); 
          viewHolder.keyWrapper.addView(viewHolder.keyword[i]); 
        } 

        viewHolder.horizContainer.addView(viewHolder.keyWrapper); 
        row.setTag(viewHolder); 
     } 

     //Set values 
     ViewHolder holder = (ViewHolder) row.getTag(); 

      //Fill aspect name 
      holder.aspectName.setText(aspect.getAspectName()); 
      holder.aspectName.setTextColor(Color.WHITE); 

      //Fill keywords 
      String[] keywords = aspect.getAspectKeys(); 
      for (int i=0; i< keywords.length; i++){ 
        holder.keyword[i].setKeyword(keywords[i]); 
        holder.keyword[i].setColor(baseColor); 

      //set keyword layout 
        LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
        llp.gravity = Gravity.CENTER_VERTICAL; 
        llp.setMargins(20, 15, 20, 20); 
        holder.keyword[i].setLayoutParams(llp); 
      } 

      return row; 
    } 

    @Override 
    public void onClick(View v) { 
      ((KeywordTextView)v).toggle(); 
    } 

    public class KeywordTextView extends TextView { 
      String keyword; 
      String color; 
      boolean selected; 
      private GradientDrawable gd; 

     public KeywordTextView(Context context) { 
      super(context); 
      gd = new GradientDrawable(); 
      selected = false; 
     } 

     public void setKeyword(String keyword){ 
      this.keyword = keyword; 
      setText(keyword); 
     } 

     public String getKeyword(){ 
      return keyword; 
     } 

     public void setColor(String color){ 
      this.color = color; 
      setText(keyword); 
     } 

     public boolean isKeywordSelected(){ 
      return this.selected; 
     } 

     protected void onDraw (Canvas canvas) { 
      super.onDraw(canvas); 

      //Set style 
      ... 
     } 

     protected void toggle(){ 
      selected = !selected; 
     } 
    } 

}

活動培訓相關部分:

... 
aspectList = (ListView)findViewById(R.id.lvAspectList); 
aspectList.setAdapter(new KeywordListAdapter(this, aspects.split(", "))); 
aspectList.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
      Log.w(TAG, "aspectList onItemClick"); 
     } 
}); 
... 
+0

我認爲它的工作原理是因爲你的列表項目有一個onClick監聽器,它將拉焦。可能是錯誤的。 – zgc7009 2014-10-10 17:35:52

+0

顯示了aspect_list_answer.xml的代碼 – 2014-10-10 17:37:15

回答

0

您遇到的問題是,因爲你已經在列表視圖的內部意見設置OnClickListener,這裏什麼情況是這會覆蓋列表視圖的默認OnClickListener。你最好的選擇是使用這個。

row.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
     //Enter Your Code Here 
     } 

    }); 
+0

如果點擊了一個子TextView,這將不起作用。看我的編輯。 – user3339411 2014-10-10 20:34:21

+0

嗯,如果我的理解正確,你試圖抓住TextView的文本,你剛纔點擊一個列表視圖,並用它做什麼?如果你能進一步闡述,我希望能幫助你。 – Rhynoboy2009 2014-10-10 20:51:44

+0

這個想法是用戶從每個主題中選擇關鍵字(topic = row/horizo​​ntal view)。對於每個選擇,我需要執行選擇效果,並將關鍵字添加到稍後將發送到服務器的關鍵字列表中。所以我需要知道關鍵字(TextView)和被點擊的行。 – user3339411 2014-10-10 21:07:30

相關問題