VIDEO
compile 'com.cleveroad:splittransformation:0.9.0'
activity_main
<? xml version= "1.0" encoding= "utf-8" ?>
<FrameLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
android :id= "@+id/container"
android :layout_width= "match_parent"
android :layout_height= "match_parent" >
</FrameLayout >
fragment_main
<? xml version= "1.0" encoding= "utf-8" ?>
<RelativeLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
xmlns: tools = "http://schemas.android.com/tools"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :background= "@color/bg_color"
android :padding= "@dimen/activity_horizontal_margin"
android :gravity= "center"
tools :context= ".MainActivity" >
<Button
android :id= "@+id/btn_views"
android :layout_width= "match_parent"
android : layout_height = "wrap_content"
android :text= "@string/simple_views" />
<Button
android :id= "@+id/btn_fragments"
android :layout_width= "match_parent"
android : layout_height = "wrap_content"
android :text= "@string/fragments"
android :layout_below= "@+id/btn_views" />
</RelativeLayout >
fragment_view_pager
<? xml version= "1.0" encoding= "utf-8" ?>
<RelativeLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
xmlns: app = "http://schemas.android.com/apk/res-auto"
xmlns: tools = "http://schemas.android.com/tools"
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :background= "@color/bg_color"
tools :context= ".MainActivity" >
<android.support.v4.view.ViewPager
android :id= "@+id/view_pager"
android :layout_width= "match_parent"
android :layout_height= "match_parent" >
</android.support.v4.view.ViewPager >
<com.cleveroad.splittransformation.SquareViewPagerIndicator
android :id= "@+id/indicator"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :layout_marginTop= "@dimen/pager_indicator_margin"
app :trans_debugItemsCount= "4" />
<Button
style= "@style/Widget.AppCompat.Button.Borderless"
android :layout_width= "wrap_content"
android :layout_height= "wrap_content"
android :layout_alignParentBottom= "true"
android :layout_alignParentEnd= "true"
android :layout_alignParentRight= "true"
android :text= "@string/skip"
android :textColor= "#7b6d63" />
</RelativeLayout >
pager_item
<? xml version= "1.0" encoding= "utf-8" ?>
<FrameLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
xmlns: tools = "http://schemas.android.com/tools"
android :layout_width= "@dimen/page_width"
android :layout_height= "wrap_content" >
<ImageView
android :id= "@+id/image"
android :layout_width= "@dimen/page_width"
android :layout_height= "@dimen/page_height"
android :layout_gravity= "center_horizontal"
android :background= "@color/white"
android :scaleType= "centerInside"
tools :src= "@drawable/administrator" />
</FrameLayout >
pager_item_et
<? xml version= "1.0" encoding= "utf-8" ?>
<FrameLayout
xmlns: android = "http://schemas.android.com/apk/res/android"
xmlns: tools = "http://schemas.android.com/tools"
android :layout_width= "@dimen/page_width"
android :layout_height= "wrap_content" >
<ImageView
android :id= "@+id/image"
android :layout_width= "@dimen/page_width"
android :layout_height= "@dimen/page_height"
android :background= " @color/white "
android :layout_gravity= "center_horizontal"
android :scaleType= "centerInside"
tools :src= "@drawable/administrator" />
<EditText
android :id= "@+id/text"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :layout_gravity= "bottom"
android :layout_margin= "16dp"
android :inputType= "text" />
</FrameLayout >
public class FragmentsFragment extends Fragment {
public static FragmentsFragment instance() {
return new FragmentsFragment();
}
private ViewPager viewPager ;
private SquareViewPagerIndicator indicator ;
@Nullable @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view_pager , container, false );
viewPager = (ViewPager) view.findViewById(R.id.view_pager );
indicator = (SquareViewPagerIndicator) view.findViewById(R.id.indicator );
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super .onViewCreated(view, savedInstanceState);
SimplePagerAdapter adapter = new SimplePagerAdapter(getChildFragmentManager());
TransformationAdapterWrapper wrapper = TransformationAdapterWrapper
.wrap (getContext(), adapter)
.rows(10 )
.columns(7 )
.marginTop(getResources().getDimensionPixelSize(R.dimen.margin_top ))
.build();
viewPager .setAdapter(wrapper);
viewPager .setPageTransformer(false , wrapper);
indicator .initializeWith(viewPager );
}
@Override
public void onDestroyView() {
indicator .reset();
super .onDestroyView();
}
@Override
public void onResume() {
super .onResume();
getActivity().setTitle(R.string.fragments );
}
private static class SimplePagerAdapter extends FragmentStatePagerAdapter {
private final int [] drawables = new int [] {
R.drawable.administrator ,
R.drawable.cashier ,
R.drawable.cook ,
R.drawable.administrator ,
R.drawable.cashier ,
R.drawable.cook ,
R.drawable.administrator ,
R.drawable.cashier ,
R.drawable.cook ,
};
public SimplePagerAdapter(FragmentManager fm) {
super (fm);
}
@Override
public Fragment getItem(int position) {
return PagerFragment.instance (drawables [position]);
}
@Override
public int getCount() {
return drawables .length ;
}
}
}public class MainFragment extends Fragment {
private Button btnViews , btnFragments ;
public static MainFragment instance() {
return new MainFragment();
}
@Nullable @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main , container, false );
btnViews = (Button) view.findViewById(R.id.btn_views );
btnFragments = (Button) view.findViewById(R.id.btn_fragments );
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super .onViewCreated(view, savedInstanceState);
btnViews .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.replace(R.id.container , SimpleViewsFragment.instance ())
.addToBackStack(null )
.commit();
}
});
btnFragments .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction()
.replace(R.id.container , FragmentsFragment.instance ())
.addToBackStack(null )
.commit();
}
});
}
@Override
public void onResume() {
super .onResume();
getActivity().setTitle(R.string.app_name );
}
}
public class PagerFragment extends Fragment {
private static final String KEY_DRAWABLE = "DRAWABLE" ;
private static final String KEY_TEXT = "TEXT" ;
private TextView textView ;
public static PagerFragment instance(@DrawableRes int drawableId) {
Bundle args = new Bundle();
args.putInt(KEY_DRAWABLE , drawableId);
PagerFragment fragment = new PagerFragment();
fragment.setArguments(args);
return fragment;
}
@Nullable @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.pager_item_et , container, false );
ImageView imageView = (ImageView) view.findViewById(R.id.image );
imageView.setImageDrawable(getContext().getResources().getDrawable(getArguments().getInt(KEY_DRAWABLE )));
textView = (TextView) view.findViewById(R.id.text );
return view;
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString(KEY_TEXT , textView .getText().toString());
super .onSaveInstanceState(outState);
}
@Override
public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
super .onViewStateRestored(savedInstanceState);
if (savedInstanceState != null ) {
textView .setText(savedInstanceState.getString(KEY_TEXT ));
}
}
}public class SimpleViewsFragment extends Fragment {
public static SimpleViewsFragment instance() {
return new SimpleViewsFragment();
}
private ViewPager viewPager ;
private SquareViewPagerIndicator indicator ;
@Nullable @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view_pager , container, false );
viewPager = (ViewPager) view.findViewById(R.id.view_pager );
indicator = (SquareViewPagerIndicator) view.findViewById(R.id.indicator );
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super .onViewCreated(view, savedInstanceState);
SimplePagerAdapter adapter = new SimplePagerAdapter(getContext());
TransformationAdapterWrapper wrapper = TransformationAdapterWrapper
.wrap (getContext(), adapter)
.rows(10 )
.columns(7 )
.marginTop(getResources().getDimensionPixelSize(R.dimen.margin_top ))
.bitmapScale(1f )
.build();
viewPager .setAdapter(wrapper);
viewPager .setPageTransformer(false , wrapper);
indicator .initializeWith(viewPager );
}
@Override
public void onDestroyView() {
indicator .reset();
super .onDestroyView();
}
@Override
public void onResume() {
super .onResume();
getActivity().setTitle(R.string.simple_views );
}
private static class SimplePagerAdapter extends PagerAdapter {
private final int [] drawables = new int [] {
R.drawable.administrator ,
R.drawable.cashier ,
R.drawable.cook ,
R.drawable.administrator ,
R.drawable.cashier ,
R.drawable.cook ,
R.drawable.administrator ,
R.drawable.cashier ,
R.drawable.cook ,
};
private final Context context ;
private final LayoutInflater inflater ;
public SimplePagerAdapter(Context context) {
this .context = context;
this .inflater = LayoutInflater.from (context);
}
@Override
public int getCount() {
return drawables .length ;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = inflater .inflate(R.layout.pager_item , container, false );
ImageView imageView = (ImageView) view.findViewById(R.id.image );
imageView.setImageDrawable(ContextCompat.getDrawable (context , drawables [position]));
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main );
if (savedInstanceState == null ) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container , MainFragment.instance ())
.commit();
}
}
}
No comments:
Post a Comment