android studio实现消息列表跳转

 

 

 

 

mainactivity:

package com.example.experiment3;

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.widget.ImageButton;
import android.widget.LinearLayout;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Fragment weixin = new weixin();
    private Fragment friends = new friends();
    private Fragment news =new news();
    private Fragment setting = new setting();
    private  FragmentManager fragmentManager;
    private LinearLayout linearLayout_weixin,linearLayout_friends,linearLayout_setting,linearLayout_news;
    private ImageButton weixin_button,friends_button,news_button,setting_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        linearLayout_weixin=findViewById(R.id.id_tab_weixin);
        linearLayout_friends=findViewById(R.id.id_tab_friends);
        linearLayout_setting=findViewById(R.id.id_tab_setting);
        linearLayout_news=findViewById(R.id.id_tab_news);

        weixin_button = findViewById(R.id.id_tab_weixin_img);
        friends_button = findViewById(R.id.id_tab_friends_img);
        news_button = findViewById(R.id.id_tab_news_img);
        setting_button = findViewById(R.id.id_tab_setting_img);

        linearLayout_weixin.setOnClickListener(this);
        linearLayout_friends.setOnClickListener(this);
        linearLayout_setting.setOnClickListener(this);
        linearLayout_news.setOnClickListener(this);

        init_Fragment();

    }

    private void init_Fragment(){
        fragmentManager =getFragmentManager();

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.Frame_container,weixin);
        fragmentTransaction.add(R.id.Frame_container,friends);
        fragmentTransaction.add(R.id.Frame_container,news);
        fragmentTransaction.add(R.id.Frame_container,setting);
        fragmentTransaction.commit();
        showFragment(0);
        news_button.setImageResource(R.drawable.img1);
    }

    private void hide_Fragment(FragmentTransaction fragmentTransaction)
    {
        fragmentTransaction.hide(weixin);
        fragmentTransaction.hide(friends);
        fragmentTransaction.hide(news);
        fragmentTransaction.hide(setting);
    }

    @Override
    public void onClick(View view) {
        resetButton();

        switch (view.getId()){
            case R.id.id_tab_weixin:
                showFragment(0);
                weixin_button.setImageResource(R.drawable.img1);
                break;
            case R.id.id_tab_friends:
                showFragment(1);
                friends_button.setImageResource(R.drawable.img2);
                break;
            case R.id.id_tab_news:
                showFragment(2);
                news_button.setImageResource(R.drawable.img3);
                break;
            case R.id.id_tab_setting:

                showFragment(3);
                setting_button.setImageResource(R.drawable.img4);
                break;
            default:
                break;

        }

    }

    private void showFragment(int i) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hide_Fragment(fragmentTransaction);
        switch (i){
            case 0:
                fragmentTransaction.show(weixin);
                break;
            case 1:
                fragmentTransaction.show(friends);
                break;
            case 2:
                fragmentTransaction.show(news);
                break;
            case 3 :
                fragmentTransaction.show(setting);
                break;
            default:
                break;
        }
        fragmentTransaction.commit();
    }
    private void resetButton(){
        weixin_button.setImageResource(R.drawable.img1);
        friends_button.setImageResource(R.drawable.img2);
        news_button.setImageResource(R.drawable.img3);
        setting_button.setImageResource(R.drawable.img4);
    }


}

Activity1:

package com.example.experiment3;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class Activity1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_1);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==31){
            String state=data.getStringExtra("state");
            Log.d("life",state);
        }

    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.d("life","activity1 is onStart...");
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        Log.d("life","activity1 is onPostResume...");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("life","activity1 is onReStart...");
    }
    @Override
    protected void onPause() {
        super.onPause();
        Log.d("life","activity1 is onPause...");
    }
    @Override
    protected void onStop() {
        super.onStop();
        Log.d("life","activity1 is onStop...");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("life","activity1 is onDestroy...");
    }
    @Override
    public void finish() {
        super.finish();
        Log.d("life","activity1 is finish...");
    }

    @Override
    public void finishActivity(int requestCode) {
        super.finishActivity(requestCode);
        Log.d("life","activity1 is finishActivity...");
    }

}

Activity2:

package com.example.experiment3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

public class Activity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);
    }


    @Override
    protected void onPostResume() {
        super.onPostResume();
        Log.d("life", "activity2 is onPostResume...");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d("life", "activity2 is onStart...");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("life", "activity2 is onReStart...");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("life", "activity2 is onStop...");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("life", "activity2 is onDestroy...");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("life", "activity2 is onPause...");
    }

    @Override
    public void finish() {
        super.finish();
        Log.d("life", "activity2 is finish...");
    }

    @Override
    public void finishActivity(int requestCode) {
        super.finishActivity(requestCode);
        Log.d("life", "activity2 is finishActivity...");
    }
}

activity1:

<?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:gravity="center"
    android:orientation="vertical"
    tools:context=".Activity1">

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="这是第一个activity"
        android:textSize="40sp" />

</LinearLayout>

activity2:

<?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"
    tools:context=".Activity2">

    <TextView
        android:id="@+id/textView5"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="详情消息内容"
        android:textSize="40dp" />
</LinearLayout>

仓库地址

  • 10
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值