当我在自定义适配器

时间:2018-04-06 09:41:51

标签: android listview android-studio

在ListView中,我通过 CustomAdapter 类(扩展ArrayAdapter)添加TextView和ImageButton。当我在CustomAdapter类中将onClickListener添加到ImageButton时,ListView onItemClickListener不工作。如果我想选择多个列表以及监听器不工作的单个列表和并行ImageButton onClickListener工作。 我想要ListView和ImageButton onClickListener和onItemClickListener在我的Activity 中工作。如何解决这个问题?< / p>

任何人都会提出一些建议!

以下代码:

public class ViewHolder {
     TextView username;
     ImageButton download;
     TextView time;
     ImageView contactpic;  }

    public interface ButtonListener {
        public void onButtonClickListner(int position, VoiceMailRecords value);
    }

    public void setCustomButtonListner(ButtonListener listener) {
        this.customListner = listener;
    }
    public View getView(final int position, View convertView, ViewGroup parent)
    {
            ViewHolder viewHolder;
            viewHolder = new ViewHolder();
            if(convertView==null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.recorded_item, parent, false);
                //TextView date = (TextView) convertView.findViewById(R.id.date);
                viewHolder.time = (TextView) convertView.findViewById(R.id.callingTime);
                //TextView number = (TextView) convertView.findViewById(R.id.callerNumber);
                viewHolder.username = (TextView) convertView.findViewById(R.id.callerName);
                viewHolder.contactpic = (ImageView) convertView.findViewById(R.id.imageView_round);
                //   ImageView checked = (ImageView) convertView.findViewById(R.id.is_checked);
                viewHolder.download = (ImageButton) convertView.findViewById(R.id.is_download);
                convertView.setTag(viewHolder);
            }
            else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            final VoiceMailRecords user = getItem(position);
            viewHolder.username.setText(user.getCallerNumber());

            return convertView;


    }

活动

voiceMailRecordAdapter=new VoiceMailRecordAdapter(this,voiceMailRecords,this);
voicemailList.setAdapter(voiceMailRecordAdapter);
voiceMailRecordAdapter.notifyDataSetChanged();

voicemailList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(ListVoiceMailRecord.this,"Position :"+position+" clicked",Toast.LENGTH_LONG).show();
        return true;
    }
});

@Override
public void onButtonClickListner(int position, VoiceMailRecords value) {
    Log.d(TAG, "VoiceMailApi onButtonClickListner " );
    Toast.makeText(ListVoiceMailRecord.this,"onButtonClickListner :"+position+"",Toast.LENGTH_LONG).show();
}

3 个答案:

答案 0 :(得分:1)

您需要为您的ImageButton设置 focusableInTouchMode false

在imageButton中添加此行

android:clickable="false"
android:focusableInTouchMode="false"
android:focusable="false"

instead of onItemClick() 尝试 onClick()for convertview

convertView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          Log.d("clicked"," yes");
      }
}

答案 1 :(得分:1)

很多时候android:focusable="false"android:focusableInTouchMode="false"都可以解决问题,但如果这些不起作用,那么有时列表将无法生成Click Listener。在这种情况下,您可能需要再添加一个属性。

android:descendantFocusability="blocksDescendants" 

此属性必须添加到您提供ListView元素的XML的最顶层布局中。

答案 2 :(得分:0)

您可以在适配器中添加界面,并可以处理点击活动或片段

     public interface  ButtonClick{
            void Click(View view,int position);
        }
    public Adapter(Context context, ButtonClick buttonclick){

            this.buttonclick=buttonclick;
          this.context=context;
        }
        ButtonClick buttonclick;

     yourimage.setTag(position);
           yourimage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos= (int) v.getTag();
                    if(buttonclick!=null){
                        buttonclick.Click(v,pos);

                    }

                }
            });

您可以在设置适配器时处理单击活动

    Adapter adapter = new Adapter(Youractivity.this, new Adapter.ButtonClick() {
                        @Override
                        public void Click(View view, int position) {
                            // your logic
                        }
                    });

希望这有帮助。

this is how i have implemented in multiple projects