vikas singh vikas singh - 1 year ago 212
Android Question

how to handle Gridview Click in ViewPager?

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

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(),
Bitmap userIcon = BitmapFactory.decodeResource(this.getResources(),

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(;
customGridAdapter = new CustomGridViewAdapter(getActivity(),
R.layout.row_grid, gridArray);
gridView.setOnItemClickListener(new OnItemClickListener() {

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

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

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




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; = data;

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(;
holder.imageItem = (ImageView) row.findViewById(;
} else {
holder = (RecordHolder) row.getTag();

Item item = data.get(position);
return row;


static class RecordHolder {
TextView txtTitle;
ImageView imageItem;


Answer Source

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.add(new Item(homeIcon, "Home"));
gridArray.add(new Item(userIcon, "User"));
//you code.....
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download