package com.marshalchen.common.demoofui.recyclerplayground.fragments; import android.app.Fragment; import android.os.Bundle; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.Toast; import com.marshalchen.common.demoofui.R; import com.marshalchen.common.demoofui.recyclerplayground.adapters.SimpleAdapter; public abstract class RecyclerFragment extends Fragment implements AdapterView.OnItemClickListener { private RecyclerView mList; private SimpleAdapter mAdapter; /** Required Overrides for Sample Fragments */ protected abstract RecyclerView.LayoutManager getLayoutManager(); protected abstract RecyclerView.ItemDecoration getItemDecoration(); protected abstract int getDefaultItemCount(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.recyclerview_playground_fragment_recycler, container, false); mList = (RecyclerView) rootView.findViewById(R.id.section_list); mList.setLayoutManager(getLayoutManager()); mList.addItemDecoration(getItemDecoration()); mAdapter = new SimpleAdapter(); mAdapter.setItemCount(getDefaultItemCount()); mAdapter.setOnItemClickListener(this); mList.setAdapter(mAdapter); return rootView; } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.recyclerview_playground_grid_options, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_add: mAdapter.addItem(); return true; case R.id.action_remove: mAdapter.removeItem(); return true; case R.id.action_empty: mAdapter.setItemCount(0); return true; case R.id.action_small: mAdapter.setItemCount(5); return true; case R.id.action_medium: mAdapter.setItemCount(25); return true; case R.id.action_large: mAdapter.setItemCount(196); return true; case R.id.action_scroll_zero: mList.scrollToPosition(0); return true; case R.id.action_smooth_zero: mList.smoothScrollToPosition(0); return true; default: return super.onOptionsItemSelected(item); } } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getActivity(), "Clicked: " + position + ", index " + mList.indexOfChild(view), Toast.LENGTH_SHORT).show(); } }