package neongarage.slakr.Course; /** * Created by Aaron on 1/22/2015. * Refactored to RecyclerView 10/5/2015 */ import android.support.v4.app.Fragment; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import neongarage.slakr.MySQLiteHelper; import neongarage.slakr.R; public class CourseListFragment extends Fragment { private RecyclerView mRecyclerView; private MySQLiteHelper db; private CourseAdapter courseAdapter; private RecyclerView.LayoutManager mLayoutManager; List<Course> courses = new ArrayList<Course>(); private View view; private static final String TAG = "RecyclerViewFragment"; private static final String KEY_LAYOUT_MANAGER = "layoutManager"; private static final int SPAN_COUNT = 2; private enum LayoutManagerType { GRID_LAYOUT_MANAGER, LINEAR_LAYOUT_MANAGER } protected LayoutManagerType mCurrentLayoutManagerType; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment view = inflater.inflate(R.layout.fragment_course_list, container, false); db = new MySQLiteHelper(getActivity()); courses = db.getAllCourses(); mRecyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view); mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false); mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER; if (savedInstanceState != null) { // Restore saved layout manager type. mCurrentLayoutManagerType = (LayoutManagerType) savedInstanceState .getSerializable(KEY_LAYOUT_MANAGER); } setRecyclerViewLayoutManager(mCurrentLayoutManagerType); courseAdapter = new CourseAdapter(courses); mRecyclerView.setAdapter(courseAdapter); return view; } public void onSaveInstanceState(Bundle savedState) { super.onSaveInstanceState(savedState); } private HashMap<String, String> createCourse(String key, String name) { HashMap<String, String> planet = new HashMap<String, String>(); planet.put(key, name); return planet; } public void addCourse(Course newCourse){ courses.add(newCourse); db.addCourse(newCourse); courseAdapter.notifyItemInserted(courses.size()-1); } public void setRecyclerViewLayoutManager(LayoutManagerType layoutManagerType) { int scrollPosition = 0; // If a layout manager has already been set, get current scroll position. if (mRecyclerView.getLayoutManager() != null) { scrollPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager()) .findFirstCompletelyVisibleItemPosition(); } switch (layoutManagerType) { case GRID_LAYOUT_MANAGER: mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT); mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER; break; case LINEAR_LAYOUT_MANAGER: default: mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false); mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER; break; } mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerView.scrollToPosition(scrollPosition); mRecyclerView.getItemAnimator(); } }