安卓学习ViewPager配合ListView实现功能

来源:互联网 发布:windows只能进安全模式 编辑:程序博客网 时间:2024/06/07 11:05
实例化控件private ViewPager mViewPager;private PagerAdapter mPagerAdapter;private List<Map<String,Object>> mData = new ArrayList<Map<String, Object>>();在主方法中 调用mData = getData();   getData()方法。例子 这是我写的一个电影的数据    public List<Map<String,Object>> getData() {        List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();        Map<String,Object> map = new HashMap<String, Object>();        map.put("info","变形金刚5:最后的骑士");        map.put("title","导演:迈克尔°贝");        map.put("titlee","主演:马克°沃尔伯格");        map.put("image",R.drawable.bianwu);        list.add(map);        map = new HashMap<String, Object>();        map.put("info","逆时营救");        map.put("title","导演:尹宏程");        map.put("titlee","主演:杨幂 霍建华 金士杰");        map.put("image",R.drawable.yishi);        list.add(map);        map = new HashMap<String, Object>();        map.put("info","神偷奶爸3");        map.put("title","导演:克里斯。雷纳德");        map.put("titlee","主演:史蒂夫。卡瑞尔");        map.put("image",R.drawable.shentou);        list.add(map);        map = new HashMap<String, Object>();        map.put("info","绝世高手");        map.put("title","导演:卢正雨");        map.put("titlee","主演:卢正雨 郭采洁 范伟");        map.put("image",R.drawable.jueshi);        list.add(map);        map = new HashMap<String, Object>();        map.put("info","悟空传");        map.put("title","导演:郭子健");        map.put("titlee","主演:彭于晏 闫妮 欧豪");        map.put("image",R.drawable.wukongzhuan);        list.add(map);        map = new HashMap<String, Object>();        map.put("info","京城81号2");        map.put("title","导演:钱人豪");        map.put("titlee","主演:张智霖 梅婷 钟欣桐");        map.put("image",R.drawable.jingcheng);        list.add(map);        map = new HashMap<String, Object>();        map.put("info","明月几时有");        map.put("title","导演:许鞍华");        map.put("titlee","主演:周迅 彭于晏 霍建华");        map.put("image",R.drawable.ming);        list.add(map);        map = new HashMap<String, Object>();        map.put("info","冈仁波齐");        map.put("title","导演:张扬");        map.put("titlee","主演:杨培 尼玛扎堆 斯朗卓嘎");        map.put("image",R.drawable.gang);        list.add(map);        return list;    }    数据的适配器public class ViewHolder{        public TextView info;        public TextView title;        public TextView titlee;        public Button btn;        public ImageView image;    }public class MyAdapter extends BaseAdapter {        private LayoutInflater inflater;        public MyAdapter(Context context){            this.inflater = LayoutInflater.from(context);        }        @Override        public int getCount() {            return mData.size();        }        @Override        public Object getItem(int i) {            return null;        }        @Override        public long getItemId(int i) {            return 0;        }        @Override        public View getView(int i, View view, ViewGroup viewGroup) {            ViewHolder holder = new ViewHolder();            if (view == null){                holder = new ViewHolder();                view = inflater.inflate(R.layout.list_view,null);                holder.image = (ImageView) view.findViewById(R.id.img);                holder.btn = (Button) view.findViewById(R.id.btn);                holder.info = (TextView) view.findViewById(R.id.info);                holder.title = (TextView) view.findViewById(R.id.title);                holder.titlee = (TextView) view.findViewById(R.id.titlee);                view.setTag(holder);            }else {                holder = (ViewHolder) view.getTag();            }            holder.info.setText((String) mData.get(i).get("info"));            holder.title.setText((String) mData.get(i).get("title"));            holder.titlee.setText((String) mData.get(i).get("titlee"));            holder.image.setBackgroundResource((Integer) mData.get(i).get("image"));            holder.btn.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View view) {                }            });            return view;        }在全局中添加点击事件implements View.OnClickListener在主方法中 调用这些方法        initview();                        initEvent();                        initViewPage();//实现滑动事件  水平低  没太研究明白private void initEvent() {        imageView1.setOnClickListener(this);        imageView2.setOnClickListener(this);        imageView3.setOnClickListener(this);        imageView4.setOnClickListener(this);        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {            @Override            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {            }            @Override            public void onPageSelected(int position) {                int currentItem = mViewPager.getCurrentItem();                switch (currentItem) {                    case 0:                        resetImg();                        break;                    case 1:                        resetImg();                        break;                    case 2:                        resetImg();                        break;                    case 3:                        resetImg();                        break;                    default:                        break;                }            }            @Override            public void onPageScrollStateChanged(int state) {            }        });    }//加载布局private void initViewPage() {        LayoutInflater mLayoutInflater = LayoutInflater.from(this);        View tab01 = mLayoutInflater.inflate(R.layout.tab01,null);        View tab02 = mLayoutInflater.inflate(R.layout.tab02,null);        View tab03 = mLayoutInflater.inflate(R.layout.tab03,null);        View tab04 = mLayoutInflater.inflate(R.layout.tab04,null);        ListView listview1 = (ListView) tab01.findViewById(R.id.listview);        MyAdapter adapter = new MyAdapter(this);        listview1.setAdapter(adapter);        mViews.add(tab01);        mViews.add(tab02);        mViews.add(tab03);        mViews.add(tab04);        mPagerAdapter = new PagerAdapter() {            @Override            public void destroyItem(ViewGroup container, int position, Object object) {                container.removeView(mViews.get(position));            }            @Override            public Object instantiateItem(ViewGroup container, int position) {                View view = mViews.get(position);                container.addView(view);                return view;            }            @Override            public int getCount() {                return mViews.size();            }            @Override            public boolean isViewFromObject(View view, Object object) {                return view == object;            }        };        mViewPager.setAdapter(mPagerAdapter);    }加载IDprivate void initview() {        mViewPager = (ViewPager) findViewById(R.id.viewpage);        imageView1 = (ImageView) findViewById(R.id.img1);        imageView2 = (ImageView) findViewById(R.id.img2);        imageView3 = (ImageView) findViewById(R.id.img3);        imageView4 = (ImageView) findViewById(R.id.img4);        tv1 = (TextView) findViewById(R.id.tv1);    }实在不会写这个 对付看代码吧 我是小菜鸟 附布局<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:background="#FFFFFF"    tools:context="com.example.administrator.movie.MainActivity">    <LinearLayout        android:layout_gravity="center"        android:layout_width="wrap_content"        android:layout_height="45dp">        <TextView            android:id="@+id/tv1"            android:gravity="center"            android:text="电影"            android:textSize="20dp"            android:layout_width="fill_parent"            android:layout_height="wrap_content" />    </LinearLayout>    <android.support.v4.view.ViewPager        android:id="@+id/viewpage"        android:layout_width="wrap_content"        android:layout_height="0dp"        android:layout_weight="1">    </android.support.v4.view.ViewPager>    <include layout="@layout/down" /></LinearLayout><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="60dp">    <ImageView        android:scaleType="fitStart"        android:src="@drawable/b1"        android:id="@+id/img1"        android:layout_width="270px"        android:layout_height="wrap_content" />    <ImageView        android:scaleType="fitStart"        android:src="@drawable/a2"        android:id="@+id/img2"        android:layout_width="270px"        android:layout_height="wrap_content" />    <ImageView        android:scaleType="fitStart"        android:src="@drawable/a3"        android:id="@+id/img3"        android:layout_width="270px"        android:layout_height="wrap_content" />    <ImageView        android:scaleType="fitStart"        android:src="@drawable/a4"        android:id="@+id/img4"        android:layout_width="270px"        android:layout_height="wrap_content" /></LinearLayout>0<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:orientation="horizontal"    android:background="#FFFFFF"    android:layout_height="match_parent">    <ListView        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:id="@+id/listview"/></LinearLayout><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:orientation="horizontal"    android:layout_height="match_parent">    <ImageView        android:layout_marginLeft="20dp"        android:id="@+id/img"        android:layout_width="70dp"        android:layout_height="100dp" />    <LinearLayout        android:orientation="vertical"        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <TextView            android:id="@+id/info"            android:layout_marginLeft="20dp"            android:textSize="30dp"            android:layout_width="150dp"            android:layout_height="wrap_content" />        <TextView            android:id="@+id/title"            android:layout_marginLeft="20dp"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <TextView            android:id="@+id/titlee"            android:layout_marginLeft="20dp"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />    </LinearLayout>    <RelativeLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <Button            android:layout_alignParentRight="true"            android:id="@+id/btn"            android:background="@drawable/goubiao"            android:layout_width="100dp"            android:layout_height="70dp" />    </RelativeLayout></LinearLayout>实在不会写这个 哎  主要是记录下自己的印象 
原创粉丝点击