1.主要代码
改进后的界面为:
2.掌握复杂控件与adapter的使用。(请使用RecycleView进行设计)
MainActivity
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
??? private Fragment weixinAdventure = new weixinAdventure();
??? private Fragment weixinCommitment = new weixinCommitment();
??? private Fragment weixinForum = new weixinForum();
??? private Fragment weixinLevel = new weixinLevel();
??? private FragmentManager fragmentManager;
??? private View LinearLayout1,LinearLayout2,LinearLayout3,LinearLayout4;
??? private TextView textView1,textView2,textView3,textView4;
??? @Override
??? protected void onCreate(Bundle savedInstanceState) {
??????? super.onCreate(savedInstanceState);
??????? supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
??????? setContentView(R.layout.activity_main);
??????? LinearLayout1 = findViewById(R.id.LinearLayout_weixinAdventure);
??????? LinearLayout2 = findViewById(R.id.LinearLayout_weixinCommitment);
??????? LinearLayout3 = findViewById(R.id.LinearLayout_weixinForum);
??????? LinearLayout4 = findViewById(R.id.LinearLayout_weixinLevel);
??????? textView1 = findViewById(R.id.textView1);
??????? textView2 = findViewById(R.id.textView2);
??????? textView3 = findViewById(R.id.textView3);
??????? textView4 = findViewById(R.id.textView4);
??????? LinearLayout1.setOnClickListener(this);
??????? LinearLayout2.setOnClickListener(this);
??????? LinearLayout3.setOnClickListener(this);
??????? LinearLayout4.setOnClickListener(this);
??????? textView1.setOnClickListener(this);
??????? textView2.setOnClickListener(this);
??????? textView3.setOnClickListener(this);
??????? textView4.setOnClickListener(this);
??????? initFragment();
??????? showFragment(0);
??? }
??? private void initFragment(){
??????? fragmentManager = getFragmentManager();
??????? FragmentTransaction transaction = fragmentManager.beginTransaction();
??????? transaction.add(R.id.content,weixinAdventure);
??????? transaction.add(R.id.content,weixinCommitment);
??????? transaction.add(R.id.content, weixinForum);
??????? transaction.add(R.id.content, weixinLevel);
??????? transaction.commit();
??? }
??? public void hideFragment(FragmentTransaction transaction){
??????? transaction.hide(weixinAdventure);
??????? transaction.hide(weixinCommitment);
? ??????transaction.hide(weixinForum);
??????? transaction.hide(weixinLevel);
??? }
??? private void showFragment(int i){
??????? FragmentTransaction transaction = fragmentManager.beginTransaction();
??????? hideFragment(transaction);
??????? switch (i){
??????????? case 0:
??????????????? transaction.show(weixinAdventure);
??????????????? textView1.setText("冒险");
??????????????? break;
??????????? case 1:
??????????????? transaction.show(weixinCommitment);
??????????????? textView1.setText("委托");
??????? ????????break;
??????????? case 2:
??????????????? transaction.show(weixinForum);
??????????????? textView1.setText("论坛");
??????????????? break;
??????????? case 3:
??????????????? transaction.show(weixinLevel);
??????????????? textView1.setText("能力");
? ??????????????break;
??????????? default:
??????????????? break;
??????? }
??????? transaction.commit();
??? }
??? @Override
??? public void onClick(View v) {
??????? switch (v.getId()){
??????????? case R.id.LinearLayout_weixinAdventure:
??????????????? showFragment(0);
??????????????? break;
??????????? case R.id.LinearLayout_weixinCommitment:
??????????????? showFragment(1);
??????????????? break;
??????????? case R.id.LinearLayout_weixinForum:
??????????????? showFragment(2);
??????????????? break;
?? ?????????case R.id.LinearLayout_weixinLevel:
??????????????? showFragment(3);
??????????????? break;
??????????? default:
??????????????? break;
??????? }
??? }
}
item.xml
<?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="wrap_content"
??? android:orientation="vertical">
?? ?<include layout="@layout/layout_sticky_header_view" />
??? <LinearLayout
??????? android:id="@+id/LinearLayoutWechatItem"
??????? android:layout_width="match_parent"
??????? android:layout_height="wrap_content"
??????? android:layout_marginBottom="5sp"
??????? android:layout_weight="1"
??????? android:background="#666699"
??????? android:gravity="center"
??????? android:orientation="horizontal"
??????? app:layout_constraintEnd_toEndOf="parent"
??????? app:layout_constraintStart_toStartOf="parent">
????? ??<LinearLayout
??????????? android:layout_width="300sp"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="left"
??????????? android:orientation="vertical">
??????????? <TextView
????????? ??????android:id="@+id/wechat_name_text"
??????????????? android:layout_width="match_parent"
??????????????? android:layout_height="match_parent"
??????????????? android:layout_weight="1"
??????????????? android:text="wechatName"
??????????????? android:textSize="25sp" />
??????????? <TextView
??????????????? android:id="@+id/wechat_content_validity"
??????????????? android:layout_width="match_parent"
??????????????? android:layout_height="match_parent"
??????????????? android:layout_weight="1"
?????????? ?????android:text="content validity"
??????????????? android:textSize="16sp" />
??????? </LinearLayout>
??????? <TextView
??????????? android:id="@+id/wechat_text_time"
??????????? android:layout_width="60sp"
??????????? android:layout_height="match_parent"
??????????? android:layout_weight="1"
??????????? android:gravity="center"
??????????? android:textSize="16sp" />
??? </LinearLayout>
</LinearLayout>
activity_contacts_details.xml
<?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:orientation="vertical"
tools:context=".ContactsDetails">
<TextView
android:id="@+id/wechat_contacts_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="委托详情"
android:textSize="36sp" />
</LinearLayout>
ContactsDetails
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ContactsDetails extends AppCompatActivity {
private TextView wechat_contacts_details;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts_details);
Log.d("life","activity_contacts_details is creating...");
Intent intent = getIntent();
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age", 18);
Log.d("life","name:"+name+",age:"+age);
wechat_contacts_details = findViewById(R.id.wechat_contacts_details);
wechat_contacts_details.setText("委托完成后记得领取药剂哦~");
}
@Override
protected void onStart() {
super.onStart();
Log.d("life","activity_contacts_details is starting...");
}
@Override
protected void onStop() {
super.onStop();
Log.d("life","activity_contacts_details is stopping...");
}
@Override
public void finish() {
super.finish();
Log.d("life","activity_contacts_details is finishing...");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("life","activity_contacts_details is destroying...");
}
@Override
public void finishActivity(int requestCode) {
super.finishActivity(requestCode);
Log.d("life","activity_contacts_details is finishActivity...");
}
}
代码仓库:https://github.com/aedela/My_Application3
- 结果截图
??
?
?
?
?
?
|