交错的gridview onitemclicklistener不在片段中工作

时间:2014-02-27 05:42:20

标签: android gridview android-fragments

如果有人在片段中实现了交错网格视图的onitemclicklistener,请帮助我,我在最后一天以上就坚持了下来。我已经用活动完成了这个,但是当我在fragment中使用它时我不知道是什么问题。如果有任何人有任何劈开,请告诉我我的代码。

 mAdapter = new STGVAdapter(getActivity(),getActivity().getApplication(),Details.getarray());
        stgv.setAdapter(mAdapter);
          stgv.setOnItemClickListener(new StaggeredGridView.OnItemClickListener() {

                @Override
                public void onItemClick(StaggeredGridView parent, View view, int position,
                        long id) {
                    // TODO Auto-generated method stub
              Toast.makeText(getActivity(), "clicked", 0).show();

                }
            });
        mAdapter.notifyDataSetChanged();   

这是我的Gridview Cell Xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/white" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2.5dip"
        android:layout_marginRight="2.5dip"
        android:layout_marginTop="5dip"
        android:background="#D1D2D6"
        android:orientation="vertical"
        android:padding="1dip" >

        <info.androidhive.slidingmenu.ScaleImageView
            android:id="@+id/img_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:focusable="false"
            android:scaleType="centerCrop" />

        <TextView
            android:id="@+id/tv_info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/img_content"
            android:background="#E7E7EF"
            android:clickable="false"
            android:focusable="false"
            android:padding="8dp"
            android:text="news" />
    </LinearLayout>

</LinearLayout>

这是一个gridView Xml。

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:staggered="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.bulletnoid.android.widget.StaggeredGridView.StaggeredGridView
        android:id="@+id/stgv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="false"
        android:focusable="false"
        staggered:drawSelectorOnTop="true"
        staggered:numColumns="2" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

请覆盖片段内的onActivityCreated()方法,然后调用其中的click事件。

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    stgv.setOnItemClickListener(new StaggeredGridView.OnItemClickListener() {

        @Override
        public void onItemClick(StaggeredGridView parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub
            Toast.makeText(getActivity(), "Item Clicked: " + position, Toast.LENGTH_SHORT).show();

        }
    });
    super.onActivityCreated(savedInstanceState);

}