Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

android - Onclick Listener Gridview does not work in Fragment

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you have to change in your adapter like below.

@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, null);
        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;
}

put null in place of false at where you inflate your view.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...