基本适配器onclick无法正常工作

时间:2015-10-26 11:04:52

标签: android list onclick baseadapter android-viewholder

我正在尝试使用材质对话框库Material Dialog,我无法使回调适用于自定义适配器。 项目显示完美,但点击项目我没有得到任何回调。

基础适配器

public class EcardBaseAdapter extends BaseAdapter implements View.OnClickListener {

    private LayoutInflater mInflater;
    private List<? super Greeting> items;
    private Context context;
    private final int width;
    private final int height;

    public EcardBaseAdapter(List<Greeting> items, Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        this.items = items;
        final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        this.width = metrics.widthPixels;
        this.height = metrics.heightPixels;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int i) {
        return items.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

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

        View view;
        ViewHolder holder;
        if (convertView == null) {
            view = mInflater.inflate(R.layout.custom_item, parent, false);
            holder = new ViewHolder();
            holder.avatar = (ImageView) view.findViewById(R.id.image_path);
            holder.name = (TextView) view.findViewById(R.id.custom_event_name);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }

        if (items.get(position) instanceof ECard) {
            ECard eCard = (ECard) items.get(position);
            Log.i("test", eCard.getImage() + "   " + eCard.getSubject());
            if (eCard.getImage() != null) {
                Glide.with(context)
                        .load(eCard.getImage())
                        .fitCenter()
                        .override(width, height / 2)
                        .into(holder.avatar);
                //holder.avatar.setImageURI(Uri.parse(eCard.getImage()));
            }
            holder.name.setTextColor(ContextCompat.getColor(context, R.color.darkGrey));
            holder.name.setText(eCard.getSubject());
        }

        return view;
    }

    @Override
    public void onClick(View view) {
        Log.i("click", "itemclick");
    }

    private class ViewHolder {
        public ImageView avatar;
        public TextView name;
    }
}

使用适配器的活动中的代码

new MaterialDialog.Builder(birthday_details.this)
                    .title("Ecard")
                    .adapter(adapter,
                            new MaterialDialog.ListCallback() {
                                @Override
                                public void onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                                    Toast.makeText(birthday_details.this, "Clicked item " + which, Toast.LENGTH_SHORT).show();
                                }
                            })
                    .show();

1 个答案:

答案 0 :(得分:0)

我不理解您的所有代码,但我似乎永远不会打电话

view.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Log.i("click", "itemclick");
    }
}; 

或在你的情况下

view.setOnClickListener(this);