Fragment 下的 OnItemClick 不起作用

OnItemClick under Fragment is not working

我不知道为什么,但我的 onItemClick 方法甚至没有调用。嗯,在每行添加按钮(添加按钮见适配器)之前它工作正常。

请帮我找出错误,谢谢

@Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    Init();

    //People list click
    chatPeoples.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Log.i("click", "Clicking ");
            PeopleObject p = peopleObjList.get(position);
            String chattingToName = p.getPersonName();
            String chattingToDeviceID = p.getRegId();

            Intent intent = new Intent(getActivity(), ChatActivityNew.class);
            intent.putExtra("chattingFrom",MyUsername);
            intent.putExtra("chattingToName", chattingToName);
            intent.putExtra("chattingToDeviceID", chattingToDeviceID);
            startActivity(intent);

        }
    });


    searchIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (eSearch.getVisibility() == View.INVISIBLE) {
                eSearch.setVisibility(View.VISIBLE);
            }
        }
    });


   // setMyUsername();
    //getPeopleList();
}

适配器Class

public class ChatAdapter extends BaseAdapter {

private Activity context;
private ArrayList<PeopleObject> chatPeoplesUsername;

public ChatAdapter(Activity cont, ArrayList<PeopleObject> cUsername ){
    this.context = cont;
    this.chatPeoplesUsername = cUsername;

}

static class ViewHolder {
    protected TextView txUsername;
    protected Button addFriend;
}

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

@Override
public Object getItem(int position) {
    return null;
}

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

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

    ViewHolder viewHolder = null;
    if (convertView == null) {

        LayoutInflater inflator = context.getLayoutInflater();
        convertView = inflator.inflate(R.layout.chat_people_row, null);
        viewHolder = new ViewHolder();

        viewHolder.txUsername = (TextView) convertView
                .findViewById(R.id.person_username);
        viewHolder.addFriend = (Button)convertView.findViewById(R.id.btn_add_friend);


        viewHolder.addFriend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "hm ..", Toast.LENGTH_SHORT).show();
            }
        });


        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    if (chatPeoplesUsername != null) {
        PeopleObject h = chatPeoplesUsername.get(position);
        viewHolder.txUsername.setText(h.getPersonName());

    }

    return convertView;
}
}

将您的点击侦听器移至 getView() 方法

  convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("click", "Clicking ");
            //remaining code            
        }
    });

你已经在每个 listItem 上添加了按钮,对吗?

然后只需将此行放入按钮的 xml 文件中,一切都会正常工作

android:focusable="false"

所以按钮的xml应该是这样的

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false" />

这背后的原因是当您单击 listItem 时按钮将获得所有内容的焦点,因此将调用该方法而不是列表的单击。

您不能使用可点击的组件,例如按钮和 OnClickListener together.in 在这种情况下,您的 OnClickListener 将不会被调用。