Android 高仿QQ 界面滑动效果

点击或者滑动切换画面,用ViewPager实现,

首先是布局文件:


	
	    
	        
		    
		    
		
	    
		      
	                  
	              
	            
	            
	           
		
	
	
	   

代码:

public class MainActivity extends ActivityGroup {
	private static final String TRENDS="动态";
	private static final String GROUP="群组";
	private static final String BUDDY="好友";
	private static final String RECENT="会话";
	private ArrayList pageViews;
	private RadioGroup radioGroup;
	private ViewPager viewPager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        
        initView();
        viewPager=(ViewPager) findViewById(R.id.main_viewpager);
        viewPager.setAdapter(new PagerAdapter(){
			public int getCount() {
				return pageViews.size();
			}
			public boolean isViewFromObject(View view, Object objcet) {
				return view==objcet;
			}
			//这里会对需要进行水平切换的页面进行了加载和初始化 android:tileMode="repeat"
			public Object instantiateItem(View view, int id) {
				((ViewPager)view).addView(pageViews.get(id));
				return pageViews.get(id);
			}
			public void destroyItem(View view, int id, Object arg2) {  
                ((ViewPager) view).removeView(pageViews.get(id));  
            }
        });
        viewPager.setCurrentItem(2);//默认显示的是好友页面
        radioGroup = (RadioGroup) findViewById(R.id.main_radiogroup);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				setClick(checkedId);
			}
		});
    }
    
    void initView() {
    	pageViews=new ArrayList();
    	View view1 = getLocalActivityManager().startActivity(TRENDS,
    			new Intent(this, TrendsActivity.class)).getDecorView();
    	View view2 = getLocalActivityManager().startActivity(GROUP,
    			new Intent(this, GroupActivity.class)).getDecorView();
    	View view3 = getLocalActivityManager().startActivity(BUDDY,
    			new Intent(this, BuddyActivity.class)).getDecorView();
    	View view4 = getLocalActivityManager().startActivity(RECENT,
    			new Intent(this, RecentActivity.class)).getDecorView();
    	pageViews.add(0,view1);
    	pageViews.add(1,view2);
    	pageViews.add(2,view3);
    	pageViews.add(3,view4);
    }

	public void setClick(int id) {
		switch(id){
		case R.id.main_radio_trends:
			viewPager.setCurrentItem(0);
			break;
		case R.id.main_radio_group:
			viewPager.setCurrentItem(1);
			break;
		case R.id.main_radio_buddy:
			viewPager.setCurrentItem(2);
			break;
		case R.id.main_radio_recent:
			viewPager.setCurrentItem(3);
			break;
		}
	}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}


你可能感兴趣的:(Android 高仿QQ 界面滑动效果)