Back to project page android-for-dummies-v3.
The source code is released under:
Apache License
If you think the Android project android-for-dummies-v3 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.dummies.tasks.fragment; /*from w w w . j a v a2s . c o m*/ import android.app.Fragment; import android.app.LoaderManager; import android.content.CursorLoader; import android.content.Intent; import android.content.Loader; import android.database.Cursor; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; 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 com.dummies.tasks.R; import com.dummies.tasks.activity.PreferencesActivity; import com.dummies.tasks.adapter.TaskListAdapter; import com.dummies.tasks.interfaces.OnEditTask; import com.dummies.tasks.provider.TaskProvider; public class TaskListFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> { TaskListAdapter adapter; RecyclerView recyclerView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); adapter = new TaskListAdapter(); getLoaderManager().initLoader(0, null, this); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View v = inflater.inflate(R.layout.fragment_task_list, container, false); recyclerView = (RecyclerView) v.findViewById(R.id.recycler); recyclerView.setAdapter(adapter); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager( new LinearLayoutManager(getActivity())); return v; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setHasOptionsMenu(true); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.menu_list, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_insert: ((OnEditTask) getActivity()).editTask(0); return true; case R.id.menu_settings: startActivity(new Intent(getActivity(), PreferencesActivity.class)); return true; } return super.onOptionsItemSelected(item); } @Override public Loader<Cursor> onCreateLoader(int ignored, final Bundle args) { return new CursorLoader(getActivity(), TaskProvider.CONTENT_URI, null, null, null, null); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { adapter.swapCursor(cursor); } @Override public void onLoaderReset(Loader<Cursor> loader) { // This is called when the last Cursor provided to // onLoadFinished() // above is about to be closed. We need to make sure we are no // longer using it. adapter.swapCursor(null); } }