如何当我从片段刷卡其他做一个事件 [英] how to do an event when i swipe from fragment to the other

查看:322
本文介绍了如何当我从片段刷卡其他做一个事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是6个片段之间约轻扫我的code ,,我想要做一个事件时,我从一个片段刷卡其他从互联网加载一些东西,,所以请任何帮助,我该怎么办呢? ?? ,,和我在哪里可以把code,等我想想进度对话框启动它在功能获得冠军,并在片段的开始是它适用取消它?
 这是我的code:

 包com.example.my_finalswipe;
                 进口的java.util.List;
                 进口java.util.Locale中;
            进口java.util.Vector中;
            进口android.os.Bundle;
            进口android.support.v4.app.Fragment;
            进口android.support.v4.app.FragmentActivity;
            进口android.support.v4.app.FragmentManager;
            进口android.support.v4.app.FragmentPagerAdapter;
            进口android.support.v4.view.ViewPager;
            进口android.view.LayoutInflater;
            进口android.view.View;
            进口android.view.ViewGroup;             公共类MainActivity扩展FragmentActivity {
            SectionsPagerAdapter mSectionsPagerAdapter;
            ViewPager mViewPager;
            清单<片断>片段;            @覆盖
            保护无效的onCreate(捆绑savedInstanceState){
                super.onCreate(savedInstanceState);
                的setContentView(R.layout.activity_main);                //创建将返回一个片段为三个的适配器
                //应用的主要部分。
               碎片=新的矢量<片断>();                        fragments.add(Fragment.instantiate(在此,FragmentOne.class.getName()));
                        fragments.add(Fragment.instantiate(在此,FragmentTwo.class.getName()));
                        fragments.add(Fragment.instantiate(在此,FragmentThree.class.getName()));
                        fragments.add(Fragment.instantiate(在此,FragmentFour.class.getName()));
                        fragments.add(Fragment.instantiate(在此,FragmentFive.class.getName()));
                        fragments.add(Fragment.instantiate(在此,FragmentSix.class.getName()));            mSectionsPagerAdapter =新SectionsPagerAdapter(super.getSupportFragmentManager());                //设置的ViewPager与部分适配器。
                mViewPager =(ViewPager)findViewById(R.id.pager);
                mViewPager.setAdapter(mSectionsPagerAdapter);            //            }            公共类SectionsPagerAdapter扩展FragmentPagerAdapter {                公共SectionsPagerAdapter(FragmentManager FM){                    超(FM);
                }                        @覆盖
                        公共片段的getItem(INT位置){
                             INT _pos =位置%6;
                             返回fragments.get(_pos);                        }                @覆盖
                公众诠释的getCount(){
                    //显示3总页​​数。
                    返回6;
                }                @覆盖
                公共CharSequence的getPageTitle(INT位置){
                    最后弦乐title_section4 =Section4;
                    最后弦乐title_section5 =Section5;
                    最后弦乐title_section6 =Section6;
                    最后弦乐title_section1 =SECTION1;
                    最后弦乐title_section2 =第2节;
                    最后弦乐title_section3 =Section3中;                    区域设置L = Locale.getDefault();
                    开关(位置){
                    情况下0:
                        返回title_section1.toUpperCase(升);                    情况1:
                        返回title_section2.toUpperCase(升);                    案例2:
                        返回title_section3.toUpperCase(升);                    案例3:
                        返回title_section4.toUpperCase(升);
                    情况4:
                        返回title_section5.toUpperCase(升);
                    情况5:
                        返回title_section6.toUpperCase(升);                    }
                    返回null;
                }
            }
            公共静态类FragmentOne扩展片段{                公共静态最后弦乐ARG_SECTION_NUMBER =SECTION_NUMBER;                公共FragmentOne(){
                }                @覆盖
                公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
                    查看rootView = inflater.inflate(R.layout.one,集装箱,FALSE);                    返回rootView;
                }
            }
            公共静态类FragmentTwo扩展片段{                公共静态最后弦乐ARG_SECTION_NUMBER =SECTION_NUMBER;                公共FragmentTwo(){
                }                @覆盖
                公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
                    查看rootView = inflater.inflate(R.layout.two,集装箱,FALSE);                    返回rootView;
                }
            }
            公共静态类FragmentThree扩展片段{
                公共静态最后弦乐ARG_SECTION_NUMBER =SECTION_NUMBER;                公共FragmentThree(){
                }                @覆盖
                公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
                    查看rootView = inflater.inflate(R.layout.three,集装箱,FALSE);                    返回rootView;
                }
            }            公共静态类FragmentFour扩展片段{                公共静态最后弦乐ARG_SECTION_NUMBER =SECTION_NUMBER;                公共FragmentFour(){
                }                @覆盖
                公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
                    查看rootView = inflater.inflate(R.layout.four,集装箱,FALSE);
                    返回rootView;
                }
            }            公共静态类FragmentFive扩展片段{                公共静态最后弦乐ARG_SECTION_NUMBER =SECTION_NUMBER;                公共FragmentFive(){
                }                @覆盖
                公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
                    查看rootView = inflater.inflate(R.layout.five,集装箱,FALSE);
                   返回rootView;
                }
            }
            公共静态类FragmentSix扩展片段{                公共静态最后弦乐ARG_SECTION_NUMBER =SECTION_NUMBER;                公共FragmentSix(){
                }                @覆盖
                公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
                    查看rootView = inflater.inflate(R.layout.six,集装箱,FALSE);
                  返回rootView;
                }
            }
             }


解决方案

使用<一个href=\"http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html\">ViewPager.OnPageChangeListener你的看法寻呼机的检测滑动或实现自定义视图寻呼机看到

添加此

  mViewPager.setOnPageChangeListener(新OnPageChangeListener(){        @覆盖
        公共无效使用onPageSelected(INT为arg0){
            // TODO自动生成方法存根            //做事HERE        }        @覆盖
        公共无效onPageScrolled(INT为arg0,ARG1浮球,诠释ARG2){
            // TODO自动生成方法存根        }        @覆盖
        公共无效onPageScrollStateChanged(INT为arg0){
            // TODO自动生成方法存根        }
    });

this is my code about swipe between 6 fragments ,, i want to do an event when i swipe from one fragment to the other as loading some thing from internet ,, so please any help how can i do this ???,, and where can i put the code ,, i think about the progress dialog start it at function get title and cancel it at the beginning of the fragment is it applicable?? this is my code :

             package com.example.my_finalswipe;
                 import java.util.List;
                 import java.util.Locale;
            import java.util.Vector;
            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.view.LayoutInflater;
            import android.view.View;
            import android.view.ViewGroup;

             public class MainActivity extends FragmentActivity {


            SectionsPagerAdapter mSectionsPagerAdapter;


            ViewPager mViewPager;
            List<Fragment> fragments ;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                // Create the adapter that will return a fragment for each of the three
                // primary sections of the app.
               fragments = new Vector<Fragment>();

                        fragments.add(Fragment.instantiate(this, FragmentOne.class.getName()));
                        fragments.add(Fragment.instantiate(this, FragmentTwo.class.getName()));
                        fragments.add(Fragment.instantiate(this, FragmentThree.class.getName()));
                        fragments.add(Fragment.instantiate(this, FragmentFour.class.getName()));
                        fragments.add(Fragment.instantiate(this, FragmentFive.class.getName()));
                        fragments.add(Fragment.instantiate(this, FragmentSix.class.getName()));

            mSectionsPagerAdapter=new SectionsPagerAdapter(super.getSupportFragmentManager());

                // Set up the ViewPager with the sections adapter.
                mViewPager = (ViewPager) findViewById(R.id.pager);
                mViewPager.setAdapter(mSectionsPagerAdapter);

            //





            }





            public class SectionsPagerAdapter extends FragmentPagerAdapter {

                public SectionsPagerAdapter(FragmentManager fm) {

                    super(fm);
                }

                        @Override
                        public Fragment getItem(int position) {
                             int _pos = position % 6;
                             return fragments.get(_pos);

                        }

                @Override
                public int getCount() {
                    // Show 3 total pages.
                    return 6;
                }

                @Override
                public CharSequence getPageTitle(int position) {
                    final String title_section4="Section4";
                    final String title_section5="Section5";
                    final String title_section6="Section6";
                    final String title_section1="Section1";
                    final String title_section2="Section2";
                    final String title_section3="Section3";

                    Locale l = Locale.getDefault();
                    switch (position) {


                    case 0:
                        return title_section1.toUpperCase(l);

                    case 1:
                        return title_section2.toUpperCase(l);

                    case 2:
                        return title_section3.toUpperCase(l);

                    case 3:
                        return title_section4.toUpperCase(l);
                    case 4:
                        return title_section5.toUpperCase(l);
                    case 5:
                        return title_section6.toUpperCase(l);

                    }
                    return null;
                }
            }


            public static class FragmentOne extends Fragment {

                public static final String ARG_SECTION_NUMBER = "section_number";

                public FragmentOne() {
                }

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                    View rootView = inflater.inflate(R.layout.one, container, false);

                    return rootView;
                }
            }


            public static class FragmentTwo extends Fragment {

                public static final String ARG_SECTION_NUMBER = "section_number";

                public FragmentTwo() {
                }

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                    View rootView = inflater.inflate(R.layout.two, container, false);

                    return rootView;
                }
            }


            public static class FragmentThree extends Fragment {
                public static final String ARG_SECTION_NUMBER = "section_number";

                public FragmentThree() {
                }

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                    View rootView = inflater.inflate(R.layout.three, container, false);

                    return rootView;
                }
            }



            public static class FragmentFour extends Fragment {

                public static final String ARG_SECTION_NUMBER = "section_number";

                public FragmentFour() {
                }

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                    View rootView = inflater.inflate(R.layout.four, container, false);
                    return rootView;
                }
            }



            public static class FragmentFive extends Fragment {

                public static final String ARG_SECTION_NUMBER = "section_number";

                public FragmentFive() {
                }

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                    View rootView = inflater.inflate(R.layout.five, container, false);
                   return rootView;
                }
            }


            public static class FragmentSix extends Fragment {

                public static final String ARG_SECTION_NUMBER = "section_number";

                public FragmentSix() {
                }

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                    View rootView = inflater.inflate(R.layout.six, container, false);
                  return rootView;
                }
            }
             }

解决方案

Use ViewPager.OnPageChangeListener of your view pager to detect swipe OR implement custom view pager see this

ADD THIS

mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub

            //DO THINGS HERE

        }

        @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

        }
    });

这篇关于如何当我从片段刷卡其他做一个事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆