I have 3 fragments with gridview but onclikitem event does not work.
What might be the problem?
I did my best and I tried all of them but not true. I think the problem is the use of fragments because with use activity works fine.
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.free_video_fragment, container, false);
final GridView gridview = (GridView) view.findViewById(R.id.freevideogridview);
List<ItemObject> allItems = getAllItemObject();
CustomAdapter customAdapter = new CustomAdapter(getActivity(), allItems);
gridview.setAdapter(customAdapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {
Toast.makeText(getActivity(),"Position" , Toast.LENGTH_LONG).show();
}
});
return view;
Layout:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/freevideogridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:horizontalSpacing="4dp"
android:numColumns="3"
android:padding="4dp"
android:scrollbars="none"
android:stretchMode="columnWidth"
android:verticalSpacing="4dp"
/>
Adapter:
public class CustomAdapter extends BaseAdapter {
private LayoutInflater layoutinflater;
private List<ItemObject> listStorage;
private Context context;
public CustomAdapter(Context context, List<ItemObject> customizedListView) {
this.context = context;
layoutinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listStorage = customizedListView;
}
@Override
public int getCount() {
return listStorage.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder listViewHolder;
if(convertView == null){
listViewHolder = new ViewHolder();
convertView = layoutinflater.inflate(R.layout.pop_music_list, parent, false);
listViewHolder.screenShot = (ImageView)convertView.findViewById(R.id.screen_shot);
listViewHolder.musicName = (TextView)convertView.findViewById(R.id.music_name);
listViewHolder.musicAuthor = (TextView)convertView.findViewById(R.id.music_author);
convertView.setTag(listViewHolder);
}else{
listViewHolder = (ViewHolder)convertView.getTag();
}
listViewHolder.screenShot.setImageResource(listStorage.get(position).getScreenShot());
listViewHolder.musicName.setText(listStorage.get(position).getMusicName());
listViewHolder.musicAuthor.setText(listStorage.get(position).getMusicAuthor());
return convertView;
}
static class ViewHolder{
ImageView screenShot;
TextView musicName;
TextView musicAuthor;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…