2013-10-30 31 views
0

這是我的GridView ArrayAdapter:的Android GridView控件onItemClickListener不火

<?xml version="1.0" encoding="utf-8"?> 
<!-- gridview adapter layout --> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res/com.coapps.pico" 
    android:layout_width="150dp" 
    android:layout_height="150dp" 
    android:background="@drawable/gridview_background_selector" > 

    <!-- profile picture layout --> 

    <RelativeLayout 
     android:id="@+id/fragment_events_adapter_relativelayout_profile_picture" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="5dp" > 

     <!-- profile picture image view --> 

     <ImageView 
      android:id="@+id/fragment_events_adapter_imageview_profile_picture" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:contentDescription="@string/app_name" 
      android:scaleType="fitXY" 
      android:src="@drawable/facebook_blank_profile" /> 

     <HorizontalScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:background="@color/background_black_transparent" > 

      <!-- user name text view --> 

      <com.coapps.pico.NillanTextView 
       android:id="@+id/fragment_events_adapter_textview_username" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:src="@drawable/facebook_blank_profile" 
       android:textColor="@android:color/white" 
       app:isBold="true" /> 
     </HorizontalScrollView> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/fragment_events_adapter_relativelayout_profile_picture" 
     android:layout_marginBottom="5dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_marginTop="3dp" 
     android:background="@color/orange" > 

     <HorizontalScrollView 
      android:id="@+id/fragment_events_adapter_horizontalscrollview_event_name" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" > 

      <!-- event name --> 

      <com.coapps.pico.NillanTextView 
       android:id="@+id/fragment_events_adapter_textview_event_name" 
       style="@style/text_shadow_black" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:src="@drawable/facebook_blank_profile" 
       android:textAppearance="@android:style/TextAppearance.Small" 
       android:textColor="@android:color/white" 
       app:isBold="true" /> 
     </HorizontalScrollView> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/fragment_events_adapter_horizontalscrollview_event_name" 
      android:layout_centerHorizontal="true" 
      android:gravity="center" 
      android:orientation="horizontal" > 

      <!-- event icon --> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="15dp" 
       android:layout_gravity="right" 
       android:layout_margin="2dp" 
       android:contentDescription="@string/app_name" 
       android:src="@drawable/ic_action_event" /> 

      <!-- event start time --> 

      <com.coapps.pico.NillanTextView 
       android:id="@+id/fragment_events_adapter_textview_start_time" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:src="@drawable/facebook_blank_profile" 
       android:textColor="@android:color/black" 
       android:textSize="12sp" 
       app:isBold="true" /> 
     </LinearLayout> 
    </RelativeLayout> 

    <!-- attending event --> 

    <ImageView 
     android:id="@+id/fragment_events_adapter_imageview_attending_mask" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_centerInParent="true" 
     android:background="@color/background_black_transparent" 
     android:contentDescription="@string/app_name" 
     android:padding="30dp" 
     android:src="@drawable/v" 
     android:visibility="gone" /> 

</RelativeLayout> 

和我設置的onItemClickedListener:

private void setGridView() 
{ 
    //create a new event list 
    picoEventsList = new ArrayList<PicoEvent>(); 
    //create new gridview arrayadapter 
    arrayAdapter = new EventDetailsArrayAdapter(getActivity(), picoEventsList); 
    //set grid's view empty view 
    View emptyView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_events_empty_view, null);  
    gridView.setEmptyView(emptyView); 
    gridView.setOnItemClickListener(this); 
    gridView.setAdapter(arrayAdapter); 

} 

@Override 
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {  
    //get clicked event 
    PicoEvent event = arrayAdapter.getItem(arg2); 
    //create new event details fragment 
    EventDetailsFragment eventDetailsFragment = new EventDetailsFragment(); 
    //add the event to fragment's arguments 
    eventDetailsFragment.setArguments(PicoEventCreator.createBundle(event)); 
    //show the event details fragment 
    getMainActivity().showFragment(null, eventDetailsFragment, true, true); 

} 

但onItemClick()方法不被解僱。 。

我已經檢查和我在我的佈局的觀點不具有可點擊=「真正的」設置好的...

有什麼想法?

+0

你可以把你的佈局XML起來,你在哪裏初始化GridView? –

回答

0

好吧,我發現了什麼問題是

顯然GridView和HorzonitalScrollView沒有這麼好的朋友......

可能是因爲HorzonitalScrollView具有的onClick的funcionality ..

我找到了解決辦法是把:

android:descendantFocusability="blocksDescendants" 

在我的ArrayAdapter根視圖。

1

嘿guyz終於得到了解決......

我們在做什麼,直接訪問GridView控件內部的佈局,所以onItemClickListener發現它混淆訪問項目。

因此,解決辦法是應用onClickListener適配器(即通常ArrayAdapter)

所以什麼IM想說的是裏面:

public View getView(int position, View convertView, ViewGroup parent) { 

      //Here row is a view and we can set OnClickListener on this 
    final View row; 
    ViewHolder holder = null; 

    if (convertView == null) { 
     LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
     //Here we inflate the layout to view (linear in my case) 
     row = inflater.inflate(layoutResourceId, parent, false); 
     holder = new ViewHolder(); 
     holder.imageTitle = (TextView) row.findViewById(R.id.text); 
     holder.image = (ImageView) row.findViewById(R.id.image); 
     row.setTag(holder); 
    } else { 
     row = convertView; 
     holder = (ViewHolder) row.getTag(); 
    } 

    ImageItem item = data.get(position); 
    holder.imageTitle.setText(item.getTitle()); 
    holder.image.setImageBitmap(item.getImage()); 

    //Now get the id or whatever needed 
    row.setId(position); 
    // Now set the onClickListener 
    row.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Toast.makeText(context, "Clicked" + row.getId() + "!!", 
        Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    return row; 
} 
相關問題