Android 高级控件(三)

1.RecyclerView 5.0

添加RecyclerView引用:
compile 'com.android.support:recyclerview-v7:24.0.0-alpha1'(build.gradle)

<1>布局方式:LayoutManager
    横向布局 
    垂直布局
    网格布局Grid
<2>分割线:ItemDecoration
    Github托管源码:https://github.com/yqritc/RecyclerView-FlexibleDivider
.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/rv_main_recycler"
        >.support.v7.widget.RecyclerView>

activity:

private RecyclerView rv_main_list;

    private List list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rv_main_list = (RecyclerView) findViewById(R.id.rv_main_recycler);

        for (int i = 1; i <=10 ; i++) {
            list.add("张"+i);
        }
        //制定默认的排列方式
        //rv_main_list.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,true));
        rv_main_list.setLayoutManager(new GridLayoutManager(this,2,GridLayoutManager.HORIZONTAL,false));
        rv_main_list.setAdapter(new MyAdapter());

    }
    class MyAdapter extends RecyclerView.Adapter{

        class MyViewHolder extends RecyclerView.ViewHolder{
            public TextView textview;
            public MyViewHolder(View itemView) {
                super(itemView);
                textview= (TextView) itemView.findViewById(android.R.id.text1);
            }
        }
        //类似 BaseAdapter中的 getView()
        //RecyclerView.ViewHolder  类似 ItemTag
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v=getLayoutInflater().inflate(android.R.layout.simple_list_item_1,null);
            return new MyViewHolder(v);
        }

        //给控件绑值
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            MyViewHolder myVH= (MyViewHolder) holder;
            myVH.textview.setText(list.get(position));
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }

2.Gallery 画廊—-过时

完成一个图片的滑动切换效果:

private Gallery g_gallery_image;
    private int images[]={R.drawable.image1,R.drawable.image3,R.drawable.image4};
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);
        g_gallery_image = (Gallery) findViewById(R.id.g_gallery_image);
        g_gallery_image.setAdapter(new MyAdapter());

    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public Object getItem(int position) {
            return images[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView=new ImageView(GalleryActivity.this);
            Gallery.LayoutParams layoutParams=new Gallery.LayoutParams(300,400);
            imageView.setLayoutParams(layoutParams);
            imageView.setImageResource(images[position]);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }
    }

3.ViewPager(Gallery的替代品)

<1>引导页
    适配器   PagerAdapter
    滑动监听 addOnPageChangeListener
    导航点   github开源框架:ViewPagerIndicator
            属性:
            setRadius:设置半径
            setPageColor:点的背景颜色
            setBackgroundColor:控件的背景颜色
            setFillColor:设置点的背景颜色
            setStrokeColor:设置点背景的边框颜色
    进入主界面

<2>ViewPager+Activity(View)—->QQ好友分组 + ListView分组的滑动切换(包含多个activity的切换)

.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mmp_mvp"
        >.support.v4.view.ViewPager>

activity:

private ViewPager mmp_mvp;
    private List views;
    private LocalActivityManager localActivityManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_vp);

        mmp_mvp = (ViewPager) findViewById(R.id.mmp_mvp);
        views=new ArrayList<>();
        //    LocalActivityManager是Android封装的把activity转换成view对象的一个api.
        localActivityManager=new LocalActivityManager(this,true);
        localActivityManager.dispatchCreate(savedInstanceState);

        //页面转换成视图
        Intent intent=new Intent(this,QQActivity.class);//第一个页面
        View view=localActivityManager.startActivity("QQActivity",intent).getDecorView();
        views.add(view);

        Intent intent2=new Intent(this,SzLvActivity.class);//第二个页面
        views.add(localActivityManager.startActivity("SzLvActivity",intent2).getDecorView());

        mmp_mvp.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);
        }
    }

你可能感兴趣的:(Android 高级控件(三))