[Android学UI之一]简单实现移动应用左右滑动导航页面


功能:

    实现图片切换。首页导航。


使用原因:

    使用起来非常的简单。只要一个类即可实现功能。


效果图:

    有图有真相。不过例子太简单了。图也简单。



实现左右滑动。


代码:Activity

package com.bbswp.viewswitcher;

import com.bbswp.viewswitcher.view.HorizontalPager;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        HorizontalPager realViewSwitcher = new HorizontalPager(getApplicationContext());

        // 添加不同的view,只可以是图片,也可以是自定义的view哦
        final int[] backgroundDrawables = {
                R.drawable.b1, R.drawable.b2, R.drawable.b3, R.drawable.b4
        };
        for (int i = 0; i < 4; i++) {
            View view = null;
            if (i < 3) {
                 view = new ImageView(getApplicationContext());
                view.setBackgroundResource(backgroundDrawables[i]);
            } else {//是可以自定义view的
                LayoutInflater inflater = LayoutInflater.from(this);
                view = inflater.inflate(R.layout.view_last, null);
            }
            realViewSwitcher.addView(view);
        }

        // 设置视图内容
        setContentView(realViewSwitcher);
        realViewSwitcher.setOnScreenSwitchListener(onScreenSwitchListener);
    }

    public void onClick(View view) {
        Toast.makeText(this, "我是Button!", Toast.LENGTH_SHORT).show();
    }
    
    private final HorizontalPager.OnScreenSwitchListener onScreenSwitchListener =
            new HorizontalPager.OnScreenSwitchListener() {
                @Override
                public void onScreenSwitched(final int screen) {
                    /**
                     * 可以对每个屏幕操作了
                     */
                    Log.d("HorizontalPager", "switched to screen: " + screen);
                    Toast.makeText(getApplicationContext(), "我是第" + screen + "张",
                            Toast.LENGTH_SHORT).show();
                }
            };

}



左右移动的核心代码:(来源 于一个开源软件)

/*
 * Modifications by Yoni Samlan; based on RealViewSwitcher, whose license is:
 *
 * Copyright (C) 2010 Marc Reichelt
 *
 * Work derived from Workspace.java of the Launcher application
 *  see http://android.git.kernel.org/?p=platform/packages/apps/Launcher.git
 *
 * 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.bbswp.viewswitcher.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Scroller;

/**
 * A view group that allows users to switch between multiple screens (layouts) in the same way as
 * the Android home screen (Launcher application).
 * <p>
 * You can add and remove views using the normal methods {@link ViewGroup#addView(View)},
 * {@link ViewGroup#removeView(View)} etc. You may want to listen for updates by calling
 * {@link HorizontalPager#setOnScreenSwitchListener(OnScreenSwitchListener)} in order to perform
 * operations once a new screen has been selected.
 *
 * Modifications from original version (ysamlan): Animate argument in setCurrentScreen and duration
 * in snapToScreen; onInterceptTouchEvent handling to support nesting a vertical Scrollview inside
 * the RealViewSwitcher; allowing snapping to a view even during an ongoing scroll; snap to
 * next/prev view on 25% scroll chan
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值