Onclick Listener Gridview在Fragment中不起作用

时间:2016-12-02 09:58:47

标签: android gridview onclicklistener

我有3个使用gridview的片段,但是onclikitem事件不起作用。

可能是什么问题?

我尽我所能,我尝试了所有这些但不是真的。我认为问题是片段的使用,因为使用活动工作正常。

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;

布局:

<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"

   />

适配器:

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;
    }

}

1 个答案:

答案 0 :(得分:0)

我认为您必须更改适配器,如下所示。

@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;
}

null放在false处,以扩大您的观点。