高级控件-ViewPager

ViewPager:现在用的比较多的页面的左右滑动

需要使用适配器

一般在滑动的时候设置监听,修改页面所显示的内容

属性:

setRadius:设置半径
setPageColor:点的背景颜色
setBackgroundColor:控件的背景颜色
setFillColor:设置点的背景颜色
setStrokeColor:设置点背景的边框颜色

下面有两个案例:

ViewPager+布局xml(View)------>LinearLayout,RelativeLayout,FrameLayout:只需要把布局文件转换成view

效果就是可以左右滑动显示页面,页面没有东西可以点击

布局代码:

只需要实现一个ViewPager控件就可以,设置一个ID在JAVA代码调用



    
    




    
    
    
    



    
        




    
上面是三个布局代码,显示内容就可以了。

下面是Java代码:

package com.example.android_studio_10;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

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

public class MainActivity extends AppCompatActivity {

    private ViewPager vp_main_image;
    private int images[]={R.layout.frame,R.layout.linear,R.layout.relative};
    private List views=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp_main_image = (ViewPager) findViewById(R.id.vp_main_image);
        //for循环把布局文件转成View加入集合
        for (int i = 0; i < images.length; i++) {
            View view=getLayoutInflater().inflate(images[i],null);
            views.add(view);
        }
        vp_main_image.setAdapter(new MyAdapter());
    }
    //适配器
    class MyAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View v=views.get(position);
            container.addView(v);
            return v;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View v=views.get(position);
            container.removeView(v);
        }
    }
}

ViewPager+Activity(View)------>QQ好友分组 +  ListView分组:还需要把java文件转换成view

这里就只展示把java问价转换成view的代码了

package com.example.android_studio_10;

import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;

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

/**
 * Created by lx on 2017/6/14.
 */

public class ViewPaperActivity extends AppCompatActivity {

    private ViewPager vp_zy2_image;
    private List views=new ArrayList<>();
    private LocalActivityManager localActivityManager;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zy2_view);
        vp_zy2_image = (ViewPager) findViewById(R.id.vp_zy2_image);
        localActivityManager = new LocalActivityManager(this,true);
        localActivityManager.dispatchCreate(savedInstanceState);
        //把java文件转成view
        Intent intent = new Intent(this,ViewPaper2Activity.class);
        views.add(localActivityManager.startActivity("Zy2Activity",intent).getDecorView());
        Intent intent2 = new Intent(this,ViewPaper3Activity.class);
        views.add(localActivityManager.startActivity("Zy3Activity",intent2).getDecorView());
        vp_zy2_image.setAdapter(new MyAdapter());
    }
    //适配器
     class MyAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View v=views.get(position);
            container.addView(v);
            return v;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View v=views.get(position);
            container.removeView(v);
        }
    }
}




你可能感兴趣的:(高级控件-ViewPager)