java模拟滑动事件_java - 当我从片段滑动到另一个时如何做一个事件

这是我写的关于在6个碎片之间进行滑动的代码,我想在从一个碎片到另一个碎片进行滑动时执行一个事件,以便从Internet加载某些内容,请帮助我如何执行此操作???,我可以把代码放在哪里,我会考虑进度对话框,从函数get title开始,在片段的开头取消它,它是否适用??

这是我的代码: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 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();

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;

}

}

}

最佳答案

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

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值