效果:
1、首先创建一个自带navigation的项目
2、在MainActivity里添加下列代码
(1)定义tablayout和viewpager
(1)fragment的集合以及自定义fragment和动态添加的fragment,tablayout,viewpager。
(2)viewpager适配器,构造方法设置数据源,使tablayout和viewpager一一对应并实现联动。
*:addview和myview的顺序区别在于先后显示哪个fragemnt,由于我数组里面是先定义的是自定义添加控件,一一对应则序号1。
List<Fragment> listFragment = new ArrayList<>();//viewpager中的fragment集合 MyViewFragment myViewFragment = new MyViewFragment(); listFragment.add(myViewFragment);//与上面的红圈1对应 AddViewFragment addViewFragment = new AddViewFragment(); listFragment.add(addViewFragment);//与上面的红圈2对应 tabLayout = (TabLayout) findViewById(R.id.tablayout); viewPager = (ViewPager) findViewById(R.id.viewpager); // ViewPagerAdapter mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(),tabTitle,listFragment); //通过构造方法设置adapter的数据源 viewPager.setAdapter(mViewPagerAdapter); tabLayout.setupWithViewPager(viewPager);
3、建立两个fragment:AddviewFragment和MyViewFragment。
AddviewFragment:
(1)这里的i是全局变量,就是用来控制我每添加一个button的,因为后面有删除,为了使其向后逐一删除。
public class AddViewFragment extends Fragment { public AddViewFragment() { // Required empty public constructor } private LinearLayout layout; private int i =0; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragmenti View inflate = inflater.inflate(R.layout.fragment_add_view, container, false); layout = (LinearLayout) inflate.findViewById(R.id.addlayout); Button addButton = (Button) inflate.findViewById(R.id.add_button); Button removeButton = (Button) inflate.findViewById(R.id.remove_button); Button button = new Button(getContext()); button.setText("button"); button.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); layout.addView(button); addButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { i++; final Button btn3 = new Button(getContext()); layout.addView(btn3); btn3.setId(i); btn3.setText("Button"+i); btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(getContext(), btn3.getText().toString(), Toast.LENGTH_LONG).show(); } }); } }); removeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(i<=0){ Toast.makeText(getContext(), "没有可以删除的按钮", Toast.LENGTH_LONG).show(); } else { layout.removeView(layout.findViewById(i)); i--; } } }); return inflate; }
MyViewFragment:
public class MyViewFragment extends Fragment { public MyViewFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_my_view, container, false); }
}
ViewPagerAdapter:
这里的适配器其实就是传值给fragment,使其在相应的tabTitle下有值。下面三个可以看做是一个for循环。
public class ViewPagerAdapter extends FragmentPagerAdapter { private String[] tabTitle; private List<Fragment> listFragment; public ViewPagerAdapter(FragmentManager fm, String[] tabTitle, List<Fragment> listFragment) { super(fm); this.tabTitle = tabTitle; this.listFragment = listFragment; } @Override public Fragment getItem(int position) { return listFragment.get(position); } @Override public int getCount() { return tabTitle.length; } @Nullable @Override public CharSequence getPageTitle(int position) { return tabTitle[position]; } }
4、layout下xml文件:
activity_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" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> </LinearLayout>
app_bar_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" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> </LinearLayout>
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" tools:context=".MainActivity" android:orientation="vertical"> <android.support.design.widget.TabLayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="wrap_content"> </android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager> </LinearLayout>
fragment_add_view.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".AddViewFragment"> <!-- TODO: Update blank fragment layout --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/add_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="添加" android:layout_weight="1" /> <Button android:id="@+id/remove_button" android:layout_width="0dp" android:layout_height="wrap_content" android:text="删除" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:id="@+id/addlayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> </LinearLayout>
fragment_my_view.xml:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MyViewFragment"> <!-- TODO: Update blank fragment layout --> <com.example.mac.myapplication1.MyView android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout>
nav_header_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" android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height" android:background="@drawable/side_nav_bar" android:gravity="bottom" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:theme="@style/ThemeOverlay.AppCompat.Dark"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/nav_header_desc" android:paddingTop="@dimen/nav_header_vertical_spacing" app:srcCompat="@mipmap/ic_launcher_round" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="@string/nav_header_title" android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/nav_header_subtitle" /> </LinearLayout>
5、menu下的文件
activity_main_drawer.xml:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:showIn="navigation_view"> <group android:checkableBehavior="single"> <item android:id="@+id/nav_camera" android:icon="@drawable/ic_menu_camera" android:title="Import" /> <item android:id="@+id/nav_gallery" android:icon="@drawable/ic_menu_gallery" android:title="Gallery" /> <item android:id="@+id/nav_slideshow" android:icon="@drawable/ic_menu_slideshow" android:title="Slideshow" /> <item android:id="@+id/nav_manage" android:icon="@drawable/ic_menu_manage" android:title="Tools" /> </group> <item android:title="Communicate"> <menu> <item android:id="@+id/nav_share" android:icon="@drawable/ic_menu_share" android:title="Share" /> <item android:id="@+id/nav_send" android:icon="@drawable/ic_menu_send" android:title="Send" /> </menu> </item> </menu>
main.xml:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/item00" android:title="创建群组" android:icon="@mipmap/add" app:showAsAction="never"> </item> <item android:id="@+id/item01" android:title="加好友/群" android:icon="@mipmap/user" app:showAsAction="never"> </item> <item android:id="@+id/item02" android:title="扫一扫" android:icon="@mipmap/scanf" app:showAsAction="never"> </item> <item android:id="@+id/item03" android:title="面对面快传" android:icon="@mipmap/file" app:showAsAction="never"> </item> <item android:id="@+id/item04" android:title="付款" android:icon="@mipmap/erweima" app:showAsAction="never"> </item> <item android:id="@+id/item05" android:title="拍摄" android:icon="@mipmap/camera" app:showAsAction="never"> </item> </menu>
总结:大概就这样了,如果对上面有什么疑惑可以问我,有不好的请指出一起探讨。谢谢!