Android ViewSwitcher 使用示例

界面布局文件 res\layout\slide_list_view.xml



图标布局文件 res\layout\label_icon.xml




    

    


自定义动画资源 res\anim\slide_in_right.xml



    

自定义动画资源 res\anim\slide_out_left.xml



    

主布局文件:




    

    

主程序代码:

package com.toby.personal.testlistview;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    final private static String TAG = "Toby_Test";

    final private static int NUMBER_PRE_SCREEN = 12;

    private static class DataItem {
        String dataName;
        Drawable drawable;
    }

    private ArrayList items = new ArrayList<>();

    private int screenNo = -1;
    private int screenCount;

    ViewSwitcher viewSwitcher;

    LayoutInflater inflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        inflater = LayoutInflater.from(this);

        for (int i = 0; i < 40; ++i) {
            String label = String.valueOf(i);
            Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher, null);
            DataItem item = new DataItem();
            item.dataName = label;
            item.drawable = drawable;
            items.add(item);
        }

        screenCount = items.size() % NUMBER_PRE_SCREEN == 0 ?
                items.size() / NUMBER_PRE_SCREEN : items.size() / NUMBER_PRE_SCREEN + 1;

        viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher);
        viewSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                return inflater.inflate(R.layout.slide_list_view, null);
            }
        });

        next(null);
    }

    public void prev(View view) {
        if (screenNo > 0) {
            --screenNo;
            viewSwitcher.setInAnimation(this, android.R.anim.slide_in_left);
            viewSwitcher.setOutAnimation(this, android.R.anim.slide_out_right);
            ((GridView) viewSwitcher.getNextView()).setAdapter(adapter);
            viewSwitcher.showPrevious();
        }
    }

    public void next(View view) {
        if (screenNo < screenCount - 1) {
            ++screenNo;
            viewSwitcher.setInAnimation(this, R.anim.slide_in_right);
            viewSwitcher.setOutAnimation(this, R.anim.slide_out_left);
            ((GridView) viewSwitcher.getNextView()).setAdapter(adapter);
            viewSwitcher.showNext();
        }
    }

    private BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            if (screenNo == screenCount - 1 && items.size() % NUMBER_PRE_SCREEN != 0) {
                return items.size() % NUMBER_PRE_SCREEN;
            }
            return NUMBER_PRE_SCREEN;
        }

        @Override
        public DataItem getItem(int i) {
            return items.get(screenNo * NUMBER_PRE_SCREEN + i);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            if (convertView == null) {
                view = inflater.inflate(R.layout.label_icon, null);
            }

            final DataItem dataItem = getItem(position);

            if (dataItem != null) {
                ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
                if (imageView != null) {
                    imageView.setImageDrawable(dataItem.drawable);
                }

                TextView textView = (TextView) view.findViewById(R.id.textView);
                if (textView != null) {
                    textView.setText(dataItem.dataName);
                }
            }

            return view;
        }
    };
}

运行效果图:


Android ViewSwitcher 使用示例_第1张图片
ViewSwitcher 使用示例

参考文献:《疯狂Android讲义(第2版)》

你可能感兴趣的:(Android ViewSwitcher 使用示例)