标签:des android style c class blog
package com.example.viewpage; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.Parcelable; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.DisplayMetrics; import android.util.Log; import android.view.View; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; @SuppressLint({ "ParserError", "WrongViewCast", "ResourceAsColor" }) public class MainActivity extends Activity { // VIewPaer控件 private ViewPager vp; // VIewPager的数据源 private MyPagerAdapter myAdapter; // 为VIewPager保存VIew界面的集合 private List<View> vessel = new ArrayList<View>(); private Map<String,List<UserInfo>> listData = new HashMap<String,List<UserInfo>>(); private List<String> dataListKey = new ArrayList<String>(); // 代表第一个和第2个界面 private View view1, view2; // 动画图片 private ImageView cursor; // 第2个窗体的ListVIew控件 private ListView list,list1; private int offset = 0;// 动画图片偏移量 private int currIndex = 0;// 当前页卡编号 private int bmpW;// 动画图片宽度 // ListView的数据集 private static List<UserInfo> userInfos; // listView右边的导航选项卡 private MySideBar myView; // ListView数据源 private static MyUserInfoAdapter adapter; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); InitImageView(); init(); } /** * 初始化控件和绑定事件 */ public void init() { dataListKey.add("1"); dataListKey.add("2"); dataListKey.add("3"); dataListKey.add("4"); dataListKey.add("5"); dataListKey.add("6"); dataListKey.add("7"); dataListKey.add("8"); for (int i = 0; i < dataListKey.size(); i++) { view1 = this.getLayoutInflater().inflate(R.layout.layout1, null); list1 = (ListView) view1.findViewById(R.id.LV); userInfos =getUserInfos(); adapter = new MyUserInfoAdapter(MainActivity.this, userInfos); list1.setAdapter(adapter); vessel.add(view1); } Log.e("viewpager", vessel.size()+""); vp = (ViewPager) findViewById(R.id.viewpagerLayout); myAdapter = new MyPagerAdapter(); vp.setAdapter(myAdapter); // 设置起始默认的值为0 vp.setCurrentItem(0); vp.setOnPageChangeListener(new MyOnPageChangeListener()); } /* * 初始化滑动条 */ private void InitImageView() { bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.a).getWidth();// 获取图片宽度 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int screenW = dm.widthPixels;// 获取分辨率宽度 System.out.println("bmpw:" + bmpW + "screenW:" + screenW); offset = (screenW / 3 - bmpW) / 2;// 计算偏移量 Matrix matrix = new Matrix(); matrix.postTranslate(offset, 0); System.out.println("offset:" + offset); } // 适配器 class MyPagerAdapter extends PagerAdapter { public void destroyItem(View arg0, int arg1, Object arg2) { ((ViewPager) arg0).removeView(vessel.get(arg1)); } public void finishUpdate(View arg0) { } public int getCount() { return vessel.size(); } public Object instantiateItem(View arg0, int arg1) { ((ViewPager) arg0).addView(vessel.get(arg1), 0); return vessel.get(arg1); } public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == (arg1); } public void restoreState(Parcelable arg0, ClassLoader arg1) { } public Parcelable saveState() { return null; } @Override public void startUpdate(View arg0) { } } /** * 页卡切换监听 */ public class MyOnPageChangeListener implements OnPageChangeListener { int one = offset * 2 + bmpW;// 页卡1 -> 页卡2 偏移量 @Override public void onPageSelected(int arg0) { Animation animation = null; switch (arg0) { case 0: if (currIndex == 1) { animation = new TranslateAnimation(one, 0, 0, 0); }/* * else if (currIndex == 2) { animation = new * TranslateAnimation(two, 0, 0, 0); } */ break; case 1: if (currIndex == 0) { animation = new TranslateAnimation(offset, one, 0, 0); } /* * else if (currIndex == 2) { animation = new * TranslateAnimation(two, one, 0, 0); } */ break; /* * case 2: if (currIndex == 0) { animation = new * TranslateAnimation(offset, two, 0, 0); } else if (currIndex == 1) * { animation = new TranslateAnimation(one, two, 0, 0); } break; */ } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } } /** * 初始化加载listVIew所需要的数据 并进行排序和匹配 */ private List<UserInfo> getUserInfos() { List<UserInfo> userInfos = new ArrayList<UserInfo>(); userInfos.add(new UserInfo("唐僧", "18765432345")); userInfos.add(new UserInfo("猪师弟", "18765432345")); userInfos.add(new UserInfo("阿呆", "18765432345")); userInfos.add(new UserInfo("8899", "18765432345")); userInfos.add(new UserInfo("孙悟空", "18765432345")); userInfos.add(new UserInfo("孙悟空", "18765432345")); userInfos.add(new UserInfo("孙悟空", "18765432345")); userInfos.add(new UserInfo("孙悟空", "18765432345")); userInfos.add(new UserInfo("孙悟空", "18765432345")); userInfos.add(new UserInfo("孙悟空", "18765432345")); return userInfos; } }
<LinearLayout 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:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/viewpagerLayout" android:layout_width="wrap_content" android:layout_height="318dp" android:background="@drawable/default_bg" android:layout_weight="0.01" > </android.support.v4.view.ViewPager> </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="#88ffffff" android:orientation="vertical" > <ListView android:id="@+id/LV" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
viewpager动态加载listView数据,布布扣,bubuko.com
标签:des android style c class blog
原文地址:http://www.cnblogs.com/linximeng/p/3751893.html