pageView 实现多数据循环显示.

                      最近项目需要用到pageView . 就是显示一些个人信息,比较多人,而且要有滑动效果,并且要实现滑到最后一个人的信息,下一个就要显示下一个人的信息.代码不多.

pageView 实现多数据循环显示.



就一个类:

package com.qiang.test;

import java.util.ArrayList;


import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;


public class MainActivity extends Activity {


 ViewPager viewPager ;
 
 ArrayList<LinearLayout> viewList ;
 
 ArrayList<String> arrayList ;
 

     @Override
    protected void onCreate(Bundle savedInstanceState) {
    	// TODO Auto-generated method stub
    	super.onCreate(savedInstanceState);
    	
    	setContentView(R.layout.activity_main);
    	
     ViewPager viewPager  = (ViewPager) findViewById(R.id.pageView);
     viewList  = new ArrayList<LinearLayout>();
   //设置20条数据
     arrayList = new ArrayList<String>();
     for(int i = 0;i<20;i++){
  	   
  	   arrayList.add("data"+i);
  	   
     }  
     
     
     for(int i=0;i<5;i++){
    	 //添加的List里面
    	  LinearLayout  view = (LinearLayout) getLayoutInflater().inflate(R.layout.scan_list_item, null);
    	 viewList.add(view);
     }
     
       
     PagerAdapter adapter ;
    
       viewPager.setAdapter( adapter=new MyPageAdapter(this));
       
     
 
		
	}
    
     
     public class MyPageAdapter extends PagerAdapter{
    	 
    	 Context context;
    	 MyPageAdapter(Activity context){
    		 
    		 this.context = context;
    		 
    	 }

    	

       //
			@Override
    		public void destroyItem(ViewGroup container, int position, Object object) {
    			// System.out.println("移除了哪一个"+position);
    			container.removeView((View) object);
    		}
    	 
    	
			//实现循环 ,pageView在这里控制数量的显示
		@Override
		public int getCount() {
		
	  //假如数据不为空,返回空,不是空,返回无限大
	   return arrayList==null?0:Integer.MAX_VALUE;
		}

		
	
	
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		LinearLayout view;
		//通过计算,因为要在viewList循环显示,要利用viewlist的view
	
		 
		if(position>viewList.size()-1){
		  //取余
		   
			
			 view =  viewList.get(position%viewList.size());
	  }else{
		  
		  view= viewList.get(position);
		  
	  }
	  //打印下位子
	
		
		//因为不想用listView,但也想不要findViewById 查找子节点,所以就此方法
	    for (int i = 0; i <view.getChildCount(); i++) {
	    	   
	    	RelativeLayout relativeLayout = (RelativeLayout) view.getChildAt(i);
	    	   
	    	   for (int j = 0; j <relativeLayout.getChildCount(); j++) {
	    		   
	    		TextView textView1 =   (TextView) relativeLayout.getChildAt(j);
	    	
	    		int dataIndex = position%arrayList.size();
	    		
	    		textView1.setText("position:"+position+", " +"data:"+arrayList.get(dataIndex));
	    		TextView textView2 =   (TextView) relativeLayout.getChildAt(j=+1);
	    	
	    		textView2.setText(i+":" +j);
				
			}
	
	    }
	    
	    container.addView(view);
	    
	    return  view;
	    
		
	}
		
		
		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}
    	 
     }
     
 	public void destroyItem(ViewGroup container, int position, Object object) {
		// System.out.println("移除了哪一个"+position);
		
 		View view = (View) object;
 		
 		container.removeView((View) object);


	}
          
    	
      
    	
    }
	
	


布局文件 总布局文件 demo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    
    <android.support.v4.view.ViewPager    
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/help_viewPage"
        
        />
    
   
    
   
    

</LinearLayout>
布局文件:scan_list_item.xml   控制pageView显示的内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical"
    >

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

      

        <TextView
            android:id="@+id/scan_title_name"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="123" />

        <TextView
            android:id="@+id/scan_peson_message"
            android:layout_width="180dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@id/scan_title_name"
            android:gravity="left"
            android:text="123" />
    </RelativeLayout>
    
    
        <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical" >


        <TextView
            android:id="@+id/scan_title_name"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="123" />

        <TextView
            android:id="@+id/scan_peson_message"
            android:layout_width="180dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@id/scan_title_name"
            android:gravity="left"
            android:text="123" />
    </RelativeLayout>
    
        
            <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        
        <TextView
            android:id="@+id/scan_title_name"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="123" />

        <TextView
            android:id="@+id/scan_peson_message"
            android:layout_width="180dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@id/scan_title_name"
            android:gravity="left"
            android:text="123" />
    </RelativeLayout>
    
            
                <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical" >


        <TextView
            android:id="@+id/scan_title_name"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="123" />

        <TextView
            android:id="@+id/scan_peson_message"
            android:layout_width="180dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@id/scan_title_name"
            android:gravity="left"
            android:text="123" />
    </RelativeLayout>

</LinearLayout>


   

你可能感兴趣的:(pageView)