fragment事物隐藏显示
package com.example.works; import java.util.ArrayList; import java.util.List; import android.content.Intent; 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.app.FragmentTransaction; 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.Button; import android.widget.TextView; import com.baidu.mapapi.SDKInitializer; import com.example.fragment.ContentFragment01; import com.example.fragment.ContentFragment02; import com.example.fragment.ContentFragment03; public class MainActivity extends FragmentActivity implements OnClickListener{ private TextView home; private TextView jie; private TextView ren; private FragmentManager fragmentManager; private ContentFragment01 f1; private ContentFragment02 f2; private ContentFragment03 f3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); home = (TextView) findViewById(R.id.home); jie = (TextView) findViewById(R.id.jie); ren = (TextView) findViewById(R.id.ren); fragmentManager = getSupportFragmentManager(); //得到事务对象fragmentTransaction FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); f1 = new ContentFragment01(); f2 = new ContentFragment02(); f3 = new ContentFragment03(); fragmentTransaction.add(R.id.frame, f1, "f1"); fragmentTransaction.add(R.id.frame, f2, "f2"); fragmentTransaction.add(R.id.frame, f3, "f3"); //隐藏指定的fragment fragmentTransaction.hide(f3).hide(f2).show(f1); //提交事务 fragmentTransaction.commit(); home.setOnClickListener(this); jie.setOnClickListener(this); ren.setOnClickListener(this); setbuttoncolor(R.id.home); } @SuppressWarnings("unused") private void setbuttoncolor(int in){ switch (in) { case R.id.home: home.setTextColor(Color.RED); jie.setBackgroundResource(R.drawable.u31); ren.setTextColor(Color.BLACK); break; case R.id.jie: home.setTextColor(Color.BLACK); jie.setBackgroundResource(R.drawable.u49); ren.setTextColor(Color.BLACK); break; case R.id.ren: home.setTextColor(Color.BLACK); jie.setBackgroundResource(R.drawable.u31); ren.setTextColor(Color.RED); break; } } public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.home: setbuttoncolor(R.id.home); FragmentTransaction transaction1=fragmentManager.beginTransaction(); transaction1.hide(f3).hide(f2).show(f1); //提交事务 transaction1.commit(); break; case R.id.jie: setbuttoncolor(R.id.jie); FragmentTransaction transaction2=fragmentManager.beginTransaction(); transaction2.hide(f3).hide(f1).show(f2); //提交事务 transaction2.commit(); break; case R.id.ren: setbuttoncolor(R.id.ren); FragmentTransaction transaction3=fragmentManager.beginTransaction(); transaction3.hide(f1).hide(f2).show(f3); //提交事务 transaction3.commit(); break; } } }
<FrameLayout android:id="@+id/frame" android:layout_width="match_parent" android:layout_height="0dp" android:background="#f00" android:layout_weight="8" android:orientation="horizontal" > </FrameLayout>