ViewPager的界面滑动(初学试用)

发布时间:2020-07-17 18:18:07 作者:迷途不迷糊
来源:网络 阅读:406


import java.util.ArrayList;

import java.util.List;

import android.app.Activity;

import android.os.Bundle;

import android.support.v4.view.PagerAdapter;

import android.support.v4.view.ViewPager;

import android.support.v4.view.ViewPager.OnPageChangeListener;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;


public class MainActivity extends Activity {


private List<View> listView = new ArrayList<View>();

private ViewPager viewPager;

private Button title1;

private Button title2;

private Button title3;

private Button title4;

private Button title5;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

initAdapter();

}

                //加载控件

private void initView() {

title1 = (Button) findViewById(R.id.bt_title1);

title2 = (Button) findViewById(R.id.bt_title2);

title3 = (Button) findViewById(R.id.bt_title3);

title4 = (Button) findViewById(R.id.bt_title4);

title5 = (Button) findViewById(R.id.bt_title5);


}


public void clickTitle1(View v) {

                //修改未选中按钮背景颜色

title2.setBackgroundResource(R.drawable.kfa);

title3.setBackgroundResource(R.drawable.kfa);

title4.setBackgroundResource(R.drawable.kfa);

title5.setBackgroundResource(R.drawable.kfa);

                //修改选中按钮背景颜色

title1.setBackgroundResource(R.drawable.fasd);

                //跳转到对应界面

viewPager.setCurrentItem(0);

}

                //Button点击事件

public void clickTitle2(View v) {

title1.setBackgroundResource(R.drawable.kfa);

title3.setBackgroundResource(R.drawable.kfa);

title4.setBackgroundResource(R.drawable.kfa);

title5.setBackgroundResource(R.drawable.kfa);

title2.setBackgroundResource(R.drawable.fasd);

viewPager.setCurrentItem(1);

}

                //Button点击事件

public void clickTitle3(View v) {

title1.setBackgroundResource(R.drawable.kfa);

title2.setBackgroundResource(R.drawable.kfa);

title4.setBackgroundResource(R.drawable.kfa);

title5.setBackgroundResource(R.drawable.kfa);

title3.setBackgroundResource(R.drawable.fasd);

viewPager.setCurrentItem(2);

}

                //Button点击事件

public void clickTitle4(View v) {

title1.setBackgroundResource(R.drawable.kfa);

title2.setBackgroundResource(R.drawable.kfa);

title3.setBackgroundResource(R.drawable.kfa);

title5.setBackgroundResource(R.drawable.kfa);

title4.setBackgroundResource(R.drawable.fasd);

viewPager.setCurrentItem(3);

}

                //Button点击事件

public void clickTitle5(View v) {

title1.setBackgroundResource(R.drawable.kfa);

title2.setBackgroundResource(R.drawable.kfa);

title3.setBackgroundResource(R.drawable.kfa);

title4.setBackgroundResource(R.drawable.kfa);

title5.setBackgroundResource(R.drawable.fasd);

viewPager.setCurrentItem(4);


}

                

private void initAdapter() {

// 获取页面

LayoutInflater inflater = LayoutInflater.from(this);

View v1 = View.inflate(getApplicationContext(),

R.layout.activity_title1, null);

View v2 = View.inflate(getApplicationContext(),

R.layout.activity_title2, null);

View v3 = View.inflate(getApplicationContext(),

R.layout.activity_title3, null);

View v4 = View.inflate(getApplicationContext(),

R.layout.activity_title4, null);

View v5 = View.inflate(getApplicationContext(),

R.layout.activity_title5, null);

// 将页面添加到集合中

listView.add(v1);

listView.add(v2);

listView.add(v3);

listView.add(v4);

listView.add(v5);

PagerAdapter pagerAdapter = new PagerAdapter() {

// PagerAdapter只缓存2个要显示的tab,如果滑动的tab超出了缓存的范围,就会调用这个方法,将tab销毁

@Override

public void destroyItem(ViewGroup view, int position, Object object) {

view.removeView(listView.get(position));


}


// 当要显示的tab可以进行缓存的时候,会调用这个方法进行显示图片的初始化,我们将要显示的tab加入到ViewGroup中,然后作为返回值返回即可

@Override

public Object instantiateItem(ViewGroup view, int position) {

view.addView(listView.get(position));

return listView.get(position);

}


// 获取要滑动的控件的数量,在这里我们以tab为例,那么这里就应该是展示tab数量

@Override

public int getCount() {


return listView.size();

}


// 来判断显示的是否是同一tab,这里我们将两个参数相比较返回即可

@Override

public boolean isViewFromObject(View arg0, Object arg1) {


return arg0 == arg1;

}

};

viewPager = (ViewPager) findViewById(R.id.vp_body);

viewPager.setAdapter(pagerAdapter);

/**

* 标题栏响应界面滑动事件

*/

viewPager.setOnPageChangeListener(new OnPageChangeListener() {


@Override

public void onPageSelected(int arg0) {

title1.setBackgroundResource(R.drawable.kfa);

title2.setBackgroundResource(R.drawable.kfa);

title3.setBackgroundResource(R.drawable.kfa);

title4.setBackgroundResource(R.drawable.kfa);

title5.setBackgroundResource(R.drawable.kfa);

switch (viewPager.getCurrentItem()) {

case 0:

title1.setBackgroundResource(R.drawable.fasd);

break;


case 1:

title2.setBackgroundResource(R.drawable.fasd);

break;


case 2:

title3.setBackgroundResource(R.drawable.fasd);

break;


case 3:

title4.setBackgroundResource(R.drawable.fasd);

break;


case 4:

title5.setBackgroundResource(R.drawable.fasd);

break;

}

}


@Override

public void onPageScrolled(int arg0, float arg1, int arg2) {


}


@Override

public void onPageScrollStateChanged(int arg0) {


}

});

}


@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.main, menu);

return true;

}


}


推荐阅读:
  1. viewpager实现广告滑动及文字滑动和下方 点的选中状态改变
  2. ViewPager左右滑动

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

摘录 age pager

上一篇:iptable详解

下一篇:Swift存储属性

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》