how to handle Gridview Click in ViewPager?

142 views Asked by At

I'm create gridview in my FragmentA class and when I click gridview position say 0 then its navigate to FragmentB class and again when I clicked position 1 then its navigate to FragmentC class.

But my problem is that when we come back in my FragmentA class then my Gridview menu is automatically double, why?

Here below is my full code:

FragmentA class

public class FragmentA extends Fragment {

    GridView gridView;
    ArrayList<Item> gridArray = new ArrayList<Item>();
    CustomGridViewAdapter customGridAdapter;

    public FragmentA() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_a, container, false);

        Bitmap homeIcon = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.home);
        Bitmap userIcon = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.personal);

        gridArray.add(new Item(homeIcon, "Home"));
        gridArray.add(new Item(userIcon, "User"));
        gridArray.add(new Item(homeIcon, "House"));
        gridArray.add(new Item(userIcon, "Friend"));
        gridArray.add(new Item(homeIcon, "Home"));
        gridArray.add(new Item(userIcon, "Personal"));
        gridArray.add(new Item(homeIcon, "Home"));
        gridArray.add(new Item(userIcon, "User"));
        gridArray.add(new Item(homeIcon, "Building"));

        gridView = (GridView) view.findViewById(R.id.gridView1);
        customGridAdapter = new CustomGridViewAdapter(getActivity(),
                R.layout.row_grid, gridArray);
        gridView.setAdapter(customGridAdapter);
        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                switch (position) {
                case 0:


                    ((MainActivity) getActivity()).switchToFragementB(2);
                    break;

                case 1:
                    ((MainActivity) getActivity()).switchToFragmentC(3);


                    break;

                }
            }

        });

        return view;
    }

}

Here is CustomGridViewAdapter adapter class

public class CustomGridViewAdapter extends ArrayAdapter<Item> {
    Context context;
    int layoutResourceId;
    ArrayList<Item> data = new ArrayList<Item>();

    public CustomGridViewAdapter(Context context, int layoutResourceId,
            ArrayList<Item> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        RecordHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new RecordHolder();
            holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
            holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
            row.setTag(holder);
        } else {
            holder = (RecordHolder) row.getTag();
        }

        Item item = data.get(position);
        holder.txtTitle.setText(item.getTitle());
        holder.imageItem.setImageBitmap(item.getImage());
        return row;

    }

    static class RecordHolder {
        TextView txtTitle;
        ImageView imageItem;

    }
}
2

There are 2 answers

1
Kartheek On BEST ANSWER

my Gridview menu is automatically double why?

This is because you are adding the items in the onCreateView() of your fragment and probably this onCreateView() called again when you come back to FragmentA. so before inserting the items into the gridArray clear the elements in the array.

gridArray.clear();
gridArray.add(new Item(homeIcon, "Home"));
gridArray.add(new Item(userIcon, "User"));
//you code.....
1
Olivia Pearls On

holder.txtTitle.setText(item.getTitle()); holder.imageItem.setImageBitmap(item.getImage());

are showing errors in getTitle() and getImage().