Stepan Sanda
Stepan Sanda

Reputation: 2340

Android ViewPager and GridView selection

I'm using ViewPager in my app and inside this pager is GridView with data. User is able to select one item from the grid view and show detail information under ViewPager. When user clicked to the item in the GridView, the item is marked by color as selected.

The problem is, that only one item at the time can be selected in all pages of the pager. So when user click on the item on page 1 its marked and than when he selected another one on the page 2 its also marked and the first one on the previous page is still marked as selected. And that is the problem. I need to have selected only one item at time so I need to deselect previous selection after new selection. If it's only on the same page, it works correctly. After next selection, previous item is deselected.

MainFragment -> PagerAdapter -> GridFragment -> GridViewAdapter

In GridFragment I handle OnItemClick and notify GridAdapter on which position was item selected and notifyDataSetChanged().

For each page I'm using his own GridView

PagerAdapter:

 @Override 
 public Fragment getItem(int position) {
        return GridFragment.getInstance(position-(Integer.MAX_VALUE / 2));
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

GridFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_calednar, container, false);

    mAdapter = new CalendarGridViewAdapter(getActivity(), getDateTime(position), MONDAY);

    mGridView = (GridView) rootView.findViewById(R.id.gridView);
    mGridView.setAdapter(mAdapter);

    // TODO handle on item click listener
    mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            mAdapter.setSelectedItem(i);
            mAdapter.notifyDataSetChanged();
        }
    });

    return rootView;
}

GridViewAdapter:

@Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
       // if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.grid_item_day, null);
        //}

        TextView txt = (TextView) convertView.findViewById(R.id.dayText);
        txt.setText(""+datetimeList.get(i).getDay());
        View actSelection = convertView.findViewById(R.id.actSelection);
        actSelection.setVisibility(View.INVISIBLE);
        if(selectedPosition == i){
            actSelection.setVisibility(View.VISIBLE);
        }

        ....

        return  convertView;
    }

    public void setSelectedItem(int position){
        selectedPosition = position;
    }

Upvotes: 0

Views: 2640

Answers (1)

vipul mittal
vipul mittal

Reputation: 17401

You need to keep the selected item index inside pager adapter instead. And on click of any item inside a fragment change that value.

interface ItemSelectionInterface{
 void onItemSelectionChanged(int fragmentPosition,int itemIndex);
 int getSelectedItemOnFragment(int fragmentPosition); 
}

implement above interface in PagerAdapter:

class YourPagerAdapter.......  implements ItemSelectionInterface{

    int selectedFragment,selectedItem;
@Override 
 public Fragment getItem(int position) {
        return GridFragment.getInstance(position-(Integer.MAX_VALUE / 2),this);
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

     void onItemSelectionChanged(int fragmentPosition,int itemIndex){
          selectedFragment=fragmentPosition;selectedItem=itemIndex;
    }
     int getSelectedItemOnFragment(int fragmentPosition){
      if(fragmentPosition!=selectedFragment) return -1;
      return selectedItem;

    }
}

Change your GridFragment to:

class GridFragment ....{
ItemSelectionInterface selectionInterface;


    @Override
public void setMenuVisibility(final boolean visible) {
    super.setMenuVisibility(visible);
    if (visible) {
        mAdapter.notifyDataSetChanged();
    }
}
public static GridFragment  getInstance(int position, ItemSelectionInterface selectionInterface){
  ...........
  ...........
  this.selectionInterface=selectionInterface;
  }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_calednar, container, false);

    mAdapter = new CalendarGridViewAdapter(getActivity(), getDateTime(position), MONDAY);
mAdapter.setSelectionInterface(selectionInterface);
    mGridView = (GridView) rootView.findViewById(R.id.gridView);
    mGridView.setAdapter(mAdapter);

    // TODO handle on item click listener
    mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            selectionInterface.onItemSelectionChanged(position,i);
            mAdapter.notifyDataSetChanged();
        }
    });
}
    return rootView;
}

And finally in adapter:

     ItemSelectionInterface selectionInterface; //Create a setter for
     int position;//create a setter
@Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
       // if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.grid_item_day, null);
        //}

        TextView txt = (TextView) convertView.findViewById(R.id.dayText);
        txt.setText(""+datetimeList.get(i).getDay());
        View actSelection = convertView.findViewById(R.id.actSelection);
        actSelection.setVisibility(View.INVISIBLE);
        if(selectionInterface.getSelectedItemOnFragment(position)== i){
            actSelection.setVisibility(View.VISIBLE);
        }

        ....

        return  convertView;
    }

Upvotes: 1

Related Questions