Java tutorial
/* * Copyright (C) 2015 Haruki Hasegawa * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.h6ah4i.android.example.advrecyclerview.demo_e_already_expanded; import android.graphics.drawable.NinePatchDrawable; import android.os.Build; import android.os.Bundle; import android.os.Parcelable; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; 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.h6ah4i.android.example.advrecyclerview.R; import com.h6ah4i.android.example.advrecyclerview.common.data.AbstractAddRemoveExpandableDataProvider; import com.h6ah4i.android.widget.advrecyclerview.animator.GeneralItemAnimator; import com.h6ah4i.android.widget.advrecyclerview.animator.RefactoredDefaultItemAnimator; import com.h6ah4i.android.widget.advrecyclerview.decoration.ItemShadowDecorator; import com.h6ah4i.android.widget.advrecyclerview.decoration.SimpleListDividerDecorator; import com.h6ah4i.android.widget.advrecyclerview.expandable.RecyclerViewExpandableItemManager; import com.h6ah4i.android.widget.advrecyclerview.utils.WrapperAdapterUtils; public class AlreadyExpandedGroupsExpandableExampleFragment extends Fragment { private static final String SAVED_STATE_EXPANDABLE_ITEM_MANAGER = "RecyclerViewExpandableItemManager"; private RecyclerView mRecyclerView; private RecyclerView.LayoutManager mLayoutManager; private AlreadyExpandedGroupsExpandableExampleAdapter mAdapter; private RecyclerView.Adapter mWrappedAdapter; private RecyclerViewExpandableItemManager mRecyclerViewExpandableItemManager; public AlreadyExpandedGroupsExpandableExampleFragment() { super(); setHasOptionsMenu(true); } @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_recycler_list_view, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); //noinspection ConstantConditions mRecyclerView = (RecyclerView) getView().findViewById(R.id.recycler_view); mLayoutManager = new LinearLayoutManager(getContext()); final Parcelable eimSavedState = (savedInstanceState != null) ? savedInstanceState.getParcelable(SAVED_STATE_EXPANDABLE_ITEM_MANAGER) : null; mRecyclerViewExpandableItemManager = new RecyclerViewExpandableItemManager(eimSavedState); // Expand all group items by default. This method must be called before creating a wrapper adapter. mRecyclerViewExpandableItemManager.setDefaultGroupsExpandedState(true); //adapter final AlreadyExpandedGroupsExpandableExampleAdapter myItemAdapter = new AlreadyExpandedGroupsExpandableExampleAdapter( mRecyclerViewExpandableItemManager, getDataProvider()); mAdapter = myItemAdapter; mWrappedAdapter = mRecyclerViewExpandableItemManager.createWrappedAdapter(myItemAdapter); // wrap for expanding final GeneralItemAnimator animator = new RefactoredDefaultItemAnimator(); // Change animations are enabled by default since support-v7-recyclerview v22. // Need to disable them when using animation indicator. animator.setSupportsChangeAnimations(false); mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerView.setAdapter(mWrappedAdapter); // requires *wrapped* adapter mRecyclerView.setItemAnimator(animator); mRecyclerView.setHasFixedSize(false); // additional decorations //noinspection StatementWithEmptyBody if (supportsViewElevation()) { // Lollipop or later has native drop shadow feature. ItemShadowDecorator is not required. } else { mRecyclerView.addItemDecoration(new ItemShadowDecorator( (NinePatchDrawable) ContextCompat.getDrawable(getContext(), R.drawable.material_shadow_z1))); } mRecyclerView.addItemDecoration(new SimpleListDividerDecorator( ContextCompat.getDrawable(getContext(), R.drawable.list_divider_h), true)); mRecyclerViewExpandableItemManager.attachRecyclerView(mRecyclerView); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // save current state to support screen rotation, etc... if (mRecyclerViewExpandableItemManager != null) { outState.putParcelable(SAVED_STATE_EXPANDABLE_ITEM_MANAGER, mRecyclerViewExpandableItemManager.getSavedState()); } } @Override public void onDestroyView() { if (mRecyclerViewExpandableItemManager != null) { mRecyclerViewExpandableItemManager.release(); mRecyclerViewExpandableItemManager = null; } if (mRecyclerView != null) { mRecyclerView.setItemAnimator(null); mRecyclerView.setAdapter(null); mRecyclerView = null; } if (mWrappedAdapter != null) { WrapperAdapterUtils.releaseAll(mWrappedAdapter); mWrappedAdapter = null; } mAdapter = null; mLayoutManager = null; super.onDestroyView(); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_e_already_expanded, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_add_group_bottom_2: mAdapter.addGroupAndChildItemsBottom(2, 3); return true; case R.id.menu_remove_group_bottom_2: mAdapter.removeGroupItemsBottom(2); return true; case R.id.menu_clear_groups: mAdapter.clearGroupItems(); return true; default: return super.onOptionsItemSelected(item); } } private boolean supportsViewElevation() { return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP); } public AbstractAddRemoveExpandableDataProvider getDataProvider() { return ((AlreadyExpandedGroupsExpandableExampleActivity) getActivity()).getDataProvider(); } }