• 20 ViewPager demo5,6:FragmentAdapter 导航数据


    Demo5

    文件结构:

    这里写图片描述

    MainActivity.java

    package com.qf.day20_viewpager_demo5;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.graphics.Color;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.support.v4.view.ViewPager.OnPageChangeListener;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    import com.qf.day20_viewpager_demo5.fragment.MyFragment;
    
    public class MainActivity extends FragmentActivity {
    
        private ViewPager viewPager;
    
        private List<Fragment> list;
    
        private TextView[]tvs;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            viewPager = (ViewPager) findViewById(R.id.viewPager);
    
            initTab();
    
            //数据源  Fragment
            initData();
            //adapter
            viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager()));
            viewPager.setOnPageChangeListener(new OnPageChangeListener() {
    
                @Override
                public void onPageSelected(int arg0) {
                    // TODO Auto-generated method stub
                    for(int i=0;i<3;i++){
                        tvs[i].setBackgroundColor(Color.BLUE);
                    }
    
                    tvs[arg0].setBackgroundColor(Color.RED);
                }
    
                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void onPageScrollStateChanged(int arg0) {
                    // TODO Auto-generated method stub
    
                }
            });
    
        }
        //导航书签
        public void initTab() {
            tvs = new TextView[3];
            LinearLayout layout = (LinearLayout) findViewById(R.id.ll);
            for(int i=0;i<3;i++){
                tvs[i] = (TextView) layout.getChildAt(i);
                tvs[i].setBackgroundColor(Color.BLUE);
                tvs[i].setTag(i);
                tvs[i].setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        viewPager.setCurrentItem((Integer)v.getTag());
                    }
                });
            }
            tvs[0].setBackgroundColor(Color.RED);
        }
    
        public void initData() {
            list = new ArrayList<Fragment>();
    
            MyFragment myFragment1 = new MyFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("index", 1);
            myFragment1.setArguments(bundle);
    
            MyFragment myFragment2 = new MyFragment();
            Bundle bundle1 = new Bundle();
            bundle1.putInt("index", 2);
            myFragment2.setArguments(bundle1);
    
            MyFragment myFragment3 = new MyFragment();
            Bundle bundle2 = new Bundle();
            bundle2.putInt("index", 3);
            myFragment3.setArguments(bundle2);
    
            list.add(myFragment1);
            list.add(myFragment2);
            list.add(myFragment3);
        }
    
        /**
         * 自定义的Adapter
         * @author sxy
         *
         */
        public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
    
            public MyFragmentPagerAdapter(FragmentManager fm) {
                super(fm);
                // TODO Auto-generated constructor stub
            }
    
            /**
             * 根据指定的下标  返回对应的Fragmengt对象
             */
            @Override
            public Fragment getItem(int arg0) {
                // TODO Auto-generated method stub
                return list.get(arg0);
            }
    
            /**
             * Fragment的数量
             */
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return list.size();
            }
    
        }
    
    
    
    }
    

    MyFragment.java

    package com.qf.day20_viewpager_demo5.fragment;
    
    import com.qf.day20_viewpager_demo5.R;
    
    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 MyFragment extends Fragment {
    
        private TextView tv;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
    
            View view = inflater.inflate(R.layout.layout_fragment, container, false);
            tv  = (TextView) view.findViewById(R.id.tv);
    
            Bundle bundle = getArguments();
            int index = bundle.getInt("index");
    
            switch (index) {
            case 1:
                tv.setText("新闻");
                break;
            case 2:
                tv.setText("娱乐");
                break;
            case 3:
                tv.setText("段子");
                break;
    
            default:
                break;
            }
    
            return view;
        }
    
    }
    

    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:orientation="vertical"
        tools:context=".MainActivity" >
    
        <LinearLayout 
            android:id="@+id/ll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <TextView 
                android:id="@+id/tv_new"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="新闻"
                />
            <TextView 
                android:id="@+id/tv_happy"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                 android:gravity="center"
                android:text="娱乐"
                />
            <TextView 
                android:id="@+id/tv_dz"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                 android:gravity="center"
                android:text="段子"
                />
        </LinearLayout>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    
    </LinearLayout>
    

    layout_fragment.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="match_parent"
        android:orientation="vertical" >
    
        <TextView 
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textColor="#f00"
            />
    
    
    </LinearLayout>
    

    Demo6

    这里写图片描述

    MainActivity.java

    package com.qf.day20_viewpager_demo5;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import android.app.ActionBar;
    import android.app.ActionBar.Tab;
    import android.app.ActionBar.TabListener;
    import android.app.FragmentTransaction;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.support.v4.view.ViewPager.OnPageChangeListener;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    import com.qf.day20_viewpager_demo5.fragment.MyFragment;
    
    public class MainActivity extends FragmentActivity implements TabListener{
    
        private ViewPager viewPager;
    
        private List<Fragment> list;
    
        private TextView[]tvs;
    
        private ActionBar actionBar;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            viewPager = (ViewPager) findViewById(R.id.viewPager);
    
            actionBar = getActionBar();
    
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            actionBar.addTab(actionBar.newTab().setText("新闻").setTabListener(this));
            actionBar.addTab(actionBar.newTab().setText("娱乐").setTabListener(this));
            actionBar.addTab(actionBar.newTab().setText("段子").setTabListener(this));
    
            //数据源  Fragment
            initData();
            //adapter
            viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager()));
            viewPager.setOnPageChangeListener(new OnPageChangeListener() {
    
                @Override
                public void onPageSelected(int arg0) {
    //              // TODO Auto-generated method stub
    //              for(int i=0;i<3;i++){
    //                  tvs[i].setBackgroundColor(Color.BLUE);
    //              }
    //              
    //              tvs[arg0].setBackgroundColor(Color.RED);
    
                    //ViewPager绑定ActionBar   
                    actionBar.setSelectedNavigationItem(arg0);
                }
    
                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void onPageScrollStateChanged(int arg0) {
                    // TODO Auto-generated method stub
    
                }
            });
    
        }
    //  //导航书签
    //  public void initTab() {
    //      tvs = new TextView[3];
    //      LinearLayout layout = (LinearLayout) findViewById(R.id.ll);
    //      for(int i=0;i<3;i++){
    //          tvs[i] = (TextView) layout.getChildAt(i);
    //          tvs[i].setBackgroundColor(Color.BLUE);
    //          tvs[i].setTag(i);
    //          tvs[i].setOnClickListener(new OnClickListener() {
    //              
    //              @Override
    //              public void onClick(View v) {
    //                  // TODO Auto-generated method stub
    //                  viewPager.setCurrentItem((Integer)v.getTag());
    //              }
    //          });
    //      }
    //      tvs[0].setBackgroundColor(Color.RED);
    //  }
    
        public void initData() {
            list = new ArrayList<Fragment>();
    
            MyFragment myFragment1 = new MyFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("index", 1);
            myFragment1.setArguments(bundle);
    
            MyFragment myFragment2 = new MyFragment();
            Bundle bundle1 = new Bundle();
            bundle1.putInt("index", 2);
            myFragment2.setArguments(bundle1);
    
            MyFragment myFragment3 = new MyFragment();
            Bundle bundle2 = new Bundle();
            bundle2.putInt("index", 3);
            myFragment3.setArguments(bundle2);
    
            list.add(myFragment1);
            list.add(myFragment2);
            list.add(myFragment3);
        }
    
        /**
         * 自定义的Adapter
         * @author sxy
         *
         */
        public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
    
            public MyFragmentPagerAdapter(FragmentManager fm) {
                super(fm);
                // TODO Auto-generated constructor stub
            }
    
            /**
             * 根据指定的下标  返回对应的Fragmengt对象
             */
            @Override
            public Fragment getItem(int arg0) {
                // TODO Auto-generated method stub
                return list.get(arg0);
            }
    
            /**
             * Fragment的数量
             */
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return list.size();
            }
    
        }
    
        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
    
        }
        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
            //Tab绑定ViewPager
            viewPager.setCurrentItem(tab.getPosition());
        }
        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
    
        }
    
    
    
    }
    

    MyFragment.java

    package com.qf.day20_viewpager_demo5.fragment;
    
    import com.qf.day20_viewpager_demo5.R;
    
    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 MyFragment extends Fragment {
    
        private TextView tv;
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
    
            View view = inflater.inflate(R.layout.layout_fragment, container, false);
            tv  = (TextView) view.findViewById(R.id.tv);
    
            Bundle bundle = getArguments();
            int index = bundle.getInt("index");
    
            switch (index) {
            case 1:
                tv.setText("新闻");
                break;
            case 2:
                tv.setText("娱乐");
                break;
            case 3:
                tv.setText("段子");
                break;
    
            default:
                break;
            }
    
            return view;
        }
    
    }
    

    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:orientation="vertical"
        tools:context=".MainActivity" >
    
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    
    </LinearLayout>
    

    layout_fragment.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="match_parent"
        android:orientation="vertical" >
    
        <TextView 
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textColor="#f00"
            />
    
    
    </LinearLayout>
    
  • 相关阅读:
    「SAP技术」SAP MM MB5M报表不显示特殊库存数据
    python-day3-条件判断与循环
    python-day2-运算符
    Mysql数据库意外崩溃导致表数据文件损坏无法启动的问题解决
    图书管理系统(Servlet+Jsp+Java+Mysql,附下载演示地址)
    求解最长递增子序列(LIS) | 动态规划(DP)+ 二分法
    HTML+CSS+JavaScript实现植物大战僵尸(附演示地址)
    面试官:如何在Integer类型的ArrayList中同时添加String、Character、Boolean等类型的数据? | Java反射高级应用
    面试官:手撕十大排序算法,你会几种?
    用x种方式求第n项斐波那契数,99%的人只会第一种
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152213.html
Copyright © 2020-2023  润新知