Commit 34974e57 authored by Administrator's avatar Administrator

主页

parent 9951b4cf
package com.virjar.echo.adr.ui;
import androidx.fragment.app.Fragment;
public class LogFragment extends Fragment {
private String fragmentText;
public LogFragment(String fragmentText) {
this.fragmentText = fragmentText;
}
}
......@@ -3,24 +3,108 @@ package com.virjar.echo.adr.ui;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.virjar.echo.adr.HttpProxyService;
import com.virjar.echo.adr.R;
public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static void go(Context context) {
context.startActivity(new Intent(context, MainActivity.class));
}
LinearLayout logLinear;
LinearLayout settingLinear;
LinearLayout userLinear;
private FragmentManager mfragmentManger;
private LogFragment logFragment;
private SettingsActivity.SettingsFragment settingsFragment;
private MineFragment mineFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logLinear = findViewById(R.id.main_log);
settingLinear = findViewById(R.id.main_setting);
userLinear = findViewById(R.id.linear_user);
mfragmentManger = getSupportFragmentManager();
logLinear.performClick();
startService(new Intent(this, HttpProxyService.class));
}
@Override
public void onClick(View view) {
FragmentTransaction fragmentTransaction = mfragmentManger.beginTransaction();//只能是局部变量,不能为全局变量,否则不能重复commit
//FragmentTransaction只能使用一次
hideAllFragment(fragmentTransaction);
switch (view.getId()) {
case R.id.main_log:
setAllFalse();
logLinear.setSelected(true);
if (logFragment == null) {
logFragment = new LogFragment("Log");
fragmentTransaction.add(R.id.fragment_frame, logFragment);
} else {
fragmentTransaction.show(logFragment);
}
break;
case R.id.main_setting:
setAllFalse();
settingLinear.setSelected(true);
if (settingsFragment == null) {
settingsFragment = new SettingsActivity.SettingsFragment();
fragmentTransaction.add(R.id.fragment_frame, settingsFragment);
} else {
fragmentTransaction.show(settingsFragment);
}
break;
case R.id.linear_user:
setAllFalse();
userLinear.setSelected(true);
if (mineFragment == null) {
mineFragment = new MineFragment();
fragmentTransaction.add(R.id.fragment_frame, mineFragment);
} else {
fragmentTransaction.show(mineFragment);
}
break;
}
fragmentTransaction.commit();//记得必须要commit,否则没有效果
}
private void hideAllFragment(FragmentTransaction fragmentTransaction) {
if (logFragment != null) {
fragmentTransaction.hide(logFragment);
}
if (settingsFragment != null) {
fragmentTransaction.hide(settingsFragment);
}
if (mineFragment != null) {
fragmentTransaction.hide(mineFragment);
}
}
private void setAllFalse() {
logLinear.setSelected(false);
settingLinear.setSelected(false);
userLinear.setSelected(false);
}
}
package com.virjar.echo.adr.ui;
import androidx.fragment.app.Fragment;
public class MineFragment extends Fragment {
}
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/comment1"/>
<item android:drawable="@drawable/comment2"/>
</selector>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/contrast1"/>
<item android:drawable="@drawable/contrast"/>
</selector>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/home3"/>
<item android:drawable="@drawable/home31"/>
</selector>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/location41"/>
<item android:drawable="@drawable/location4"/>
</selector>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="@color/colorAccent"/>
<item android:color="@android:color/white"/>
</selector>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<TextView
android:layout_width="wrap_content"
<FrameLayout
android:id="@+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/tab_linear">
</FrameLayout>
<LinearLayout
android:id="@+id/tab_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
android:orientation="horizontal">
<LinearLayout
android:onClick="onClick"
android:id="@+id/main_log"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="@drawable/home" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="日志"
android:textColor="@drawable/text_color_back" />
</LinearLayout>
<LinearLayout
android:onClick="onClick"
android:id="@+id/main_setting"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="@drawable/location_view" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="设置"
android:textColor="@drawable/text_color_back" />
</LinearLayout>
<LinearLayout
android:onClick="onClick"
android:id="@+id/linear_user"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="@drawable/contrast_view" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="我的"
android:textColor="@drawable/text_color_back" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
</RelativeLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment