main_layout.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="com.mrzhao.examdemo1.MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v4.view.ViewPager> </LinearLayout>
fragmnet_first_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="match_parent" android:text="点击延迟10s发送广播" android:id="@+id/send_bt" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:textSize="30sp" android:layout_gravity="center" android:textStyle="bold" android:text="这是第一个Fragment" android:layout_height="wrap_content" /> </LinearLayout>
fragment_second_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:textSize="30sp" android:layout_gravity="center" android:textStyle="bold" android:text="这是第二个Fragment" android:layout_height="wrap_content" /> </LinearLayout>
fragment_third_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:textSize="30sp" android:layout_gravity="center" android:textStyle="bold" android:text="这是第二个Fragment" android:layout_height="wrap_content" /> </LinearLayout>
FirstFragment:
public class FirstFragment extends Fragment { private static MainActivity activity; //接受消息 private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); //10s后该方法执行 延迟10s发送广播 Intent intent = new Intent(); intent.setAction("myReceiver"); getActivity().sendBroadcast(intent); } }; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); //获取Activity 对象 activity = (MainActivity) getActivity(); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragmen_first_layout, container, false); view.findViewById(R.id.send_bt).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //点击延迟发送广播 handler.sendEmptyMessageDelayed(100, 10 * 1000); } }); return view; } /** * 内部类方式 */ public static class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //自动跳转到第三个Fragment activity.changePager(); //土司成功 Toast.makeText(context, "成功", Toast.LENGTH_SHORT).show(); //创建一个数据库 MySQLiteHelper hel = new MySQLiteHelper(activity); } } }
SecondFragment:
public class SecondFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragmen_second_layout, container, false); return view; } }
ThirdFragment:
public class ThirdFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragmen_third_layout, container, false); return view; } }
适配器:
public class MyFragmentAdapter extends FragmentPagerAdapter { private List<Fragment> list; public MyFragmentAdapter(FragmentManager fm, List<Fragment> list) { super(fm); this.list = list; } @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } }
创建数据库
public class MySQLiteHelper extends SQLiteOpenHelper { public MySQLiteHelper(Context context) { super(context, "person.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table if not exists person(_id integer primary key autoincrement,name varchar(10),age integer(10))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
MainActivity:
public class MainActivity extends AppCompatActivity { private ViewPager viewPager; private List<Fragment> list = new ArrayList<>(); private FirstFragment firstFragment; private SecondFragment secondFragment; private ThirdFragment thirdFragment; private MyFragmentAdapter myFragmentAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = (ViewPager) findViewById(R.id.viewPager); //创建三个Fragment页面 firstFragment = new FirstFragment(); secondFragment = new SecondFragment(); thirdFragment = new ThirdFragment(); //添加到集合中 list.add(firstFragment); list.add(secondFragment); list.add(thirdFragment); //创建适配器 myFragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager(),list); //设置适配器 viewPager.setAdapter(myFragmentAdapter); //添加监听事件 viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { //页面切换弹出土司 Toast.makeText(MainActivity.this, "我是第"+(position+1)+"个Fragment", Toast.LENGTH_SHORT).show(); } @Override public void onPageScrollStateChanged(int state) { } }); } //修改ViwPager的页卡显示第三页 public void changePager(){ //修改 viewPager.setCurrentItem(2); } }
清单文件中的注册:
<receiver android:name=".FirstFragment$MyReceiver"> <intent-filter> <action android:name="myReceiver"></action> </intent-filter> </receiver>