片段下的OnItemClick不起作用

时间:2015-07-04 05:05:01

标签: android android-listview fragment android-adapter

我不知道为什么,但我的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();
}

适配器类

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;
}
}

3 个答案:

答案 0 :(得分:2)

你在每个listItem上都添加了按钮吗?

然后将这一行放在你按钮的xml文件中,一切正常

android:focusable="false"

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

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

这背后的原因是当你点击listItem时按钮将成为所有事情的焦点,所以这个方法将被调用而不是list的点击。

答案 1 :(得分:1)

将您的点击列表器移至getView()方法

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

答案 2 :(得分:0)

你不能一起使用像按钮和OnClickListener这样的可点击组件。在这种情况下你的OnClickListener不会被调用。