2014-02-27 77 views
0

如果有人已經實現onitemclicklistener與交錯gridview在片段中請幫助我,我堅持從最後一天多一天。我已經完成了這項活動,但當我在fragment中使用它時,我不知道什麼是問題。如果有任何人有任何劈開,請告訴我我的代碼是什麼。交錯gridview onitemclicklistener不工作在片段

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細胞的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> 
+0

你的gridview有wha項目。你可以發佈的XML。如果你有ImageVIew或ImageButton,它將獲得焦點。 – Raghunandan

+0

好吧會更新我的問題 –

+0

嘗試添加這個'android:descendantFocusability =「blocksDescendants」'到佈局 – Raghunandan

回答

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

}