viewpager和两种adaper使用

viewpager和pageradapter一起使用

使用的事Android.support.v4.view.ViewPager包,现在Eclipse中已经自带了。

下面贴出代码:

MainActivity.java


package com.example.text;


import java.util.ArrayList;
import java.util.List;


import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;




public class MainActivity extends Activity {

private LinearLayout mTabBtnWeixin=null;
private LinearLayout mTabBtnFrd;
private LinearLayout mTabBtnAddress;
private LinearLayout mTabBtnSettings;

    private PagerAdapter pagerAdapter;
    private List<View> list;
    private int current=0;
    private ViewPager viewPager;
    private LayoutInflater inflater;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        inflater=LayoutInflater.from(this);
        init();
        viewPager=(ViewPager)findViewById(R.id.view);
        adapterViewpager a=new adapterViewpager(list);
        viewPager.setAdapter(a);
        viewPager.setOnPageChangeListener(new OnPageChangeListener() {      

@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
retBtn();
switch (position) {
case 0:
((ImageButton)findViewById(R.id.btn_tab_bottom_weixin)).setImageResource(R.drawable.tab_weixin_pressed);

break;
case 1:
((ImageButton)findViewById(R.id.btn_tab_bottom_friend)).setImageResource(R.drawable.tab_find_frd_pressed);

break;

case 2:
((ImageButton)findViewById(R.id.btn_tab_bottom_contact)).setImageResource(R.drawable.tab_address_pressed);

break;

case 3:
((ImageButton)findViewById(R.id.btn_tab_bottom_setting)).setImageResource(R.drawable.tab_settings_pressed);

break;

default:
break;
}

current=position;

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

}

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

}
});
        
        
    }
    //把按钮充值成原来的颜色
    public void retBtn(){
   
    ((ImageButton)findViewById(R.id.btn_tab_bottom_weixin)).setImageResource(R.drawable.tab_weixin_normal);
    ((ImageButton)findViewById(R.id.btn_tab_bottom_friend)).setImageResource(R.drawable.tab_find_frd_normal);
    ((ImageButton)findViewById(R.id.btn_tab_bottom_contact)).setImageResource(R.drawable.tab_address_normal);
    ((ImageButton)findViewById(R.id.btn_tab_bottom_setting)).setImageResource(R.drawable.tab_settings_normal);
   
    }
    
    public void init(){
   
    mTabBtnWeixin =(LinearLayout)findViewById(R.id.id_tab_bottom_weixin);
    mTabBtnFrd=(LinearLayout)findViewById(R.id.id_tab_bottom_friend);
    mTabBtnAddress=(LinearLayout)findViewById(R.id.id_tab_bottom_contact);
    mTabBtnSettings=(LinearLayout)findViewById(R.id.id_tab_bottom_setting);
    list=new ArrayList<View>();
   
    View[] view=new View[4];
    view[0]=inflater.inflate(R.layout.main_tab_01, null);
   
   
    view[1]=inflater.inflate(R.layout.main_tab_02,null);
    view[2]=inflater.inflate(R.layout.main_tab_03,null);
    view[3]=inflater.inflate(R.layout.main_tab_04,null);
   
    for(int i=0;i<4;i++){
    list.add(view[i]);
    }
   
   }


    public class adapterViewpager extends PagerAdapter{
   
    private List<View> list;
   
    public adapterViewpager(List<View> list){
    this.list=list;
    }


@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}


@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0==arg1;
}


@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView((View)object);

}

@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
View view=list.get(position);
container.addView(view);
return view;

}
   
    }
   
}

 下面是viewpager和FragmentStatePagerAdapter


FragmentStatePagerAdapter是Android.support.v13包中的,一定不要使用v4或v7

下面贴出代码

MainActivity1.java



(运行此窗口时把mainfest.xml文件中红色字体部分修改成.MainActivity1


<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity1"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>



)



package com.example.text;


import java.util.ArrayList;
import java.util.List;


import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;


public class MainActivity1 extends Activity {

private LinearLayout mTabBtnWeixin=null;
private LinearLayout mTabBtnFrd;
private LinearLayout mTabBtnAddress;
private LinearLayout mTabBtnSettings;

    private PagerAdapter pagerAdapter;
    private List<Fragment> list;
    private int current=0;
    private ViewPager viewPager;
    private FragmentManager fm;
    
    public void init(){
    mTabBtnWeixin =(LinearLayout)findViewById(R.id.id_tab_bottom_weixin);
    mTabBtnFrd=(LinearLayout)findViewById(R.id.id_tab_bottom_friend);
    mTabBtnAddress=(LinearLayout)findViewById(R.id.id_tab_bottom_contact);
    mTabBtnSettings=(LinearLayout)findViewById(R.id.id_tab_bottom_setting);
    list=new ArrayList<Fragment>();
    MainTab01 tab1=new MainTab01();
    MainTab02 tab2=new MainTab02();
    MainTab03 tab3=new MainTab03();
    MainTab04 tab4=new MainTab04();
    list.add(tab1);
    list.add(tab2);
    list.add(tab3);
    list.add(tab4);
   
    }


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
viewPager=(ViewPager)findViewById(R.id.view);
fm=getFragmentManager();
adapterDema a=new adapterDema(fm);
viewPager.setAdapter(a);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
//把图片换成原始颜色
((ImageButton)findViewById(R.id.btn_tab_bottom_weixin)).setImageResource(R.drawable.tab_weixin_normal);
((ImageButton)findViewById(R.id.btn_tab_bottom_friend)).setImageResource(R.drawable.tab_find_frd_normal);
((ImageButton)findViewById(R.id.btn_tab_bottom_contact)).setImageResource(R.drawable.tab_address_normal);
((ImageButton)findViewById(R.id.btn_tab_bottom_setting)).setImageResource(R.drawable.tab_settings_normal);
System.out.println("---"+arg0);
switch (arg0) {
case 0:
((ImageButton)findViewById(R.id.btn_tab_bottom_weixin)).setImageResource(R.drawable.tab_weixin_pressed);


break;
case 1:
((ImageButton)findViewById(R.id.btn_tab_bottom_friend)).setImageResource(R.drawable.tab_find_frd_pressed);


break;
case 2:
((ImageButton)findViewById(R.id.btn_tab_bottom_contact)).setImageResource(R.drawable.tab_address_pressed);


break;
case 3:
((ImageButton)findViewById(R.id.btn_tab_bottom_setting)).setImageResource(R.drawable.tab_settings_pressed);


break;


default:
break;
}
            current=arg0;


}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

}

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

}
});


}


public class adapterDema extends FragmentStatePagerAdapter{


public adapterDema(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}


@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}


@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}


@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}


@Override
public void destroyItem(View container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager)container).removeView((View)object);
}



}




}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值