public class MaintenanceRecordActivity extends BaseActivity implements View.OnClickListener {
private TextView tv_se_equipment;
private TextView tv_se_message;
private FrameLayout fl;
private RecordsFragment mRecordsFragment = null;
private MaintenanceRecordFragment mMaintenanceRecordFragment = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_maintenance_record);
initview();
addFragment(); //初始化fragment
tv_se_equipment.performClick();
}
private void initview() {
tv_se_equipment = (TextView) findViewById(R.id.tv_se_equipment);
tv_se_equipment.setOnClickListener(this);
tv_se_message = (TextView) findViewById(R.id.tv_se_message);
tv_se_message.setOnClickListener(this);
fl = (FrameLayout) findViewById(R.id.fl);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_se_equipment:
tv_se_equipment.setSelected(true);
tv_se_message.setSelected(false);
changFragment(0);
break;
case R.id.tv_se_message:
tv_se_equipment.setSelected(false);
tv_se_message.setSelected(true);
changFragment(1);
break;
}
}
List<Fragment> mFragmentList = new ArrayList<>();
private void addFragment() {
mFragmentList.add(new RecordsFragment());
mFragmentList.add(new MaintenanceRecordFragment());
}
private void changFragment(int position) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (position == 0) {
if (mRecordsFragment == null) {
mRecordsFragment = new RecordsFragment();
transaction.add(R.id.fl, mRecordsFragment, RecordsFragment.class.getSimpleName());
}
transaction.show(mRecordsFragment);
if (mMaintenanceRecordFragment != null) {
transaction.hide(mMaintenanceRecordFragment);
}
} else {
if (mMaintenanceRecordFragment == null) {
mMaintenanceRecordFragment = new MaintenanceRecordFragment();
transaction.add(R.id.fl, mMaintenanceRecordFragment, MaintenanceRecordFragment.class.getSimpleName());
}
transaction.show(mMaintenanceRecordFragment);
if (mRecordsFragment != null) {
transaction.hide(mRecordsFragment);
}
}
transaction.commitAllowingStateLoss();
}
}
<?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:background="#f2f2f2"
android:orientation="vertical"
tools:context="com.docom.magiccubeapp.activity.warden.MaintenanceRecordActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/mian_63"
android:background="#ffffff"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1">
<TextView
android:id="@+id/tv_se_equipment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:drawableLeft="@drawable/main_pic_selected"
android:drawablePadding="@dimen/main_8"
android:text="报修记录"
android:textColor="@drawable/main_equipment_selected"
android:textSize="@dimen/mian_20"/>
</RelativeLayout>
<View
android:layout_width="2dp"
android:layout_height="@dimen/mian_28"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/main_8"
android:layout_marginRight="@dimen/main_8"
android:background="#d1d6d8"/>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1">
<TextView
android:id="@+id/tv_se_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:drawableLeft="@drawable/main_pic_msg_selected"
android:drawablePadding="@dimen/main_8"
android:text="检修记录"
android:textColor="@drawable/main_equipment_selected"
android:textSize="@dimen/mian_20"/>
</RelativeLayout>
</LinearLayout>
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/main_13">
</FrameLayout>
</LinearLayout>
Fragment显示隐藏
最新推荐文章于 2023-03-04 19:23:05 发布