介绍
本篇文章给大家分享的是有关使用片段怎么实现界面切换效果,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
, MainActivity。java:
package com.crazy.gemi; , import android.app.SearchManager; import android.content.Intent; import android.graphics.Color; import android.provider.SearchRecentSuggestions; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.widget.TextView; , import com.crazy.gemi.ui.cheaper.CheaperFragment; import com.crazy.gemi.ui.cheaper.SearchSuggestionSampleProvider; import com.crazy.gemi.ui.favor.FavorFragment; import com.crazy.gemi.ui.more.MoreFragment; import com.crazy.gemi.ui.near.NearFragment; import com.crazy.gemi.ui.pocket.PocketFragment; , public class  MainActivity extends  FragmentActivity ,,,implements View.OnClickListener, CheaperFragment.SearchResult { , private 才能TextView [], textView =, new TextView [5]; private 才能查看[],views =, new 观点[5];//才能,其中的,firstFragment 相当于是个中间变量 private 才能Fragment firstFragment, nearFragment,, cheaperFragment,, favorFragment,, pocketFragmnet,, moreFragment; , @Override才能 protected 才能;void  onCreate (Bundle savedInstanceState), { ,,,super.onCreate (savedInstanceState); ,,,setContentView (R.layout.activity_main); , ,,,init (); ,,,initFragment (); ,,} , private 才能;void  init (), { , ,,,textView [0],=, (textView) findViewById (R.id.near); ,,,textView [1],=, (textView) findViewById (R.id.search_cheaper); ,,,textView [2],=, (textView) findViewById (R.id.favor); ,,,textView [3],=, (textView) findViewById (R.id.pocket); ,,,textView [4],=, (textView) findViewById (R.id.more); , ,,,的观点[0],=,findViewById (R.id.near_top_line); ,,,的观点[1],=,findViewById (R.id.cheaper_top_line); ,,,的观点[2],=,findViewById (R.id.favor_top_line); ,,,的观点[3],=,findViewById (R.id.pocket_top_line); ,,,的观点[4],=,findViewById (R.id.more_top_line); , ,,,textView [0] .setOnClickListener(这个); ,,,textView [1] .setOnClickListener(这个); ,,,textView [2] .setOnClickListener(这个); ,,,textView [3] .setOnClickListener(这个); ,,,textView [4] .setOnClickListener(这个); , ,,} , private 才能;void  initFragment (), { ,,,firstFragment =, FavorFragment.newInstance (); ,,,favorFragment =, firstFragment; ,,,//,最先加载的,片段 ,,,getSupportFragmentManager () .beginTransaction ()。 ,,,,,,,添加(R.id.frame_layout, favorFragment) .commit (); ,,,textView [2] .setTextColor (Color.BLACK); ,,,的观点[2].setBackgroundColor (Color.parseColor (“# FF6600")); ,,} , @Override才能 public 才能;void  onClick (View v), { ,,,switch (v.getId ()), { ,,,,,case R.id.near://,,,,,,,,getSupportFragmentManager () .beginTransaction ()。//,,,,,,,,,,,,取代(R.id.frame_layout, NearFragment.newInstance ()) .commit (); , ,,,,,,,如果(nearFragment==null) { ,,,,,,,,,nearFragment=, NearFragment.newInstance (); ,,,,,,,} ,,,,,,,switchContent (firstFragment, nearFragment,, getSupportFragmentManager () .beginTransaction ()); ,,,,,,,firstFragment =, nearFragment; , ,,,,,,,selectStringAndBackgroundColor (0); ,,,,,,,休息; ,,,,,case R.id.search_cheaper: ,,,,,,,如果(cheaperFragment==null) { ,,,,,,,,,cheaperFragment=, CheaperFragment.newInstance (); ,,,,,,,} null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null使用片段怎么实现界面切换效果