问题
I have the following layout . What I'm trying to achieve is 7 horizontal scrollablelayouts with an "ImageView" and a gridview in each. When I click on each of the 7 gridviews I'm hoping to start another activity.
Problem is setting onClickListener on each of the 7 gridView is not working , the OnItemClickListener is never called. the click is not registering apparently. I'm not sure what is going wrong. I saw a few posts which said that click is going to some other child item-- ?? i'm not sure which child item. Worst case I might be able to register a click on the 7 imageViews and take an according action. At the moment I'm trying it out when the gridview is empty. even when its showing images i want a click to happen only on the gridView and not on any of its individual contents.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="@drawable/mon"
android:scaleType="centerInside"
android:background="@android:color/white"
android:cropToPadding="true"
/>
<GridView
android:id="@+id/gridview_mon"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
/>
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="@drawable/tue"
android:scaleType="centerInside"
android:background="@android:color/white"
android:cropToPadding="true"
/>
<GridView
android:id="@+id/gridview_tue"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
/>
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="@drawable/wed"
android:scaleType="centerInside"
android:background="@android:color/white"
android:cropToPadding="true"
/>
<GridView
android:id="@+id/gridview_wed"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
/>
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="@drawable/thu"
android:scaleType="centerInside"
android:background="@android:color/white"
android:cropToPadding="true"
/>
<GridView
android:id="@+id/gridview_thu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
/>
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="@drawable/fri"
android:scaleType="centerInside"
android:background="@android:color/white"
android:cropToPadding="true"
/>
<GridView
android:id="@+id/gridview_fri"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
/>
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="@drawable/sat"
android:scaleType="centerInside"
android:background="@android:color/white"
android:cropToPadding="true"
/>
<GridView
android:id="@+id/gridview_sat"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
/>
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="@drawable/sun"
android:scaleType="centerInside"
android:background="@android:color/white"
android:cropToPadding="true"
/>
<GridView
android:id="@+id/gridview_sun"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
One of the click listener setting given below
mMonView = (GridView) v.findViewById(R.id.gridview_mon);
try {
setupAdapter(mMonView, mWeekPlanner.getDayList(WeekPlanner.MON));
} catch (InvalidClothesWeekPlannerException e) {
setupAdapter(mMonView, null);
}
mMonView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> gridView, View view,
int pos, long id) {
// Clothes item
// =mWeekPlanner.getDayList(WeekPlanner.MON).get(pos);
Toast.makeText(getActivity(), "YAY gridview clicked!", Toast.LENGTH_SHORT);
Intent intent =new Intent(getActivity(),WeekPagerActivity.class);
intent.putExtra(WeekPlannerFragment.EXTRA_DAY, WeekPlanner.MON);
startActivity(intent);
}
});
来源:https://stackoverflow.com/questions/21581414/gridview-click-not-working