我发现有些人也有这个问题,但现有的解决方案都没有对我有用。基本上我有一个片段(称为HeadlinesFragment),它将OnItemClickListener分配给包含图像的GridView。图像看起来很好,但触摸/点击事件永远不会被触发。我尝试了android的多种组合:clickable =“false”/ android:focusable =“false”等,但nothings对我有用
public static class HeadlinesFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.menufragment, container, false);
return rootView;
}
public class ImageAdapter extends BaseAdapter {
private Context context;
Integer[] imageIDs = {
R.drawable.pasta05,
R.drawable.pasta05,
R.drawable.pasta05,
R.drawable.pasta05,
R.drawable.pasta05,
R.drawable.pasta05,
R.drawable.pasta05
};
public ImageAdapter(Context c) {
context = c;
}
//---returns the number of images---
public int getCount() {
return imageIDs.length;
}
//---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(185, 185));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(imageIDs[position]);
return imageView;
}
}
GridView gridView;
@Override
public void onActivityCreated (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
gridView = (GridView) getView().findViewById(R.id.GridView);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity().getApplicationContext(), "SSSSSX", Toast.LENGTH_SHORT).show();
System.out.println("R=");
}
});
gridView.setAdapter(new ImageAdapter(getActivity()));
}
}
另一方面,这是包含GridView的片段的XML:
<?xml version="1.0" encoding="utf-8"?>
<GridView
android:id="@+id/GridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:gravity="center"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:numColumns="3"
android:descendantFocusability="blocksDescendants"
android:stretchMode="columnWidth" >
</GridView>
最后,这是包含托管片段的主要活动的XML。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:components="http://schemas.android.com/apk/res-auto"
android:background="#0099cc"
tools:context="com.purplemechanics.rest_a_urant.FullscreenActivity">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:descendantFocusability="blocksDescendants" android:id="@+id/richardo">
</LinearLayout>
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="112dp" />
</RelativeLayout>
<ListView
android:id="@+id/list_view"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#007095"
android:choiceMode="singleChoice"
android:divider="#005875"
android:dividerHeight="2dp"
android:textColor="#FFFFFF"
/>
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:0)
尝试使用new AdapterView.OnItemClickListener()
进行实例化,如下所示:
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity().getApplicationContext(), "SSSSSX", Toast.LENGTH_SHORT).show();
System.out.println("R=");
}
});