0

这是我的 GridView ArrayAdapter:

<?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() 方法没有被解雇..

我已经检查过,并且我的布局中的视图都没有设置 clickable="true" ...

有任何想法吗 ?

4

2 回答 2

1

嘿家伙终于有一个解决方案......

我们所做的是直接访问 GridView 内的 Layout,因此 onItemClickListener 发现访问项目很混乱。

所以解决方案是在Adapter内部应用onClickListener(即通常是ArrayAdapter)

所以我想说的是:

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;
}
于 2014-01-22T11:33:44.670 回答
0

好的,我发现了问题所在

显然 GridView 和 HorzonitalScrollView 不是那么好的朋友......

可能是因为 HorzonitalScrollView 具有 onClick funcionality..

我找到的解决方案是:

android:descendantFocusability="blocksDescendants"

在我的 ArrayAdapter 根视图中。

于 2013-10-30T20:29:11.837 回答