• Viewpager的用法


    内页面  不单写页面   viewpager+布局

    import java.util.ArrayList;
    import java.util.List;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.graphics.Color;
    import android.support.v4.view.ViewPager;
    import android.support.v4.view.ViewPager.OnPageChangeListener;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity implements OnClickListener {
    
        private List<View> list;
        private ViewPager viewpager;
        private Button button1;
        private Button button2;
        private Button button3;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            setData();
            button1 = (Button) findViewById(R.id.button1);
            button2 = (Button) findViewById(R.id.button2);
            button3 = (Button) findViewById(R.id.button3);
            button1 .setOnClickListener(this);
            button2.setOnClickListener(this);
            button3.setOnClickListener(this);
            
            viewpager = (ViewPager) findViewById(R.id.viewpager);
            viewpager.setAdapter(new MyPagerAdapter(list));
            viewpager.setCurrentItem(1);
            setButtonColor(viewpager.getCurrentItem());
            viewpager.setOnPageChangeListener(new OnPageChangeListener() {
                
                @Override
                public void onPageSelected(int position) {
                    setButtonColor(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
                    
                }
            });
        
        
        }
    
        private void setData() {
            list = new ArrayList<View>();
            View view1=View.inflate(MainActivity.this,R.layout.viewpager_1, null);
            View view2=View.inflate(MainActivity.this,R.layout.viewpager_2,null);
            View view3=View.inflate(MainActivity.this,R.layout.viewpager_3,null);
            list.add(view1);
            list.add(view2);
            list.add(view3);
        }
        private void setButtonColor(int position) {
            // TODO Auto-generated method stub
            switch(position){
            case 0:
                button1.setTextColor(Color.WHITE);
                
                button2.setTextColor(Color.BLACK);
                button3.setTextColor(Color.BLACK);
                break;
            case 1:
                button2.setTextColor(Color.WHITE);
                
                button1.setTextColor(Color.BLACK);
                button3.setTextColor(Color.BLACK);
                break;
            case 2:
                button3.setTextColor(Color.WHITE);
                
                button2.setTextColor(Color.BLACK);
                button1.setTextColor(Color.BLACK);
                break;
            }
        }
        @Override
        public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
            viewpager.setCurrentItem(0);
            
        
            break;
        case R.id.button2:
            viewpager.setCurrentItem(1);
            
        
            break;
        case R.id.button3:
            viewpager.setCurrentItem(2);
            
            
            break;
        
        }    
            
        }
    
       
        
    }
    import java.util.List;
    
    import android.support.v4.view.PagerAdapter;
    import android.view.View;
    import android.view.ViewGroup;
    
    public class MyPagerAdapter extends PagerAdapter{
        List<View> list;
        public MyPagerAdapter(List<View> list) {
            this.list=list;
        }
    
        @Override
        public int getCount() {
            
            return list.size();
        }
    
        @Override
        public boolean isViewFromObject(View view, Object object) {
            
            return view==object;
        }
       @Override
       public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(list.get(position));
            return list.get(position);
        }
    }

    外页面   自定义页面   Viewpager+Fragment   (Fragment导V4包)

    import java.util.ArrayList;
    
    import java.util.List;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.view.ViewPager;
    import android.support.v4.view.ViewPager.OnPageChangeListener;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends FragmentActivity implements OnClickListener{
    
        private ViewPager viewPager;
        private Button button1;
        private Button button2;
        private Button button3;
        private List<Fragment> list;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
         viewPager = (ViewPager) findViewById(R.id.viewpager);
         
         button1 = (Button) findViewById(R.id.button1);
         button2 = (Button) findViewById(R.id.button2);
         button3 = (Button) findViewById(R.id.button3);
         
         button1.setOnClickListener(this);
         button2.setOnClickListener(this);
         button3.setOnClickListener(this);
         setCon();
         FragmentManager fm = getSupportFragmentManager();
         viewPager.setAdapter(new MyFragmentpagerAdapter(fm, list));  
         viewPager.setOnPageChangeListener(new OnPageChangeListener() {
            
            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "当前页面是:页卡"+(arg0+1), 0).show();
            }
            
            @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
                
            }
        });
        }
    
        private void setCon() {
            list = new ArrayList<Fragment>();
            Fragment1 f1=new Fragment1();
            Fragment2 f2=new Fragment2();
            Fragment3 f3=new Fragment3();
            list.add(f1);
            list.add(f2);
            list.add(f3);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.button1:
                viewPager.setCurrentItem(0);
                break;
            case R.id.button2:
                viewPager.setCurrentItem(1);
                break;
            case R.id.button3:
                viewPager.setCurrentItem(2);
                break;
            
            }
            
        }
    
        
    }
    import java.util.List;
    
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.PagerAdapter;
    import android.view.View;
    
    public class MyFragmentpagerAdapter extends FragmentPagerAdapter {
         List<Fragment> list;
        public MyFragmentpagerAdapter(FragmentManager fm, List<Fragment> list) {
            super(fm);
            this.list=list;
        }
    
        @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();
        }
    
        
    
    }
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    
    public class Fragment1 extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view=inflater.inflate(R.layout.fragment1, null);
        return view;
    }
    }
  • 相关阅读:
    array与xml转换实现(转)
    设计模式之: 策略模式
    设计模式之: 代理模式
    设计模式之: 状态模式
    dedecms分页
    dedecms导出csv文件
    假如项目中使用到了多 表查询,怎么办?
    git忽略某个文件夹
    git忽略某个文件
    无极限分类
  • 原文地址:https://www.cnblogs.com/1426837364qqcom/p/5205110.html
Copyright © 2020-2023  润新知