簡體   English   中英

ListView onItemClick不適用於自定義適配器

[英]ListView onItemClick not working with custom adapter

我一直在尋找解決方案,但找不到任何可以使我找到解決方案的東西。

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

問題是ListView onItemClick不會觸發。 我稍微玩了一下代碼,由於某種原因,我不明白,有時會觸發click事件,但僅當我在每個列表項之間單擊鼠標右鍵時才會觸發。 我認為是因為我的TextView處理click事件,還是因為其中一種布局阻止了該事件。

編輯:

我想做的是:

  1. 單擊TextView時,在其上切換一些視覺效果。

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

我的自定義TextView可以很好地處理click事件,但是我無法在適配器中獲取該click事件。 我需要它,因為我需要知道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");
        }
});
...

您遇到的問題是因為您已在列表視圖內的視圖上設置了OnClickListener,這是在此情況下,它會覆蓋列表視圖的默認OnClickListener。 最好的選擇就是使用它。

row.setOnClickListener(new OnClickListener(){

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

  });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM