Android : Android : Change fragment on grid view item click

on Wednesday, January 28, 2015


I'm trying to implement a calendar for Android. So I have a fragment which represents the calendar(a gridview) and I try to change the current fragment by clicking on an item of the grid. So I've tried to put a setOnItemClickListener on the gridview :



public class CalendarFragment extends Fragment implements View.OnClickListener {
private GridView calendarView;
private GridCellAdapter adapter;

@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState ) {

View rootView = inflater.inflate( R.layout.fragment_calendar, container, false );

calendarView = (GridView) rootView.findViewById( R.id.calendar );
calendarView.setOnItemClickListener( new AdapterView.OnItemClickListener() {
@Override
public void onItemClick( AdapterView<?> parent, View view, int position, long id ) {
Fragment fragment = new DayCalendarFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace( R.id.frame_container, fragment )
.addToBackStack( null ).commit();

}
} );

adapter = new GridCellAdapter( getActivity(), R.id.calendar_day_gridcell, month, year );
adapter.notifyDataSetChanged();
calendarView.setAdapter( adapter );

return rootView;
}
}


But this doesn't work, I don't have any error but nothing happen when I click on it. I also tried to put the setOnItemClickListener in the onCreate method but it's the same. My problem is similar to this one : replace fragment on click of grid view item


Can you help me pls ?


0 comments:

Post a Comment