GridView上的OnItemClickListener

时间:2017-11-13 21:51:32

标签: android gridview fragment onitemclicklistener

我找到了一个link,其中我使用了GridView的图标样式。其他人评论说他的OnItemClickListener代码不起作用。我已经尝试将其作为ListView,但我认为它不会那样工作。我也尝试过将其改为;

import android.widget.AdapterView.OnItemClickListner;

但它只是变灰了。

同样在我的项目中,这个java扩展到了Fragment。所以我想要发生的是当我点击这个GridView上的一个项目时,它将移动到另一个片段。

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.AdapterView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class R_MainCat extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_gridview, container, false);
    GridView gridview = (GridView) rootView.findViewById(R.id.gridview);

    List<GridItemObject> allItems = getAllItemObject();

    CustomAdapter customAdapter = new CustomAdapter(getActivity(), allItems);
    gridview.setAdapter(customAdapter);
    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
            Toast.makeText(getActivity(), "Main Category: " + i, Toast.LENGTH_SHORT).show();
            switch(i)
            {
                case 0:
                    R_Bakery f2 = new R_Bakery();
                    FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
                    fragTransaction.replace(R.id.container_view,f2 );
                    fragTransaction.addToBackStack(null);
                    fragTransaction.commit();
                    break;
                /*case 1:
                *
                * */
            }
        }
    });

    return rootView;
}
private List<GridItemObject> getAllItemObject(){
    List<GridItemObject> items = new ArrayList<>();
    items.add(new GridItemObject(R.drawable.cat_dairy,"Dairy Products"));
    items.add(new GridItemObject(R.drawable.cat_bread,"Bakery"));
    items.add(new GridItemObject(R.drawable.cat_grocery,"Grocery"));
    items.add(new GridItemObject(R.drawable.cat_vegies,"Fruits and Vegetables"));
    items.add(new GridItemObject(R.drawable.cat_cleaning,"Cleaning & Washing"));
    return items;
}}

第二个JAVA是CustomAdapter:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;

public class CustomAdapter extends BaseAdapter {

    private List<GridItemObject> listStorage;

    private LayoutInflater layoutinflater;
    private Context context;

    public CustomAdapter(Context context, List<GridItemObject> 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.gridview_icon, parent, false);
            listViewHolder.screenShot = (ImageView)convertView.findViewById(R.id.screen_shot);
            listViewHolder.catName = (TextView)convertView.findViewById(R.id.cat_name);
            convertView.setTag(listViewHolder);
        }else{
            listViewHolder = (ViewHolder)convertView.getTag();
        }
        listViewHolder.screenShot.setImageResource(listStorage.get(position).getScreenShot());
        listViewHolder.catName.setText(listStorage.get(position).getCatName());

        return convertView;
    }

    static class ViewHolder{
        ImageView screenShot;
        TextView catName;
    }}

问题:当我点击该项目时,它不会执行任何操作并且Toast未显示。

0 个答案:

没有答案