ViewPager的界面滑动(初学试用)
import java.util.ArrayList;
十年的东洲网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。网络营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整东洲建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“东洲网站设计”,“东洲网站推广”以来,每个客户项目都认真落实执行。
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
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;
}
}
网页名称:ViewPager的界面滑动(初学试用)
网站网址:http://scyanting.com/article/gghope.html