android 通过向viewpage中添加listview来完成滑动效果(类似于qq滑动界面)

偷偷色一把

偷偷色一把

2016-02-19 10:08

下面图老师小编跟大家分享一个简单易学的android 通过向viewpage中添加listview来完成滑动效果(类似于qq滑动界面)教程,get新技能是需要行动的,喜欢的朋友赶紧收藏起来学习下吧!

文件名:page.xml
代码如下:

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" /

/RelativeLayout

文件名:listviewitem.xml
代码如下:

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"

     TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="hello"
        android:textColor="#00ff00"
      /
    ImageView
        android:id="@+id/imageView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        /
/LinearLayout

以上的第二个布局文件 只提供给simpleAdapter

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

代码如下:

package com.example.learnpager2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
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.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;

public class PagerActivity extends Activity {
    ViewPager viewPager;
    ArrayListView pagesArrayList;// 添加listview 然后传给MyPagerAdapter 充当媒介
    String[] strs;
    String[] strs2;
    ListView listView;
    Button button1;
    Button button2;
    Button button3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pager);
        iniParams();
        iniViews();

    }
    private void iniParams(){
        pagesArrayList=new ArrayListView();
        strs=new String[]{"a","b","c","a","b","c","a","b","c"};
        strs2=new String[]{"1","2","3","1","2","3","1","2","3"};

    }
    private void iniViews(){
        viewPager=(ViewPager)findViewById(R.id.viewPager);
        //第一个页面的viewpager第1个page里的listview
        LayoutInflater layoutInflater=getLayoutInflater();

        listView=(ListView)
                (layoutInflater.inflate(R.layout.page,null).findViewById(R.id.listview));
        ArrayAdapterString arrrayAdapter=new ArrayAdapterString(this,android.R.layout.simple_list_item_1,strs);
        listView.setAdapter(arrrayAdapter);
        pagesArrayList.add(listView);

        //第个页面的viewpager第2个page里的listview
        listView=(ListView)
                (layoutInflater.inflate(R.layout.page,null).findViewById(R.id.listview));
        ArrayAdapterString arrrayAdapter2=new ArrayAdapterString(this,android.R.layout.simple_list_item_1,strs2);
        listView.setAdapter(arrrayAdapter2);
        pagesArrayList.add(listView);

        //第三个面的viewpager第3个page里的listview
        SimpleAdapter simpleAdapter3=new SimpleAdapter(this,getDatasForListView(),
                R.layout.listviewitem,new String[]{"title","image"},new int[]{R.id.textView,R.id.imageView});
        listView=(ListView)
                (layoutInflater.inflate(R.layout.page, null).findViewById(R.id.listview));
        listView.setAdapter(simpleAdapter3);
        pagesArrayList.add(listView);

        viewPager.setAdapter(new MyPagerAdapter(pagesArrayList));
        viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
        viewPager.setCurrentItem(0);

        button1=(Button)findViewById(R.id.button1);
        button2=(Button)findViewById(R.id.button2);
        button3=(Button)findViewById(R.id.button3);
    }
    public ListMapString, Object getDatasForListView(){
        Log.e("3", "");
        ListMapString, Object listMaps=new ArrayListMapString,Object();

        String[] strings=new String[]{"图片1","图片2","图片3"};
        int[] images=new int[]{R.drawable.p1,R.drawable.p1,R.drawable.p1};

        for(int i=0;istrings.length;i++){
            listMaps.add(ListViewItemFactory.generate(new Object[]{strings[i],images[0]}));
        }
        return listMaps;
    }
    static class ListViewItemFactory{
        static MapString,Object generate(Object[] obj){
            MapString,Object map=new HashMapString, Object();
            map.put("title", obj[0]);
            map.put("image",obj[1]);
            return map;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.pager, menu);
        return true;
    }
    public class MyPagerAdapter extends PagerAdapter {
        public ListView mListViews;

        public MyPagerAdapter(ListView mListViews) {
            this.mListViews = mListViews;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {

            Log.d("destroyItem", ""+arg0+" "+arg1);
            ((ViewPager) arg0).removeView(mListViews.get(arg1));
        }
        @Override
        public int getCount() {
            return mListViews.size();
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            Log.d("instantiateItem", ""+arg0+" "+arg1);
            try {
                if(mListViews.get(arg1).getParent()==null)
                    ((ViewPager) arg0).addView(mListViews.get(arg1), 0); 
                else{
                    //  很难理解新添加进来的view会自动绑定一个父类,由于一个儿子view不能与两个父类相关,所以得解绑
                    //不这样做否则会产生 viewpager java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
// 还有一种方法是viewPager.setOffscreenPageLimit(3); 这种方法不用判断parent 是不是已经存在,但多余的listview不能被destroy
                    ((ViewGroup)mListViews.get(arg1).getParent()).removeView(mListViews.get(arg1));

                    ((ViewPager) arg0).addView(mListViews.get(arg1), 0);
                }
            } catch (Exception e) { 
                // TODO Auto-generated catch block 
                Log.d("parent=", ""+mListViews.get(arg1).getParent());
                e.printStackTrace(); 
            } 
            return mListViews.get(arg1);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == (arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
        }
    }
    class MyOnPageChangeListener implements OnPageChangeListener{

        @Override
        public   void onPageScrollStateChanged (int state){
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // TODO Auto-generated method stub

(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)

        }
        Color preColor;
        @Override
        public void onPageSelected(int position) {
            // TODO Auto-generated method stub
            Log.d("page",  "pos="+position);
            switch(position)
            {
            case 0: //button1.setBackgroundColor(0x00FF00);break;
            case 1:// button1.setBackgroundColor(0xFF0000);break;
            case 2:
            }

        }

    }
}

展开更多 50%)
分享

猜你喜欢

android 通过向viewpage中添加listview来完成滑动效果(类似于qq滑动界面)

编程语言 网络编程
android 通过向viewpage中添加listview来完成滑动效果(类似于qq滑动界面)

AlternatingItemTemplate类似于 ItemTemplate 元素

Web开发
AlternatingItemTemplate类似于 ItemTemplate 元素

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

类似于iis浏览的功能

ASP
类似于iis浏览的功能

android中实现指针滑动的动态效果方法

编程语言 网络编程
android中实现指针滑动的动态效果方法

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

类似于FlashGet的悬浮框的制作

C语言教程 C语言函数
类似于FlashGet的悬浮框的制作

AjaxUI:滑动条

Web开发
AjaxUI:滑动条

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

Win10防火墙在哪?

Win10防火墙在哪?

ajax回调函数参数传递正确方法

ajax回调函数参数传递正确方法
下拉加载更多内容 ↓