android将OnClickListener表单适配器移动到ListFragment

时间:2014-03-03 23:41:33

标签: android listview onclicklistener


我有ListFragment和自定义适配器 列表中的每一行都包含两个TextView和一个ImageButton。 我想在单击图像按钮时调用方法。

public class MyCustomAdapter extends SimpleAdapter {

private final Context context;
private final List<? extends Map<String,? >> data;
private final String[] from;

    public MyCustomAdapter(Context context, List<? extends Map<String, ?>> data, String[] from) {
        super(context, data, R.layout.list_item, from, new int[]{R.id.firstLine, R.id.secondLine});
        this.context=context;
        this.data=data;
        this.from=from;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_item, parent, false);
        TextView firstLine = (TextView) rowView.findViewById(R.id.firstLine);
        TextView secondLine = (TextView) rowView.findViewById(R.id.secondLine);
        firstLine.setText((CharSequence) data.get(position).get(from[0]));
        secondLine.setText((CharSequence) data.get(position).get(from[1]));
        ImageButton imageButton = (ImageButton) rowView.findViewById(R.id.tourItemDelete);
        final long tourId = (Long) data.get(position).get("key");
        imageButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //do something dependend on tourId


            }
        });


        return rowView;
    }

我想在我的ListFragment中使用onClickListener

我的ListFragment中的onListItemClick方法仅在我单击其中一个TextView时调用,而不是在我单击ImageButton时调用。

  1. 是否可以将此onClickListener从我的适配器移动到ListFragment? tourId也应该从我的适配器传递给ListFragment中的方法。
  2. 为什么单击ImageButton时我的ListFragment中的onListItemClick方法没有被调用?

2 个答案:

答案 0 :(得分:1)

我的一位朋友帮助我,并告诉我如何解决我的问题。

我在我的适配器中创建了一个接口,并在我的片段中实现了它。

public class MyCustomAdapter extends SimpleAdapter {

private OnImgClicked mImageClicked;
private final Context context;
private final List<? extends Map<String,? >> data;
private final String[] from;

public MyCustomAdapter(Context context, List<? extends Map<String, ?>> data, String[] from) {
    super(context, data, R.layout.list_item, from, new int[]{R.id.firstLine, R.id.secondLine});
    this.context=context;
    this.data=data;
    this.from=from;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.list_item, parent, false);
    TextView firstLine = (TextView) rowView.findViewById(R.id.firstLine);
    TextView secondLine = (TextView) rowView.findViewById(R.id.secondLine);
    firstLine.setText((CharSequence) data.get(position).get(from[0]));
    secondLine.setText((CharSequence) data.get(position).get(from[1]));
    ImageButton imageButton = (ImageButton) rowView.findViewById(R.id.tourItemDelete);
    final long tourId = (Long) data.get(position).get("key");
    imageButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mImageClicked.imgClicked(tourId);

        }
    });

    return rowView;
}

public interface OnImgClicked {
    public void imgClicked(long tourId);
}

public void setOnImgClickListener(OnImgClicked imgClicked) {
    this.mImageClicked = imgClicked;
}
}

在片段中实现接口

public class MyFragment extends ListFragment implements MyCustomAdapter.OnImgClicked {
    //...other methods like

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    [...]//init/create variables for MyCustomAdapter Creation
    MyCutsomAdapter adapter = new MyCustomAdapter(getActivity(),....);
    adapter.setOnImgClickListener(this);

    }

    @Override
    public void imgClicked(long tourId) {
        //do something with tourId
    }

}

答案 1 :(得分:0)

中的

imgBtn.setFocusable(false);
        imgBtn.setClickable(false);  

应修复点击问题