Java tutorial
/* * Copyright [2017] [zhi] * * 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.zhi.android.modules.welcome; import android.content.res.Resources; import android.os.Bundle; import android.support.annotation.IntRange; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewStub; import android.widget.TextView; import com.google.common.collect.Lists; import java.util.List; public class WelcomePageFragment extends Fragment { public static final String ARGS_PAGE_INDEX = "args-page-index"; public static final String ARGS_PAGE_OPTION = "args-page-option"; public static WelcomePageFragment newInstance(@IntRange(from = 0) int pageIndex, @NonNull WelcomePageOption pageOption) { final WelcomePageFragment pf = new WelcomePageFragment(); final Bundle args = new Bundle(); args.putInt(ARGS_PAGE_INDEX, pageIndex); args.putParcelable(ARGS_PAGE_OPTION, pageOption); pf.setArguments(args); return pf; } public WelcomePageFragment() { // LEFT EMPTY } @IntRange(from = 0) private int mPageIndex; private WelcomePageOption mPageOption; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mPageIndex = getArguments().getInt(ARGS_PAGE_INDEX); mPageOption = getArguments().getParcelable(ARGS_PAGE_OPTION); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.welcome_page, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); final ViewStub stub = view.findViewById(R.id.welcome_layout); if (mPageOption.layout != 0) { stub.setLayoutResource(mPageOption.layout); final View warmView = stub.inflate(); warmView.setTag(warmViewTag(mPageIndex)); warmView.setTag(R.id.welcome_layout, new WarmInfo(warmView, getResources())); } final TextView primaryView = view.findViewById(R.id.welcome_primary); if (mPageOption.primary != 0) { primaryView.setText(mPageOption.primary); } final TextView blurbView = view.findViewById(R.id.welcome_blurb); if (mPageOption.blurb != 0) { blurbView.setText(mPageOption.blurb); } } public static String warmViewTag(int pageIndex) { return "welcome:warm:tag" + pageIndex; } public static void onPageWarm(@NonNull View view, float offset) { final Object layoutTag = view.getTag(R.id.welcome_layout); if (layoutTag instanceof WarmInfo) { final WarmInfo warmInfo = (WarmInfo) layoutTag; warmInfo.onPageWarm(offset); } } private static class WarmInfo { final float layer2Offset; final float layer3Offset; final List<View> layers2Views; final List<View> layers3Views; WarmInfo(View view, Resources res) { layer2Offset = res.getDimension(R.dimen.layer_2_offset); layer3Offset = res.getDimension(R.dimen.layer_3_offset); layers2Views = Lists.newArrayList(); layers3Views = Lists.newArrayList(); with(view, layers2Views, layers3Views); } static void with(View view, List<View> layers2, List<View> layers3) { if (view instanceof ViewGroup) { final int childCount = ((ViewGroup) view).getChildCount(); for (int i = 0; i < childCount; i++) { with(((ViewGroup) view).getChildAt(i), layers2, layers3); } } else if ("2".equals(view.getTag())) { layers2.add(view); } else if ("3".equals(view.getTag())) { layers3.add(view); } } void onPageWarm(float offset) { for (View view : layers2Views) { view.setTranslationX(layer2Offset * offset); } for (View view : layers3Views) { view.setTranslationX(layer3Offset * offset); } } } }