Android 如何处理按钮单击列表片段

Android 如何处理按钮单击列表片段,android,android-fragments,android-ui,Android,Android Fragments,Android Ui,我很好奇如何最好地处理带有自定义适配器的ListFragment中的按钮点击 */ @Override public View getView(int position, View convertView, ViewGroup parent) { View view; if (convertView == null) { view = _inflater.inflate(R.layout.test_single_item,

我很好奇如何最好地处理带有自定义适配器的
ListFragment
中的按钮点击

*/
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;

        if (convertView == null) {
            view = _inflater.inflate(R.layout.test_single_item, parent, false);
        } else {
            view = convertView;
        }

        TestItemModel item = getItem(position);
        ((TextView) view.findViewById(R.id.item_label)).setText(item.getName());
        ((TextView) view.findViewById(R.id.item_id)).setText(item.getId());
        ImageView image = (ImageView) view.findViewById(R.id.image_id);
        Resources resources = this.getContext().getResources();
        image.setImageDrawable(resources.getDrawable(R.drawable.ic_launcher));
        Button btn = (Button) view.findViewById(R.id.button_id);
        Button btn2 = (Button) view.findViewById(R.id.button_id_2);
        Button btn3 = (Button) view.findViewById(R.id.button_id_3);
        ol = new OnItemClickListener(position);
        btn.setOnClickListener(ol);
        btn.setTag(1);
        btn2.setOnClickListener(ol);
        btn2.setTag(2);
        btn3.setOnClickListener(ol);
        btn3.setTag(3);

        return view;
    }
我有一个按钮的
onClickListener
设置,但是我需要能够获取单击它的项目,因为它在一个项目中,这里是自定义适配器中的
getView

*/
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;

        if (convertView == null) {
            view = _inflater.inflate(R.layout.test_single_item, parent, false);
        } else {
            view = convertView;
        }

        TestItemModel item = getItem(position);
        ((TextView) view.findViewById(R.id.item_label)).setText(item.getName());
        ((TextView) view.findViewById(R.id.item_id)).setText(item.getId());
        ImageView image = (ImageView) view.findViewById(R.id.image_id);
        Resources resources = this.getContext().getResources();
        image.setImageDrawable(resources.getDrawable(R.drawable.ic_launcher));
        Button btn = (Button) view.findViewById(R.id.button_id);
        Button btn2 = (Button) view.findViewById(R.id.button_id_2);
        Button btn3 = (Button) view.findViewById(R.id.button_id_3);
        ol = new OnItemClickListener(position);
        btn.setOnClickListener(ol);
        btn.setTag(1);
        btn2.setOnClickListener(ol);
        btn2.setTag(2);
        btn3.setOnClickListener(ol);
        btn3.setTag(3);

        return view;
    }
正如您所看到的,我使用标记来确定单击了哪个按钮,
OnItemClickListener
知道调用位置的位置在哪里


我不确定最好的方法以及如何正确地执行此操作。

使用开关盒,如下所示

private OnClickListener mClickListener = new OnClickListener() {

            public void onClick(View v) {
                switch(v.getId())
                {
                case R.id.button_id :
                    // btn clicked
                            Toast.makeText(context," Button1 clicked at positon"+v.getTag(), Toast.LENGTH_SHORT).show();
                    break;
                case R.id.button_id2 :
                    // btn2 clicked   
                           Toast.makeText(context," Button2 clicked at positon"+v.getTag(), Toast.LENGTH_SHORT).show();
                    break;  
                case R.id.button_id3 :
                            Toast.makeText(context," Button3 clicked at positon"+v.getTag(), Toast.LENGTH_SHORT).show();  
                    // btn 3 clciked
                    break; 
                }

            }
        };
使用

使用两个按钮捕捉示例

在第一行位置0处单击的按钮1的捕捉

在位置1(即第二行)处单击按钮2的捕捉


为什么不在
onClick
中使用开关盒?无法获取单击的项目。而且还需要点击的位置来知道点击了哪个项目。你可以我看不出它有什么问题吗?提供一个点击的例子来提供这一点?除了提供具体是什么项目,最终有效外,我还解决了我的问题。我把这个放进去是为了解决我的问题。ol=新的监听器(位置、项目);在我使用的侦听器中添加了一项,我的问题是,这是否会阻止良好的模型视图分离?这里是否有影响,因为最终数据库事务将在这些按钮点击上执行,沿着line@kev2316试一下上面的方法,它会起作用的,你在问题中没有提到的问题是什么最初我的问题是如何将与按钮相关的项目传递给听者,以便对其执行操作,我已经解决了,因为你做了我正在做的事对不起,但现在我的问题是,这是否有效,是否遵循适当的模型-视图分离。@kev2316我认为is方法没有问题