美文网首页
Tablayout+ViewPager+Fragment的使用

Tablayout+ViewPager+Fragment的使用

作者: 栗子daisy | 来源:发表于2019-08-16 14:57 被阅读0次

    xml

    tab_layout.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">
        <!--TabLayout+ViewPager-->
        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:ignore="MissingConstraints" />
    
        <android.support.v4.view.ViewPager
            android:id="@+id/main_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:ignore="MissingConstraints" />
    </LinearLayout>
    

    tab_sec.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tab_first"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <ImageView
            android:id="@+id/iv_head"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="25dp"
            android:background="@color/btn_filled_blue_bg_normal" />
        <!--输入框-->
        <EditText
            android:id="@+id/et_user_name"
            android:layout_width="fill_parent"
            android:layout_height="48dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="35dp"
            android:layout_marginTop="35dp"
            android:layout_marginRight="35dp"
            android:drawableLeft="@drawable/user"
            android:drawablePadding="10dp"
            android:gravity="center_vertical"
            android:hint="请输入用户名"
            android:paddingLeft="8dp"
            android:singleLine="true"
            android:textColor="#000000"
            android:textColorHint="#a3a3a3"
            android:textSize="14sp" />
        <!--输入框-->
        <EditText
            android:id="@+id/et_psw"
            android:layout_width="fill_parent"
            android:layout_height="48dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="35dp"
            android:layout_marginRight="35dp"
            android:drawableLeft="@drawable/pwd"
            android:drawablePadding="10dp"
            android:gravity="center_vertical"
            android:hint="请输入密码"
            android:inputType="textPassword"
            android:paddingLeft="8dp"
            android:singleLine="true"
            android:textColor="#000000"
            android:textColorHint="#a3a3a3"
            android:textSize="14sp" />
        <!--按钮-->
        <Button
            android:id="@+id/btn_login"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="35dp"
            android:layout_marginTop="15dp"
            android:layout_marginRight="35dp"
            android:text="登 录"
            android:textColor="@android:color/white"
            android:textSize="18sp" />
        <!--显示tv register , find_psw -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="35dp"
            android:layout_marginTop="8dp"
            android:layout_marginRight="35dp"
            android:gravity="center_horizontal"
            android:orientation="horizontal">
    
            <TextView
                android:id="@+id/tv_register"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:padding="8dp"
                android:text="立即注册"
                android:textColor="@android:color/white"
                android:textSize="14sp" />
            <!--layout_weight="1" layout_width="0dp"实现均分效果-->
            <TextView
                android:id="@+id/tv_find_psw"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:padding="8dp"
                android:text="找回密码?"
                android:textColor="@android:color/white"
                android:textSize="14sp" />
        </LinearLayout>
    </LinearLayout>
    

    activity

    LoginActivity.java

    public class LoginActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
          //  隐藏标题栏
            if (getSupportActionBar() != null) {
                getSupportActionBar().hide();
            }
            setContentView(R.layout.tab_layout);
            TabLayout mTabLayout = findViewById(R.id.tab_layout);
            // 添加 tab item
            mTabLayout.addTab(mTabLayout.newTab().setText("注册"));
            mTabLayout.addTab(mTabLayout.newTab().setText("登录"));
            //设置adapter,滑动时间
            final ViewPager viewPager;
            viewPager = (ViewPager) findViewById(R.id.main_pager);
            viewPager.setAdapter(new PageAdapter(getSupportFragmentManager(), mTabLayout.getTabCount()));
            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
            //绑定tab点击事件
            mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    viewPager.setCurrentItem(tab.getPosition());
                }
                @Override
                public void onTabUnselected(TabLayout.Tab tab) {
                }
                @Override
                public void onTabReselected(TabLayout.Tab tab) {
                }
            });
    

    PageAdapter.java

    package com.example.tmftest;
    
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.util.Log;
    
    import java.util.HashMap;
    
    public class PageAdapter extends FragmentPagerAdapter {
        private int num;
        private HashMap<Integer, Fragment> mFragmentHashMap = new HashMap<>();
    
        public PageAdapter(FragmentManager fm, int num) {
            super(fm);
            this.num = num;
        }
        @Override
        public Fragment getItem(int position) {
            return createFragment(position);
        }
        @Override
        public int getCount() {
            return num;
        }
        private Fragment createFragment(int pos) {
            Fragment fragment = mFragmentHashMap.get(pos);
            if (fragment == null) {
                switch (pos) {
                    case 0:
                        fragment = new Fragment1();
                        Log.i("fragment", "fragment1");
                        break;
                    case 1:
                        fragment = new Fragment2();
                        Log.i("fragment", "fragment2");
                        break;
                }
                mFragmentHashMap.put(pos, fragment);
            }
            return fragment;
        }
    }
    

    Fragment2.java

    public class Fragment2 extends android.support.v4.app.Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            View view = inflater.inflate(R.layout.tab_first,container,false);
            EditText editText1 = view.findViewById(R.id.et_user_name);
            Drawable drawable1 = getResources().getDrawable(R.drawable.user);
            drawable1.setBounds(0, 0, 60, 60);//第一0是距左边距离,第二0是距上边距离,60,60 分别是长宽
            editText1.setCompoundDrawables(drawable1, null, null, null);//只放左边
    
            EditText editText2 = view.findViewById(R.id.et_psw);
            Drawable drawable2 = getResources().getDrawable(R.drawable.pwd);
            drawable2.setBounds(0, 0, 60, 60);
            editText2.setCompoundDrawables(drawable2, null, null, null);
    
            return view;
        }
    }
    
    页面

    相关文章

      网友评论

          本文标题:Tablayout+ViewPager+Fragment的使用

          本文链接:https://www.haomeiwen.com/subject/cazfsctx.html