Android 简单好用又特效十足的粘性布局CoordinatorLayout,解决屏幕适配问题

版权声明:未经本人允许,转载需要注明出处 https://blog.csdn.net/nazicsdn/article/details/79583850

coordinatorlayout介绍:

coordinatorlayout是5.0之后推出一个功能。使用布局完成特效比较简单。它像一个协调者,来协助一些属性完成一些特效。appbarlayout,是一个可以滑动的控件,但是必须在coordinatorlayout里面才可以有滑动效果的,appbarlayout也是5.0推出的新特效。在这里不多说了,具体详细的一些属性,你可以在网上查找。这个属性用起来特别方便的,好像有的公司就推荐用这一种布局。

需要依赖的包:

            compile 'com.android.support:design:24.2.0'

xml布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <android.support.design.widget.AppBarLayout
        android:id="@+id/mAppbarLayout"
        android:fitsSystemWindows="true"
        android:layout_width="match_parent"
        android:layout_height="300dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/mCtbarLayout"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|enterAlways"
            android:layout_width="match_parent"
            android:layout_height="250dp">


        </android.support.design.widget.CollapsingToolbarLayout>
        <android.support.design.widget.TabLayout
            android:id="@+id/mTable"
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            ></android.support.design.widget.TabLayout>

    </android.support.design.widget.AppBarLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    </android.support.v4.view.ViewPager>


</android.support.design.widget.CoordinatorLayout>



主界面代码:接下来是三个fragment的布局自己任意写,在这里就不展示了。

MainActivity

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    private String[] s=new String[]{"主页","销售","我的"};
    private List<Fragment> list;
    private TabLayout mTablayout;
    private ViewPager viewpager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initTitle();
    }
    private void initView() {
        mTablayout= (TabLayout) findViewById(R.id.mTable);
        viewpager= (ViewPager) findViewById(R.id.viewpager);
    }
    private void initTitle() {
        list=new ArrayList<>();
        list.add(new Fragment1());
        list.add(new Fragment2());
        list.add(new Fragment3());
        MyAdapter adapter=new MyAdapter(getSupportFragmentManager(),list,s);
        viewpager.setAdapter(adapter);
        mTablayout.setupWithViewPager(viewpager);
    }
}

MyAdapter

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;
import android.view.ViewGroup;
import java.util.List;

public class MyAdapter extends FragmentPagerAdapter{
    private List<Fragment> list;
    private String[] s;
    public MyAdapter(FragmentManager fm,List<Fragment> list,String[] s) {
        super(fm);
        this.list=list;
        this.s=s;
    }

    @Override
    public Fragment getItem(int position) {
        return list.get(position);
    }

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

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

    @Override
    public CharSequence getPageTitle(int position) {
        return s[position];
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return super.instantiateItem(container, position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
    }
}
VpAdapter
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

public class Vpadapter extends PagerAdapter{
    private List<ImageView> list;

    public Vpadapter(List<ImageView> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return 3;
    }

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


    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        container.addView( list.get(position), 0);//添加页卡
        return list.get(position);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(list.get(position));//删除页卡
    }
}
接下来写三个Fragment类,也不一一列举了,Fragment里添加个布局就行。
展开阅读全文

没有更多推荐了,返回首页