GridView的OnItemClickListener没有调用onItemClick方法

4

我正在尝试为我的Gridview中的项目设置一个OnItemClickListener。但是由于某些原因,监听器内的onItemClick方法从未被调用。

设置监听器和适配器:

UsersAdapter usersAdapter = new UsersAdapter(venueUsers);
gridView.setAdapter(usersAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        Intent intent = new Intent(Users.this, com.roqbot.client.login.Profile.class);
        intent.putExtra("idUser", id);
        startActivity(intent);
    }
});

我的适配器:

private class UsersAdapter extends BaseAdapter implements ListAdapter {
    private JSONArray users;

    private UsersAdapter(JSONArray users) {
        this.users = users;
    }

    public int getCount() {
        return users.length();
    }

    public JSONObject getItem(int position) {
        return users.optJSONObject(position);
    }

    public long getItemId(int position) {
        return users.optJSONObject(position).optInt("idUser");
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = Users.this.getLayoutInflater().inflate(R.layout.user_icon, null);

        JSONObject user = getItem(position);

        TextView username = (TextView) convertView.findViewById(R.id.username);
        username.setText(user.optString("sName"));

        TextView userScore = (TextView) convertView.findViewById(R.id.userScore);
        int iDJScore = user.optInt("iDJScore");
        if (iDJScore > 0) {
            userScore.setText(Integer.toString(iDJScore));
        }
        else {
            userScore.setVisibility(TextView.INVISIBLE);
            ((ImageView) convertView.findViewById(R.id.userScoreBg)).setVisibility(View.INVISIBLE);
        }

        TextView userLevel = (TextView) convertView.findViewById(R.id.userLevel);
        userLevel.setText(user.optString("iDJLevel"));

        TextView userMatch = (TextView) convertView.findViewById(R.id.userMatch);
        ImageView matchIcon = (ImageView) convertView.findViewById(R.id.matchIcon);
        int iCompatibility = user.optInt("iCompatibility");
        if (iCompatibility != 0) {
            userMatch.setText( iCompatibility + "%");
        }
        else {
            userMatch.setVisibility(TextView.INVISIBLE);
            matchIcon.setVisibility(ImageView.INVISIBLE);
        }

        ImageView userIcon = (ImageView) convertView.findViewById(R.id.userIcon);
        String sUserIcon = user.optString("sImageUrl-thumb");
        imageLoader.DisplayImage(sUserIcon, Users.this, userIcon);

        return convertView;
    }
}

我对为什么点击监听器不起作用感到困惑。这段代码在其他很多地方都能正常工作。


1
我认为你在gridView的onClickListener中漏掉了一个闭合括号。 - user370305
你的导入是否正确? - Pratik Bhat
如果括号或圆括号有误,它就无法正确编译,对吧?我刚刚检查了导入的内容,一切看起来都是正确的。 - dbaugh
1
我实际上找到了一个解决方法。我只是在适配器的convertView方法中为ImageView设置了一个点击监听器。这样做可以起作用,但似乎有些粗糙。 - dbaugh
2个回答

6

我遇到了同样的问题。

在我的情况下,“android:focusable=true” 阻止了项目的点击事件。

在我的 GridList 项布局中。

<TextView android:id="@+id/auction_item_title"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:singleLine="true"
    ***android:focusable="true"***
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
/>

我移除了 [ android:focusable="true" ],问题得到解决。


为了解决这个问题,您应该添加 android:focusable="false"android:focusableInTouchMode="false" - Houcine

3

实际上,您只需要确保网格中的项不可点击。GridView 无法处理可点击的项。例如按钮将无效。如果您使用 LinearLayout 或任何其他不可点击的项创建了一个项,则必须确保您没有设置 clickable="true" 属性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接