Use ViewSwitcher to simulate the split and switch of mobile phone screen applications

Source: Internet
Author: User

ViewSwitcher is often used to switch between two views with animation effects. If you try to display only images, you can also use ImageSwitcher. As described earlier, this article simulates the screen menu on the mobile phone, apply the function of split-screen display and switching, and implement slide-screen animation. The following describes how to improve and optimize some content source networks:

SlideMenuActivity:

Package com. home. testviewswitcher; import java. util. arrayList; import android. app. activity; import android. graphics. drawable. drawable; import android. OS. bundle; import android. view. gestureDetector; import android. view. gestureDetector. onGestureListener; import android. view. motionEvent; import com. home. testviewswitcher. menuData. dataItem; public class SlideMenuActivity extends Activity implements OnGestureListener {private SlideMenuSwitcher switcher; private GestureDetector detecter; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); switcher = new SlideMenuSwitcher (this); setContentView (switcher); detecter = new GestureDetector (this, this); switcher. setData (makeItems ();}/*** Add the initial application ** @ return */private ArrayList <DataItem> makeItems () {ArrayList <DataItem> items = new ArrayList <DataItem> (); for (int I = 1; I <54; I ++) {String label = "App" + I; drawable drawable = getResources (). getDrawable (R. drawable. ic_launcher); DataItem item = new DataItem (); item. dataName = label; item. drawable = drawable; items. add (item) ;}return items ;}@ Overridepublic boolean onTouchEvent (MotionEvent event) {return detecter. onTouchEvent (event) ;}@ Overridepublic boolean onDown (MotionEvent arg0) {return false ;}@ Overridepublic boolean onFling (MotionEvent e1, MotionEvent e2, float arg2, float arg3) {float instance = e1.getX ()-e2.getX (); if (instance> 10) {switcher. showNextScreen ();} else if (instance <10) {switcher. showpreviusscreen () ;}return false ;}@ Overridepublic void onLongPress (MotionEvent arg0) {}@ Overridepublic boolean onScroll (MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) {return false ;}@ Overridepublic void onShowPress (MotionEvent arg0) {}@ Overridepublic boolean onSingleTapUp (MotionEvent arg0) {return false ;}@ Overridepublic boolean dispatchTouchEvent (MotionEvent ev) {detecter. onTouchEvent (ev); return super. dispatchTouchEvent (ev );}}

SlideMenuSwitcher:

Package com. home. testviewswitcher; import java. util. arrayList; import android. content. context; import android. view. view; import android. widget. gridView; import android. widget. viewSwitcher; import com. home. testviewswitcher. menuData. dataItem;/*** custom ViewSwitcher for screen sharding and switching ** @ author Administrator **/public class SlideMenuSwitcher extends ViewSwitcher {private MenuData mMenuData; private int mCurrentScreen; // The current screen index private Context mContext; public SlideMenuSwitcher (Context context) {super (context); mContext = context; setFactory (new SlideViewFactory ());} /*** set the data and display the initial screen ** @ param dataItems */public void setData (ArrayList <DataItem> dataItems) {mMenuData = new MenuData (); mMenuData. setMenuItems (dataItems); // sets the index mCurrentScreen = mMenuData for the screen to be displayed. getScreenNumber ()/2; GridView listView = (GridView) getCurrentView (); OneScreenListAdapter adapter = new OneScreenListAdapter (mContext); adapter. setScreenData (mMenuData. getScreen (mCurrentScreen); listView. setAdapter (adapter);}/*** display next screen */public void showNextScreen () {if (mCurrentScreen <mMenuData. getScreenNumber ()-1) {mCurrentScreen ++; setInAnimation (mContext, R. anim. push_left_in); setOutAnimation (mContext, R. anim. push_left_out);} else {return;} setViewData (mCurrentScreen); showNext ();}/*** display the previous screen */public void showpreviusscreen () {if (mCurrentScreen> 0) {mCurrentScreen --; setInAnimation (mContext, R. anim. push_right_in); setOutAnimation (mContext, R. anim. push_right_out);} else {return;} setViewData (mCurrentScreen); showPrevious ();} /*** set data for the GridView and refresh ** @ param index */private void setViewData (int index) {GridView gridView = (GridView) getNextView (); oneScreenListAdapter adapter = new OneScreenListAdapter (mContext); adapter. setScreenData (mMenuData. getScreen (index); gridView. setAdapter (adapter);}/*** factory, construct the content of the GridView as ViewSwitcher ** @ author Administrator **/class SlideViewFactory implements ViewFactory {public View makeView () {GridView gridView = new GridView (mContext); gridView. setNumColumns (3); gridView. setVerticalSpacing (20); gridView. setHorizontalSpacing (20); return gridView ;}}}

OneScreenListAdapter:

package com.home.testviewswitcher;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import com.home.testviewswitcher.MenuData.MenuDataOneScreen;public class OneScreenListAdapter extends BaseAdapter {private MenuDataOneScreen mScreen;private LayoutInflater mInflater;public OneScreenListAdapter(Context context) {mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);}public void setScreenData(MenuDataOneScreen screenData) {mScreen = screenData;}public int getCount() {return mScreen.mDataItems.size();}public Object getItem(int position) {return mScreen.mDataItems.get(position);}public long getItemId(int position) {return position;}public View getView(int position, View convertView, ViewGroup parent) {View view = convertView;if (convertView == null) {view = mInflater.inflate(R.layout.gridview_grid, null);}ImageView imageView = (ImageView) view.findViewById(R.id.imageview);TextView textView = (TextView) view.findViewById(R.id.textview);imageView.setImageDrawable(mScreen.mDataItems.get(position).drawable);textView.setText(mScreen.mDataItems.get(position).dataName);return view;}}

MenuData:

Package com. home. testviewswitcher; import java. util. arrayList; import android. graphics. drawable. drawable; /*** simulate the data section of the function menu ** @ author Administrator **/public class MenuData {/** number of applications per screen */public static final int NUMBER_IN_ONE_SCREEN = 12; /** set of all screens */public ArrayList <MenuDataOneScreen> mScreens = new ArrayList <MenuDataOneScreen> (); /** this class indicates the data part of each application */public static class DataItem {public String dataName; // Application name public Drawable drawable; // application icon}/** this class represents all applications on a screen */public static class MenuDataOneScreen {ArrayList <DataItem> mDataItems = new ArrayList <DataItem> ();} /*** assign data to this class ** @ param dataItems */public void setMenuItems (ArrayList <DataItem> dataItems) {// obtain the number of screens int screenNum = dataItems. size ()/NUMBER_IN_ONE_SCREEN; int remain = dataItems. size () % NUMBER_IN_ONE_SCREEN; scre EnNum + = remain = 0? 0: 1; // set int pos = 0 for each screen; for (int I = 0; I <screenNum; I ++) {MenuDataOneScreen screen = new MenuDataOneScreen (); for (int j = 0; j <NUMBER_IN_ONE_SCREEN; j ++) {if (pos <= dataItems. size ()-1) {screen. mDataItems. add (dataItems. get (pos); pos ++ ;}} mScreens. add (screen) ;}/ *** get screen count *** @ return */public int getScreenNumber () {return mScreens. size ();}/*** obtain the data of a screen based on the screen index ** @ param screenIndex * @ return */public MenuDataOneScreen getScreen (int screenIndex) {return mScreens. get (screenIndex );}}

The animation used here is the same as the one used to customize an ImageSwitcher.

Gridview_grid.xml :( layout in each GridView ):

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <ImageView        android:id="@+id/imageview"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/textview"        android:layout_below="@id/imageview"        android:layout_alignLeft="@id/imageview"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></RelativeLayout>






 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.