• 【IMOOC学习笔记】多种多样的App主界面Tab实现方法(四)


     ViewPagerIndicator+ViewPager

    要想使用ViewPagerIndicator,要使用到viewPagerlibrary开源库

    top.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#58ACED"
        android:gravity="center_vertical"
        android:orientation="horizontal" >
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/idx_logo" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="imooc"
            android:textColor="#ffffff"
            android:layout_marginLeft="3dp"
            android:textSize="20sp"
            android:textStyle="bold" />
    
    </LinearLayout>

    每个tab页面,很简单,只有一个textView:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="#ffffff"
        android:layout_height="match_parent" >
    
        <TextView
            android:id="@+id/id_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textStyle="bold"
            android:textSize="22sp"
            android:text="helloworld" />
    
    </RelativeLayout>

    activity_main.xml

        <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:background="#C5DAED"
        android:orientation="vertical" >
    
        <include layout="@layout/top" />
    
        <com.viewpagerindicator.TabPageIndicator
            android:id="@+id/id_indicator"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent" >
        </com.viewpagerindicator.TabPageIndicator>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/id_viewpager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </android.support.v4.view.ViewPager>
    
    </LinearLayout>

    MainActivity.java

    package com.imooc.tab04;
    
    import java.util.List;
    
    import android.app.Fragment;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.view.ViewPager;
    import android.view.Window;
    
    import com.viewpagerindicator.TabPageIndicator;
    
    public class MainActivity extends FragmentActivity
    {
        private ViewPager mViewPager;
        private TabPageIndicator mTabPageIndicator;
        private TabAdapter mAdapter ;
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main);
            
            initView();
        }
        private void initView()
        {
            mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
            mTabPageIndicator = (TabPageIndicator) findViewById(R.id.id_indicator);
            mAdapter = new TabAdapter(getSupportFragmentManager());
            mViewPager.setAdapter(mAdapter);
            
            mTabPageIndicator.setViewPager(mViewPager, 0);
        }   
    }

    TabAdapter

    package com.imooc.tab04;
    
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    
    public class TabAdapter extends FragmentPagerAdapter
    {
    
        public static String[] TITLES = new String[]
        { "课程", "问答", "求课", "学习", "计划" };
    
        public TabAdapter(FragmentManager fm)
        {
            super(fm);
        }
    
        @Override
        public Fragment getItem(int arg0)
        {
            TabFragment fragment = new TabFragment(arg0);
            return fragment;
        }
    
        @Override
        public int getCount()
        {
            return TITLES.length;
        }
    
        @Override
        public CharSequence getPageTitle(int position)
        {
            return TITLES[position];
        }
    
    }

    TabFragment

    package com.imooc.tab04;
    
    import android.annotation.SuppressLint;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    @SuppressLint("ValidFragment")
    public class TabFragment extends Fragment
    {
        private int pos;
    
        @SuppressLint("ValidFragment")
        public TabFragment(int pos)
        {
            this.pos = pos;
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState)
        {
            View view = inflater.inflate(R.layout.frag, container, false);
            TextView tv = (TextView) view.findViewById(R.id.id_tv);
            tv.setText(TabAdapter.TITLES[pos]);
            return view;
        }
    }
  • 相关阅读:
    QTableWidget清空
    SQLite查询表是否存在
    QSplitter测试
    Qto_CoveringBaseQuantities
    osg旋转
    Qto_CurtainWallQuantities
    没有理清的一段代码
    方块
    Qto_DoorBaseQuantities
    全微分在近似计算中的应用
  • 原文地址:https://www.cnblogs.com/six-moon/p/4644723.html
Copyright © 2020-2023  润新知