Android---侧滑效果自定义及fragment,tabLy

2017-06-22  本文已影响477人  this_is_for_u

记录方便后续使用查找,全部知识参考code小生,需更系统学习直接看他简书http://www.jianshu.com/p/ea691fda7d2a

效果如图:

image.png image.png

目录:

image.png

Android studio创建module时直接选择创建带侧滑栏的activity,在其上面修改自定义布局即可。

actity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:fitsSystemWindows="true" tools:openDrawer="start">

    <include layout="@layout/app_bar_main" android:layout_width="match_parent"
        android:layout_height="match_parent" />

    //自定义侧滑栏更改此处,注释掉后面内容
    <include layout="@layout/draw_menu_layout"/>

    <!--<android.support.design.widget.NavigationView android:id="@+id/nav_view"-->
        <!--android:layout_width="wrap_content" android:layout_height="match_parent"-->
        <!--android:layout_gravity="start" android:fitsSystemWindows="true"-->
        <!--app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" />-->

</android.support.v4.widget.DrawerLayout>

draw_menu_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:clickable="true"
    android:tag="left">

    <ImageView
        android:id="@+id/iv_drawer_bg"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/fnn" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/id_draw_menu_header"
            android:layout_width="match_parent"
            android:layout_height="200dp">

            <TextView
                android:id="@+id/id_draw_menu_item_backup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="100dp"
                android:drawableLeft="@drawable/ic_launcher"
                android:drawablePadding="15dp"
                android:gravity="center"
                android:text="你好"
                android:textColor="@android:color/white"
                android:textSize="15sp" />

            <ImageView
                android:id="@+id/id_draw_menu_item_download"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_alignParentRight="true"
                android:layout_marginRight="15dp"
                android:layout_marginTop="35dp"
                android:scaleType="centerCrop"
                android:src="@drawable/qrcode_min_cm" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white">

            <TextView
                android:id="@+id/id_draw_menu_item_main_tv"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_alignParentBottom="true"
                android:gravity="center_vertical"
                android:paddingLeft="15dp"
                android:text="hello world"
                android:textSize="18sp" />

            <ListView
                android:id="@+id/id_draw_menu_item_list_select"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_above="@id/id_draw_menu_item_main_tv"
                android:divider="@android:color/transparent"
                android:overScrollMode="never"
                android:scrollbars="none" />
        </RelativeLayout>
    </LinearLayout>

</FrameLayout>

MainActivity

listView = (ListView) findViewById(R.id.id_draw_menu_item_list_select);
        listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, strings));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this, strings[i], Toast.LENGTH_SHORT).show();
            }
        });

侧滑效果已经完成,之后为fragment侧滑翻页功能:
content_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="megvii.tab.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <android.support.design.widget.TabLayout
        android:id="@+id/tab"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:tabGravity="center"
        app:tabMode="scrollable" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

MainActivity:

tabLayout = (TabLayout) findViewById(R.id.tab);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        //这种方式没有侧滑换页功能
        for(int i = 0; i < 5; i++){
            tabList.add("hello" + i);
        }
        for(int i = 0; i < 5; i++){
            tabLayout.addTab(tabLayout.newTab().setText(tabList.get(i)));
        }
        myFragmentAdapter adapter = new myFragmentAdapter(getSupportFragmentManager(),MainActivity.this);
        viewPager.setAdapter(adapter);
        //setupWithViewPager()方法底部会调用FragmentPagerAdapter中的getPageTitle()方法
        tabLayout.setupWithViewPager(viewPager);

myFragmentAdapter:

package megvii.tab;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

/**
 * Created by wangzhiqiang on 2017/6/22.
 */
import java.util.List;
//control + o
public class myFragmentAdapter extends FragmentPagerAdapter {

    private Context context;
    private String[] titles = new String[]{"消息", "联系人", "动态", "3", "f"};

    public myFragmentAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public Fragment getItem(int position) {
        return CommonFragment.newInstance(titles[position]);
    }

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

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

CommonFragment


public class CommonFragment extends Fragment {

    public static final String ARGS_PAGE = "args_page";
    private String contents;

    public static CommonFragment newInstance(String string) {
        Bundle args = new Bundle();
        args.putString(ARGS_PAGE, string);
        CommonFragment fragment = new CommonFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contents = getArguments().getString(ARGS_PAGE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        TextView textView = new TextView(getActivity());
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(17);
        textView.setText(contents + " Fragment");
        return textView;
    }
}

可以在AppBarLayout嵌套TabLayout,实现视图滚动时Toolbar隐藏的功能或AppBarLayout嵌套CollapsingToolbarLayout:
具体参考http://blog.csdn.net/xyz_lmn/article/details/48055919

上一篇下一篇

猜你喜欢

热点阅读