|
| 1 | +package com.devbrackets.android.recyclerextdemo.ui.fragment; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.support.v4.app.Fragment; |
| 6 | +import android.support.v7.widget.LinearLayoutManager; |
| 7 | +import android.support.v7.widget.RecyclerView; |
| 8 | +import android.view.LayoutInflater; |
| 9 | +import android.view.View; |
| 10 | +import android.view.ViewGroup; |
| 11 | + |
| 12 | +import com.devbrackets.android.recyclerext.adapter.RecyclerHeaderAdapter; |
| 13 | +import com.devbrackets.android.recyclerext.decoration.StickyHeaderDecoration; |
| 14 | +import com.devbrackets.android.recyclerextdemo.R; |
| 15 | +import com.devbrackets.android.recyclerextdemo.data.database.DBHelper; |
| 16 | +import com.devbrackets.android.recyclerextdemo.data.database.ItemDAO; |
| 17 | +import com.devbrackets.android.recyclerextdemo.ui.viewholder.ContactsHeaderViewHolder; |
| 18 | +import com.devbrackets.android.recyclerextdemo.ui.viewholder.SimpleTextViewHolder; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | + |
| 23 | +/** |
| 24 | + * An example of the {@link HeaderAsChildListFragment.HeaderAdapter} |
| 25 | + * that has the display style of the Lollipop and Marshmallow Contacts app |
| 26 | + * using the {@link StickyHeaderDecoration} to keep the header at the top of the screen when reached. |
| 27 | + */ |
| 28 | +public class HeaderAsChildListFragment extends Fragment { |
| 29 | + private DBHelper dbHelper; |
| 30 | + private RecyclerView recyclerView; |
| 31 | + |
| 32 | + public static HeaderAsChildListFragment newInstance() { |
| 33 | + return new HeaderAsChildListFragment(); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 38 | + View view = inflater.inflate(R.layout.fragment_recycler, container, false); |
| 39 | + recyclerView = (RecyclerView)view.findViewById(R.id.recyclerext_fragment_recycler); |
| 40 | + return view; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void onActivityCreated(Bundle savedInstanceState) { |
| 45 | + super.onActivityCreated(savedInstanceState); |
| 46 | + |
| 47 | + //Makes sure the database is initialized and open for use |
| 48 | + dbHelper = new DBHelper(getActivity()); |
| 49 | + setupRecyclerExt(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Retrieves the items from the database, and sets the layout manager, adapter, and sticky decoration |
| 54 | + * on the RecyclerView. |
| 55 | + */ |
| 56 | + private void setupRecyclerExt() { |
| 57 | + HeaderAdapter adapter = new HeaderAdapter(getActivity(), ItemDAO.findAll(dbHelper.getWritableDatabase())); |
| 58 | + |
| 59 | + recyclerView.setAdapter(adapter); |
| 60 | + recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); |
| 61 | + |
| 62 | + //OPTIONAL: The StickyHeaderDecoration is used to keep the current header always visible |
| 63 | + recyclerView.addItemDecoration(new StickyHeaderDecoration(recyclerView)); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * The adapter that extends the {@link RecyclerHeaderAdapter} to provide the |
| 68 | + * minimum number of methods to function |
| 69 | + */ |
| 70 | + private class HeaderAdapter extends RecyclerHeaderAdapter<ContactsHeaderViewHolder, SimpleTextViewHolder> { |
| 71 | + private LayoutInflater inflater; |
| 72 | + private List<ItemDAO> items; |
| 73 | + |
| 74 | + public HeaderAdapter(Context context, List<ItemDAO> items) { |
| 75 | + this.items = items; |
| 76 | + inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 77 | + |
| 78 | + //This is the call that makes the adapter treat the headers position as a child |
| 79 | + // e.g. CHILD(position=9, getItem(9)), HEADER(position=10, getItem(10)), CHILD(position=11, getItem(11)) |
| 80 | + // whereas normally the header doesn't interfere with the child items |
| 81 | + // e.g. CHILD(position=9, getItem(9)), HEADER(position=10, getItem(10)), CHILD(position=11, getItem(10)) |
| 82 | + showHeaderAsChild(true); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public ContactsHeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent, int viewType) { |
| 87 | + return ContactsHeaderViewHolder.newInstance(inflater, parent); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public SimpleTextViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) { |
| 92 | + return SimpleTextViewHolder.newInstance(inflater, parent); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void onBindHeaderViewHolder(ContactsHeaderViewHolder holder, int childPosition) { |
| 97 | + ItemDAO item = items.get(childPosition); |
| 98 | + |
| 99 | + holder.setText(item.getText()); |
| 100 | + holder.setRegionText(childPosition / 10 + ""); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onBindChildViewHolder(SimpleTextViewHolder holder, int childPosition) { |
| 105 | + holder.setText(items.get(childPosition).getText()); |
| 106 | + holder.setSpacingVisible(true); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public int getChildCount() { |
| 111 | + return items.size(); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * For simplicity sake, we just return a simple mathematical id for the headers. |
| 116 | + * You should provide an actual id. |
| 117 | + */ |
| 118 | + @Override |
| 119 | + public long getHeaderId(int childPosition) { |
| 120 | + return items.get(childPosition).getOrder() / 10; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Specifying this will make only the number field from the header be sticky |
| 125 | + */ |
| 126 | + @Override |
| 127 | + public int getCustomStickyHeaderViewId() { |
| 128 | + return R.id.contacts_header_item_region_text_view; |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments