Android滑动屏幕

来源:互联网 发布:淘宝刷手退款能报警 编辑:程序博客网 时间:2024/05/22 15:40

自已学习了一下. 原理的东西咱不会讲, 会误倒别人. 只能上代码了.

用的Android Stdio 1.0.2 , (一看就是外行啊.哈哈..兴趣使然..)


MainActivity.java

package com.example.youtwo.testapp;import android.support.v4.view.PagerAdapter;import android.support.v4.view.ViewPager;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import java.util.ArrayList;import java.util.List;public class MainActivity extends ActionBarActivity {    private View view1, view2, view3;    private ViewPager viewPager;  //对应的viewPager    private List<View> viewList;//view数组    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        viewPager = (ViewPager) findViewById(R.id.viewpager);        LayoutInflater inflater=getLayoutInflater();        view1 = inflater.inflate(R.layout.layout1, null);        view2 = inflater.inflate(R.layout.layout2,null);        view3 = inflater.inflate(R.layout.layout3, null);        viewList = new ArrayList<View>();// 将要分页显示的View装入数组中        viewList.add(view1);        viewList.add(view2);        viewList.add(view3);        PagerAdapter pagerAdapter = new PagerAdapter() {            @Override            public boolean isViewFromObject(View arg0, Object arg1) {                return arg0 == arg1;            }            @Override            public int getCount() {                return viewList.size();            }            @Override            public void destroyItem(ViewGroup container, int position,                                    Object object) {                container.removeView(viewList.get(position));            }            @Override            public Object instantiateItem(ViewGroup container, int position) {                container.addView(viewList.get(position));                return viewList.get(position);            }        };        viewPager.setAdapter(pagerAdapter);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

布局文件 layout/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"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity">    <android.support.v4.view.ViewPager        android:id="@+id/viewpager"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center" /></RelativeLayout>

layout/layout1.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:background="#ffffff"    android:orientation="vertical"></LinearLayout>


layout/layout2.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:background="#ffff00"    android:orientation="vertical"></LinearLayout>

</LinearLayout>

<?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:background="#ff00ff"    android:orientation="vertical"></LinearLayout>

其实三个Layout也就颜色, 不一样. 

使用PageAdapter实现滑动.



0 0
原创粉丝点击