【问题标题】:Can not get gridView on item click listener无法在项目单击侦听器上获取 gridView
【发布时间】:2012-04-12 09:23:26
【问题描述】:

我无法在 Fragment 中获取 gridView 的 setOnItemClickListener。可能是什么问题?

这是我的代码::

 public class MainMenuFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment

        View view = inflater.inflate(R.layout.main_menu_fragment, container, false);

        itemsGridViewObj = (GridView) view.findViewById(R.id.itemsGridView);



        itemsGridViewObj.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {

                Log.d(TAG, "--> onItemClick listener...");      // Can not getting this method.
                /*if(position == 1) {
                    FruitMenuFragment fruitMenuFragment = new FruitMenuFragment();
                    fragmentTransaction.replace(android.R.id.content, fruitMenuFragment);
                    fragmentTransaction.commit();
                }*/
            }
        });

        return view;
    }
}`

【问题讨论】:

  • 其实是自定义的GridView,只包含ButtonView。我认为这会导致获取 onItemClickListener 出现问题!
  • 你的意思是GridView的每一项里面都有一个按钮吗? IE。在 LisAdapter 使用的布局中。
  • 是的。在这种情况下,我没有得到那个 onItemClickListener!
  • 我相信按钮会覆盖 OnItemClickListener。我在列表视图中的复选框也有类似的问题。在适配器中,将一个新的 OnClickListener 添加到按钮内联,看看 OnItemClickListener 是否生效。
  • 我的线性布局中有一个图像视图和一个复选框,用于填充网格视图。我使图像视图和复选框不可点击且不可聚焦,而线性布局可点击且可聚焦,但我仍然没有收到点击事件。

标签: android gridview fragment onitemclicklistener


【解决方案1】:

您可能需要在 ButtonView 中设置以下内容。 安卓:可聚焦=“假” android:focusableInTouchMode="false"

adding CheckBox to list row loses my onItemClick events?

【讨论】:

  • 也解决了我的问题。我没有选中 Clickable,但不是 Focusable。
【解决方案2】:

当使用 Fragments 时,视图的初始化发生在两个阶段。

视图仅在 onCreateView 方法之后膨胀(因此可访问)。此方法仅用于膨胀视图并将其返回到 Fragment。

因此,与查找视图和设置 onClickListeners 相关的任何逻辑都应在 onActivityCreated() 函数中完成,因为这是您可以访问膨胀视图的第一个点。

http://developer.android.com/reference/android/app/Fragment.html#Lifecycle查看 Google 文档

以下是您调整后的代码以符合我上面描述的内容:

public class MainMenuFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment

    return inflater.inflate(R.layout.main_menu_fragment, container, false);     
}

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

    GridView itemsGridViewObj = (GridView) findViewById(R.id.itemsGridView);

    itemsGridViewObj.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long arg3) {

        Log.d(TAG, "--> onItemClick listener..."); // You should see this now
        /*if(position == 1) {
            FruitMenuFragment fruitMenuFragment = new FruitMenuFragment();
            fragmentTransaction.replace(android.R.id.content, fruitMenuFragment);
            fragmentTransaction.commit();
        }*/
    }});
}
}

【讨论】:

  • 对我没有用。首先在Fragment 中没有方法findViewById 并且做getView().findViewById 也不起作用。我不知道这个答案是如何获得 3 票的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-21
  • 1970-01-01
  • 1970-01-01
  • 2011-03-24
  • 2011-07-07
相关资源
最近更新 更多