android 很多应用中用到的 listView + viewPager
比如网易啊啥的, 有些界面的 listview 第一行是可以 左右滑动的 viewpager,当滑动下面listView的时候 会一起滑动上去
工程目录:
效果图:
1. MyViewPager.java
public class MyViewPager extends ViewPager { int mLastMotionY; int mLastMotionX; public MyViewPager(Context context) { super(context); } public MyViewPager(Context context, AttributeSet attrs) { super(context, attrs); } //拦截 TouchEvent @Override public boolean onInterceptTouchEvent(MotionEvent arg0) { // TODO Auto-generated method stub return super.onInterceptTouchEvent(arg0); } //处理 TouchEvent @Override public boolean onTouchEvent(MotionEvent arg0) { // TODO Auto-generated method stub return super.onTouchEvent(arg0); } //因为这个执行的顺序是 父布局先得到 action_down的事件 /** * onInterceptTouchEvent(MotionEvent ev)方法,这个方法只有ViewGroup类有 * 如LinearLayout,RelativeLayout等 可以包含子View的容器的 * * 用来分发 TouchEvent * 此方法 返回true 就交给本 View的 onTouchEvent处理 * 此方法 返回false 就交给本View的 onInterceptTouchEvent 处理 */ @Override public boolean dispatchTouchEvent(MotionEvent ev) { //让父类不拦截触摸事件就可以了。 this.getParent().requestDisallowInterceptTouchEvent(true); return super.dispatchTouchEvent(ev); } }
2. MainActivity.java
public class MainActivity extends Activity { private ListView listView; private MyAdapter MyAdapter; private List<View> list_views =new ArrayList<View>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.newslist_main); //造1个 假数据 for (int i = 0; i <4; i++) { View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.topimage, null); if(i == 0){ v.setBackgroundResource(R.drawable.a1); }else if( i ==1){ v.setBackgroundResource(R.drawable.a2); }else if(i ==2){ v.setBackgroundResource(R.drawable.a3); }else if( i ==3){ v.setBackgroundResource(R.drawable.a4); } list_views.add(v); } listView = (ListView) findViewById(R.id.newslist); MyAdapter = new MyAdapter(this,list_views); listView.setAdapter(MyAdapter); } }
3.MyAdapter
public class MyAdapter extends BaseAdapter { private View topViewPager; //顶部View private ViewPager viewPager; //顶部View 中的ViewPager private MyPagerAdapter pageAdaper; //Viewpager adapter private List<View> images; //上方viewpager的图片 private Context context; public MyAdapter(Context context,List<View> images) { this.context = context; this.images = images; pageAdaper = new MyPagerAdapter(); } //ListView size @Override public int getCount() { return 20; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(position == 0){ //第一行 viewpager的layout return setTopView(convertView); }else { //其他listView View view = LayoutInflater.from(context).inflate(R.layout.listitem, null); return view; } } private View setTopView(View convertView){ if(topViewPager==null){ //加载顶部View topViewPager = LayoutInflater.from(context).inflate(R.layout.index_photos, null); //设置Viewpager viewPager = (ViewPager)topViewPager.findViewById(R.id.topViewPager); //塞入adapter viewPager.setAdapter(pageAdaper); //默认选中第一个 viewPager.setCurrentItem(0); viewPager.setOnPageChangeListener(new MyPagerChangeListener()); } return topViewPager; } private class MyPagerChangeListener implements OnPageChangeListener{ @Override public void onPageSelected(int position) { Toast.makeText(context, "position="+ position, 1).show(); } @Override public void onPageScrollStateChanged(int arg0) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } } /** * ViewPager adapter */ public class MyPagerAdapter extends PagerAdapter { @Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0 == arg1; } // @Override // public int getItemPosition(Object object) { // // return super.getItemPosition(object); // return POSITION_NONE; // } @Override public int getCount() { return images.size(); } @Override public Parcelable saveState() { return null; } @Override public void destroyItem(View container, int position, Object object) { ((ViewPager) container).removeView(images.get(position)); } // viewpager 每个页卡的实例化 @Override public Object instantiateItem(View container, int position) { ((ViewPager) container).addView(images.get(position)); return images.get(position); } } }
adapter 把 pageradapter 和 listadapter的组合
xml其实没有什么贴的了。, 就是1个 listview ,1个 viewpager 就完了(当然这个是自己继承后的 那个View),
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <com.example.custome.MyViewPager android:id="@+id/topViewPager" android:layout_width="match_parent" android:layout_height="300dip" android:background="#ffffff" android:layout_gravity="top" /> </LinearLayout>
另外1个就是一个 listview了。
好了。