Android:ViewSwitcher

ViewSwitcher

1. ViewSwitcher is a subclass of ViewAnimator which is a subclass of Fragment.

2. A ViewSwitcher is a ViewAnimator that switches between two views, and has a factory(ViewFactory) from which these views are created.

  You can either use the factory to create the views, or add them yourself. A ViewSwitcher can only have two child views, of which only one is shown at a time.

3.The following demo is a sample to learn about ViewSwitcher:

activity_main.xml

<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"
    tools:context=".MainActivity" >
    
    <com.slowalker.viewswitcher.SlideMenuSwitcher
        android:id="@+id/slide_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    
    <Button 
        android:id="@+id/pre_screen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="Previous Screen"
        />
    
    <Button 
        android:id="@+id/next_screen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Next Screen"
        />

</RelativeLayout>
View Code

 

MainActivity.java

package com.slowalker.viewswitcher;

import java.util.ArrayList;

import com.slowalker.viewswitcher.MenuData.DataItem;
import com.slowalker.viewswitcher.R.id;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/*MainActivity*/

public class MainActivity extends Activity implements OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();
    private static final boolean DEBUG = true;
    
    private SlideMenuSwitcher switcher;
    private Button preBtn;
    private Button nextBtn;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (DEBUG) {
            Log.d(TAG, "onCreate");
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        switcher = (SlideMenuSwitcher) findViewById(R.id.slide_view);
        switcher.setData(makeItems());
        preBtn = (Button) findViewById(R.id.pre_screen);
        nextBtn = (Button) findViewById(R.id.next_screen);
        preBtn.setOnClickListener(this);
        nextBtn.setOnClickListener(this);
    }
    
    private ArrayList<DataItem> makeItems() {
        ArrayList<DataItem> items = new ArrayList<DataItem>();
        for (int i = 0; i < 32; i++) {
            items.add(new DataItem("App " + i, getResources().getDrawable(R.drawable.ic_launcher)));
        }
        return items;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
        case R.id.pre_screen:
            switcher.showPreScreen();
            break;
        case R.id.next_screen:
            switcher.showNextScreen();
            break;
        default:
            break;
        }
    }
    
    
}
View Code

 

SlideMenuSwitcher.java

package com.slowalker.viewswitcher;

import java.util.ArrayList;

import com.slowalker.viewswitcher.MenuData.DataItem;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.GridView;
import android.widget.ViewSwitcher;

/*A ViewSwitcher implements by yourself.*/
public class SlideMenuSwitcher extends ViewSwitcher {

    private static final String TAG = SlideMenuSwitcher.class.getSimpleName();
    private static final boolean DEBUG = true;
    
    private MenuData mMenuData;
    private int mCurrentScreenId;
    private Context mContext;
    
    public SlideMenuSwitcher(Context context) {
        super(context);
        if (DEBUG) {
            Log.d(TAG, "SlideMenuSwitcher(Context context) is invoked.");
        }
        init(context);
    }
    
    public SlideMenuSwitcher(Context context,AttributeSet attri) {
        super(context, attri);
        if (DEBUG) {
            Log.d(TAG, "SlideMenuSwitcher(Context context,AttributeSet attri) is invoked.");
        }
        init(context);
    }
    
    private void init(Context context){
        if (DEBUG) {
            Log.d(TAG, "init");
        }
        mContext = context;
        setFactory(new ViewFactory() {
            
            @Override
            public View makeView() {
                GridView gv = new GridView(mContext);
                //gv.setColumnWidth(3);
                gv.setNumColumns(3);
                return gv;
            }
        });

    }
        
    public void setData(ArrayList<DataItem> items) {
        mMenuData = new MenuData();
        mMenuData.setMenuItems(items);
        mCurrentScreenId = mMenuData.getScreenNumber() >> 1;
        GridView gv = (GridView) getCurrentView();
        OneScreenAdapter adapter = new OneScreenAdapter(mContext, mMenuData.getScreen(mCurrentScreenId));
        gv.setAdapter(adapter);
    }
    
    public void showNextScreen() {
        if (mCurrentScreenId >= mMenuData.getScreenNumber() - 1) return;
        mCurrentScreenId++;
        setInAnimation(AnimationUtils.loadAnimation(mContext, android.R.anim.slide_in_left));
        setOutAnimation(AnimationUtils.loadAnimation(mContext, android.R.anim.slide_out_right));
        setViewData(mCurrentScreenId);
        showNext();
    }
    
    public void showPreScreen() {
        if (mCurrentScreenId <= 0) return;
        mCurrentScreenId--;
        setInAnimation(AnimationUtils.loadAnimation(mContext, android.R.anim.slide_in_left));
        setOutAnimation(AnimationUtils.loadAnimation(mContext, android.R.anim.slide_out_right));
        setViewData(mCurrentScreenId);
        /*I think here is wrong, showNext() will be ok. In fact, either method to invoke will work.*/
        showPrevious();
        //showNext();
    }

    private void setViewData(int index) {
        GridView gv = (GridView) getNextView();
        OneScreenAdapter adapter = new OneScreenAdapter(mContext, mMenuData.getScreen(index));
        gv.setAdapter(adapter);
    }
    
}
View Code

 

MenuData.java

 

package com.slowalker.viewswitcher;

import java.util.ArrayList;

import android.graphics.drawable.Drawable;
/*Data Class*/
public class MenuData {
    
    public static final int APP_CNT_ONE_SCREEN = 9;
    
    private ArrayList<MenuDataOneScreen> mScreens = new ArrayList<MenuDataOneScreen>();
    
    public void setMenuItems(ArrayList<DataItem> items) {
        int size = items.size();
        int screenCnt = size / APP_CNT_ONE_SCREEN;
        if (size % APP_CNT_ONE_SCREEN != 0) screenCnt++;
        for (int i = 0; i < screenCnt; i++) {
            MenuDataOneScreen screen = new MenuDataOneScreen();
            int l = i * APP_CNT_ONE_SCREEN, r = Math.min(l + APP_CNT_ONE_SCREEN, size) - 1;
            for (int j = l; j <= r; j++) screen.mDataItems.add(items.get(j));
            mScreens.add(screen);
        }
    }
    
    public int getScreenNumber() {
        return mScreens.size();
    }
    
    public MenuDataOneScreen getScreen(int index) {
        return mScreens.get(index);
    }
    
    
    public static class DataItem {
        private String name;
        private Drawable drawable;
        
        public DataItem(String name, Drawable drawable) {
            this.name = name;
            this.drawable = drawable;
        }
        
        public String getName() {
            return this.name;
        }
        
        public Drawable getDrawable() {
            return this.drawable;
        }
    }
    
    public static class MenuDataOneScreen {
        ArrayList<DataItem> mDataItems = new ArrayList<DataItem>();
    }
}
View Code

 

 

 

appdesc.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <ImageView
        android:id="@+id/app_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <TextView 
        android:id="@+id/app_name"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginTop="10dp" 
        />

</LinearLayout>
View Code

 

 

 

OneScreenAdapter.java

package com.slowalker.viewswitcher;

import com.slowalker.viewswitcher.MenuData.MenuDataOneScreen;

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;

/*Adapter for you gridview in a single screen.*/
public class OneScreenAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private MenuDataOneScreen screen;
    
    public OneScreenAdapter(Context context, MenuDataOneScreen oneScreen) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        screen = oneScreen; 
    }
    
    @Override
    public int getCount() {
        return screen.mDataItems.size();
    }

    @Override
    public Object getItem(int position) {
        return screen.mDataItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = inflater.inflate(R.layout.appdesc, null);
        }
        ImageView iv = (ImageView) view.findViewById(R.id.app_logo);
        TextView tv = (TextView) view.findViewById(R.id.app_name);
        iv.setImageDrawable(screen.mDataItems.get(position).getDrawable());
        tv.setText(screen.mDataItems.get(position).getName());
        return view;
    }

}
View Code

 

 

 

posted @ 2013-11-01 18:54  slowalker  阅读(295)  评论(0编辑  收藏  举报