布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/contentPanel" android:layout_above="@+id/fl_tab" android:layout_width="match_parent" android:layout_height="match_parent" /> <RelativeLayout android:id="@+id/fl_tab" android:layout_width="match_parent" android:layout_height="44dp" android:layout_alignParentBottom="true" android:background="#1f2531"> <Button android:id="@+id/btn_home_select" android:layout_width="58dp" android:layout_height="36dp" android:layout_centerInParent="true" android:background="@drawable/tab_home_select"/> <Button android:id="@+id/btn_home_left" android:layout_toLeftOf="@id/btn_home_select" android:layout_marginRight="67dp" android:layout_width="24dp" android:layout_height="24dp" android:layout_centerVertical="true" android:background="@drawable/ic_home_video_selected"/> <Button android:id="@+id/btn_home_right" android:layout_toRightOf="@id/btn_home_select" android:layout_marginLeft="67dp" android:layout_width="24dp" android:layout_height="24dp" android:layout_centerVertical="true" android:background="@drawable/ic_user_normal"/> </RelativeLayout> <!--<android.support.v4.app.FragmentTabHost--> <!--android:id="@android:id/tabhost"--> <!--android:layout_width="match_parent"--> <!--android:layout_height="wrap_content"--> <!--android:layout_gravity="bottom"--> <!--android:background="#1f2531"/>--> </RelativeLayout>
Java代码:
public class TCMainActivity extends FragmentActivity implements View.OnClickListener { private static final String TAG = "TCMainActivity"; private Button mBtnVideo, mBtnSelect, mBtnUser; private Fragment mCurrentFragment; private Fragment mTCLiveListFragment, mTCUserInfoFragment; private long mLastClickPubTS = 0; private ShortVideoDialog mShortVideoDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //显示第一个fragment showVideoFragment(); } private void initView() { mShortVideoDialog = new ShortVideoDialog(); mBtnVideo = (Button) findViewById(R.id.btn_home_left); mBtnSelect = (Button) findViewById(R.id.btn_home_select); mBtnUser = (Button) findViewById(R.id.btn_home_right); mBtnUser.setOnClickListener(this); mBtnVideo.setOnClickListener(this); mBtnSelect.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_home_left: showVideoFragment(); break; case R.id.btn_home_select: showSelect(); break; case R.id.btn_home_right: showUserFragment(); break; } } private void showSelect() { if (!TCUserMgr.getInstance().hasUser()) { Intent intent = new Intent(TCMainActivity.this, TCLoginActivity.class); startActivity(intent); } else { // 防止多次点击 if (System.currentTimeMillis() - mLastClickPubTS > 1000) { mLastClickPubTS = System.currentTimeMillis(); if (mShortVideoDialog.isAdded()) mShortVideoDialog.dismiss(); else mShortVideoDialog.show(getFragmentManager(), ""); } } } private void showUserFragment() { mBtnVideo.setBackgroundResource(R.drawable.ic_home_video_normal); mBtnUser.setBackgroundResource(R.drawable.ic_user_selected); if (mTCUserInfoFragment == null) { mTCUserInfoFragment = new TCUserInfoFragment(); } showFragment(mTCUserInfoFragment, "user_fragment"); } private void showVideoFragment() { mBtnVideo.setBackgroundResource(R.drawable.ic_home_video_selected); mBtnUser.setBackgroundResource(R.drawable.ic_user_normal); if (mTCLiveListFragment == null) { mTCLiveListFragment = new TCLiveListFragment(); } showFragment(mTCLiveListFragment, "live_list_fragment"); } //显示fragment,添加tag防止fragment重叠 private void showFragment(Fragment fragment, String tag) { if (fragment == mCurrentFragment) return; FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); if (mCurrentFragment != null) { transaction.hide(mCurrentFragment); } if (!fragment.isAdded()) { transaction.add(R.id.contentPanel, fragment, tag); } else { transaction.show(fragment); } mCurrentFragment = fragment; transaction.commit(); } }