Android(安卓)Fragment viewPage TabLayout用法。
16lz
2021-12-04
今天,搞了两个小时,真特么丢脸。 以前只晓得用各种框架,各种套用,现在项目 帮别人擦屁股,不懂具体怎么走的 还是不行啊。今天记下
前提:
compile 'com.android.support:support-v4:25.2.0' compile 'com.bigkoo:convenientbanner:2.0.5' compile 'com.android.support:design:25.2.0' //后面的版本号,根据当前情况来定。
第一种:写死的用法。(就是不灵活,需要几个创建指定的几个)
1,布局页面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/blankgray2" android:orientation="vertical"> <include layout="@layout/navigation_tow"/> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="wrap_content"/>LinearLayout>
2,include里面的 这里就是一个标题栏而已。只用下面TabLayout。
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/navigtion" android:layout_width="match_parent" android:layout_height="88px"> <ImageView android:id="@+id/back" android:layout_centerVertical="true" android:layout_marginLeft="30px" android:src="@mipmap/white_back" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <android.support.design.widget.TabLayout android:id="@+id/sliding_tabs" style="@style/MyCustomTabLayout" android:layout_width="wrap_content" app:tabBackground="@drawable/tab_shape_oval" app:tabGravity="center" android:layout_centerInParent="true" android:layout_height="match_parent" app:tabTextAppearance="@style/TabLayoutTextStyle" /> <TextView android:layout_marginRight="30px" android:layout_centerVertical="true" android:id="@+id/right_title_content" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <View android:visibility="gone" android:background="@color/blankgray" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="1px" />RelativeLayout>
第二步:创建Fragment 需要的页面 继承Fragment 里面。
第三步: 适配器页面
package com.theagent.adapter;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentPagerAdapter;import com.theagent.fragment.MeberAndMerFragment;import java.util.List;public class MemberAndMerChantAdapter extends FragmentPagerAdapter { private String[] titles ; private List list; public MemberAndMerChantAdapter(FragmentManager fm,List list,String[] titles) { super(fm); this.list = list; this.titles=titles; } @Override public Fragment getItem(int position) { return list.get(position);//这个是fragment 的 下标。 } @Override public int getCount() { return list.size(); } //重写这个方法,将设置每个Tab的标题 @Override public CharSequence getPageTitle(int position) { return titles[position]; }}
第四步:Activity 页面 也就是第一步的class
package com.theagent.activity;import android.os.Bundle;import android.support.design.widget.TabLayout;import android.support.v4.app.Fragment;import android.support.v4.view.ViewPager;import android.widget.ImageView;import android.widget.RelativeLayout;import com.theagent.R;import com.theagent.adapter.GradedAdapter;import com.theagent.adapter.MemberAndMerChantAdapter;import com.theagent.base.RxBaseActivity;import com.theagent.fragment.DataStatisticsFragment;import com.theagent.fragment.MeberAndMerFragment;import java.util.ArrayList;import java.util.List;import butterknife.Bind;/** * Created by Administrator on 2017/3/30. * */public class DatastatisticsActivity extends RxBaseActivity { @Bind(R.id.sliding_tabs) TabLayout tabLayout; @Bind(R.id.viewpager) ViewPager mViewPager; @Bind(R.id.navigtion) RelativeLayout mNavigtion; MemberAndMerChantAdapter andMerChantAdapter; private List list; private DataStatisticsFragment mMemberFragment, mMerchantFragment; private GradedAdapter mGradedAdapter; private String[] titles = new String[]{"会员", "商家"}; @Override public int getLayoutId() { return R.layout.data_statistics_activity; } @Override public void initViews(Bundle savedInstanceState) { mNavigtion.setBackgroundResource(R.color.background2); list=new ArrayList<>(); //这种比较灵活吧。不过页面具体也不算好控制。 如果都是listview //就推荐这种方式: for (int i = 0; i new MeberAndMerFragment(); Bundle bundle=new Bundle(); bundle.putString("title",titles[i]);//很显然这里面的值就是数据里面的,所以,可以根据穿值来显示 f.setArguments(bundle); list.add(f);//添加到list 中 } /** * 还有一种写法是这样子的,就是实际 有多少个,Frament 就显示几个 *不要上面的循环 * list.add(new Fragment1) * list.add(new Fragment2) * .... 最后效果一样的, */ //ViewPage的适配器 andMerChantAdapter=new MemberAndMerChantAdapter(getSupportFragmentManager(),list,titles); mViewPager.setAdapter(andMerChantAdapter); //绑定 tabLayout.setupWithViewPager(mViewPager); }}
最后说说:如果用到循环那种方式,我们就要在Fragment中取值,进行判断了。你要是没用灵活创建,就没必要取值。
在 fragment 中onCreateView中. @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(getLayoutId(), container, false); ButterKnife.bind(this, rootView); mPresenter = InstanceUtil.getInstance(this, 0); //这里就是把activity 中的值取出来。 if else 里面就是标题栏数组咯。 savedInstanceState = getArguments(); String order = savedInstanceState.getString("title"); OkLogger.e("类型 save:" + order); if (order.equals("会员")) { //加载会员的数据, OkLogger.e("类型: " + order); testTv.setText(order); } else {//加载商家的数据 OkLogger.e("类型: " + order); testTv.setText(order); } return rootView; }
这是效果图:
好了,其他用法,我用的少,也是新手,希望大家多多指点。推荐好的博客也好。
更多相关文章
- Android(安卓)Studio 首次创建工程下载Gradle失败
- Android(安卓)开发常用开源库
- [android] 保存文件到手机内存
- Android创建隐藏文件或者文件夹
- AndroidStudio多渠道打包心得
- shape 的创建和使用
- Android源代码下载
- android 格式化
- Android(安卓)shell 创建 模拟器