package com.example.Cat.activity; import android.app.Activity; import android.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; import androidx.annotation.Nullable; import com.example.Cat.R; import com.example.Cat.fragment.IndexFragment; import com.example.Cat.fragment.PearsonFragment; import com.example.Cat.fragment.ProductFragment; import com.example.Cat.fragment.ShoppingCartFragment; public class IndexActivity extends Activity implements View.OnClickListener { private IndexFragment indexFragment; private ProductFragment productFragment; private ShoppingCartFragment shoppingCartFragment; private PearsonFragment pearsonFragment; String username; private LinearLayout indexLine, productLine, shoppingCartLine, pearsonLine; Intent intent_message; TextView usernameText; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_index); init(); initIndexFragment(); //setData(); // Thread thread = new Thread(runnable); //thread.start(); } /** * 组件初始化 */ private void init() { indexLine = findViewById(R.id.content_index); indexLine.setOnClickListener(this); productLine = findViewById(R.id.content_product); productLine.setOnClickListener(this); shoppingCartLine = findViewById(R.id.content_cart); shoppingCartLine.setOnClickListener(this); pearsonLine = findViewById(R.id.content_pearson); pearsonLine.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.content_index: initIndexFragment(); break; case R.id.content_product: initproductFragment(); break; case R.id.content_cart: initshoppingCartFragment(); break; case R.id.content_pearson: initpearsonFragment(); break; } } /** * 初始化首页Fragment */ private void initIndexFragment() { //开启事务,fragment的控制是由事务来实现的 FragmentTransaction transaction = getFragmentManager().beginTransaction(); if (indexFragment == null) { indexFragment = new IndexFragment(); } transaction.replace(R.id.main_content, indexFragment); transaction.commit(); } /** * 初始化产品Fragment */ private void initproductFragment() { //开启事务,fragment的控制是由事务来实现的 FragmentTransaction transaction = getFragmentManager().beginTransaction(); if (productFragment == null) { productFragment = new ProductFragment(); } transaction.replace(R.id.main_content, productFragment); transaction.commit(); } /** * 初始化购物车Fragment */ private void initshoppingCartFragment() { //开启事务,fragment的控制是由事务来实现的 FragmentTransaction transaction = getFragmentManager().beginTransaction(); if (shoppingCartFragment == null) { shoppingCartFragment = new ShoppingCartFragment(); } transaction.replace(R.id.main_content, shoppingCartFragment); transaction.commit(); } /** * 初始化个人Fragment */ private void initpearsonFragment() { //开启事务,fragment的控制是由事务来实现的 FragmentTransaction transaction = getFragmentManager().beginTransaction(); if (pearsonFragment == null) { Intent intent = IndexActivity.this.getIntent(); Bundle bundle = intent.getExtras(); pearsonFragment = new PearsonFragment(); pearsonFragment.setArguments(bundle); } usernameText=pearsonFragment.usernameText; transaction.replace(R.id.main_content, pearsonFragment); transaction.commit(); } // public Intent getIntentm(){ // intent_message=new Intent(this,UserMessageActivity.class); // return intent_message; // } // public void intentopration(){ // getIntentm().putExtra("username",usernameText.getText().toString()); // } }