• 开源代码ViewPageIndicator的使用


    1. 导入Android studio

    使用SlidingMenu的方式导入Android studio不行,不知道为何,过会懂了再写上

     

     

     

    2. 代码

    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="slidingmenu.com.haha.MainActivity">
    
        <com.viewpagerindicator.TabPageIndicator
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/TabIndicator_id"
            android:background="@drawable/base_action_bar_bg_nine"
            ></com.viewpagerindicator.TabPageIndicator>
    
        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/viewPager_id"
            ></android.support.v4.view.ViewPager>
    
    </LinearLayout>

    fragment_item.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:orientation="vertical"
        tools:context="slidingmenu.com.haha.ItemFragment">
    
        <!-- TODO: Update blank fragment layout -->
        <TextView
            android:id="@+id/textview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:textSize="18sp"
            android:gravity="center" />
    
    </LinearLayout>

    MainActivity.java

    package slidingmenu.com.haha;
    
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Toast;
    
    import com.viewpagerindicator.TabPageIndicator;
    
    public class MainActivity extends AppCompatActivity {
        private String TAG = "MainActivity";
        private String[] TITTLe = new String[]{"哈哈","呵呵","嘿嘿","嘻嘻"};
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //1. 拿到viewpager
            ViewPager viewPager = findViewById(R.id.viewPager_id);
            //2. 创建一个适配器
            myPagerAdapter mPagerAdapter = new myPagerAdapter(getSupportFragmentManager());
            //3. 将适配器和Viewpager绑定
            viewPager.setAdapter(mPagerAdapter);
            //4. 拿到TabPageIndicator
            TabPageIndicator tabPageIndicator = findViewById(R.id.TabIndicator_id);
            //5. 将ViewPager和TabPageIndicator进行绑定
            tabPageIndicator.setViewPager(viewPager);
            //6. 设置页面改变时候的监听函数
            tabPageIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    Log.d(TAG, "onPageScrolled");
                }
    
                @Override
                public void onPageSelected(int position) {
                    Log.d(TAG, "onPageSelected");
                    Toast.makeText(getApplicationContext(), TITTLe[position], Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onPageScrollStateChanged(int state) {
                    Log.d(TAG, "onPageScrollStateChanged");
                }
            });
        }
    
        //适配器
        protected class myPagerAdapter extends FragmentPagerAdapter{
    
            public myPagerAdapter(FragmentManager fm) {
                //Log.d(TAG, "myPagerAdapter");
                super(fm);
            }
    
            @Override
            public int getCount() {
                Log.d(TAG, "getCount");
                return TITTLe.length;
            }
            //创建一个新的界面(通过fragment)
            @Override
            public Fragment getItem(int position) {
                Log.d(TAG, "getItem");
                ItemFragment fragment = new ItemFragment();
                Bundle args = new Bundle();
                args.putString("arg", TITTLe[position]);
                fragment.setArguments(args);
                return fragment;
            }
            //显示抬头的tab
            public CharSequence getPageTitle(int position) {
                Log.d(TAG, "getPageTitle");
                return TITTLe[position % TITTLe.length];
            }
        }
    }

    ItemFragment.java

    package slidingmenu.com.haha;
    
    import android.content.Context;
    import android.net.Uri;
    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;
    
    public class ItemFragment extends Fragment {
    
        public ItemFragment() {
            // Required empty public constructor
        }
    
        public static ItemFragment newInstance(String param1, String param2) {
            ItemFragment fragment = new ItemFragment();
            return fragment;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View contextView = inflater.inflate(R.layout.fragment_item, container, false);
            TextView mTextView = (TextView) contextView.findViewById(R.id.textview);
            //获取Activity传递过来的参数
            Bundle mBundle = getArguments();
            String title = mBundle.getString("arg");
            mTextView.setText(title);
    
            return contextView;
        }
    
    }

    资料:

    导入:http://blog.csdn.net/CDWLX/article/details/52161075

    使用:http://blog.csdn.net/xiaanming/article/details/10766053

  • 相关阅读:
    LVS三种模式配置及优点缺点比较
    LVS负载均衡-DR模式
    Nginx反向代理和负载均衡
    LNMP(centos7.6 Nginx1.16 MySQL5.7 PHP7 )
    Nginx常用模块
    表演中的晴天
    迷雾中前行
    mysql Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nona
    npm install -g cnpm --registry=https://registry.npm.taobao.org
    ubuntu16 安装elasticsearch6.3问题
  • 原文地址:https://www.cnblogs.com/maogefff/p/7840479.html
Copyright © 2020-2023  润新知