记录一个fragment切换问题(暂时解决)

采用fragment进行页面切换,仿照《零基础学安卓》5.4.3-实例-04写了一个仿微信底边切换。

修改前源码:

下面是main_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.studenttimemanage.MainActivity">
    <!--Fragment组件-->
    <fragment
        android:id="@+id/fragment"
        android:name="com.example.studenttimemanage.fragment.TimeRecord"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">
        <!--微信图标-->
        <ImageView
            android:id="@+id/TimeRecord"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@drawable/bottom_1" />
        <!--通讯录图标-->
        <ImageView
            android:id="@+id/TypeManage"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@drawable/bottom_2" />
        <!--发现图标-->
        <ImageView
            android:id="@+id/image3"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@drawable/bottom_3" />
        <!--我图标-->
        <ImageView
            android:id="@+id/image4"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@drawable/bottom_4" />

    </LinearLayout>

</RelativeLayout>

暂时只修改了前面两个页面,更改了图片view名字,在后面进行引用。

MainActivity.java如下:

package com.example.studenttimemanage;


import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.example.studenttimemanage.fragment.TimeRecord;
import com.example.studenttimemanage.fragment.TypeManage;

public class MainActivity extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取布局文件的第一个导航图片
        ImageView imageView1 = (ImageView) findViewById(R.id.TimeRecord);
        //获取布局文件的第二个导航图片
        ImageView imageView2 = (ImageView) findViewById(R.id.TypeManage);
        //获取布局文件的第三个导航图片
        ImageView imageView3 = (ImageView) findViewById(R.id.image3);
        //获取布局文件的第四个导航图片
        ImageView imageView4 = (ImageView) findViewById(R.id.image4);
        imageView1.setOnClickListener(l);//为第一个导航图片添加单机事件
        imageView2.setOnClickListener(l);//为第二个导航图片添加单机事件
        imageView3.setOnClickListener(l);//为第三个导航图片添加单机事件
        imageView4.setOnClickListener(l);//为第四个导航图片添加单机事件


    }


    //创建单机事件监听器
    View.OnClickListener l = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fm = getFragmentManager();   // 获取Fragment
            FragmentTransaction ft = fm.beginTransaction(); // 开启一个事务
            Fragment f = null; //为Fragment初始化
            switch (v.getId()) {    //通过获取点击的id判断点击了哪个张图片
                case R.id.TimeRecord:
                    f = new TimeRecord(); //创建第一个Fragment
                    break;
                case R.id.TypeManage:
                    f = new TypeManage() ;
                    break;
                default:
                    break;
            }
            ft.replace(R.id.fragment, f); //替换Fragment
            ft.commit(); //提交事务
        }
    };


}


使用了replace的方法,主要是不太会如何修改成add()和hide(),尝试过之后还是放弃了。

出现的问题:

点击下边图片view切换时,初始界面不变,新界面出现在初始界面下方。

没有发生重叠现象,并且下面fragment切换正常。

问题解决方法(不完善):

在网上找了很多,大多是解决重叠问题的。

推测这个问题并不像是fragment切换产生的,更像是初始界面没有正确消失的问题,不知道是不是哪里的代码有错误。

最后发现了一个方法

将avtivity_main.xml中的fragment

    <fragment
        android:id="@+id/fragment"
        android:name="com.example.studenttimemanage.fragment.TimeRecord"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

修改为FrameLayout

 <FrameLayout
        android:id="@+id/fragment"
        android:name="com.example.studenttimemanage.fragment.TimeRecord"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

页面上下叠加问题确实消失了,但是没有了初始页面。

(对毕设来说凑合能用,等做完毕设回来再研究一下)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 创建项目:新建一个Android项目,选择Empty Activity模板。 2. 添加Fragment:右键点击app目录,New -> Fragment -> Fragment(Blank)。创建两个Fragment,分别为Fragment1和Fragment2。 3. 编写布局文件:在res/layout文件夹下创建activity_main.xml文件,添加一个FrameLayout作为容器,用于切换Fragment。 ``` <?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" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btn_fragment1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment1" android:layout_weight="1"/> <Button android:id="@+id/btn_fragment2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fragment2" android:layout_weight="1"/> </LinearLayout> </LinearLayout> ``` 4. 编写Fragment:分别在Fragment1和Fragment2的布局文件中添加一个TextView,用于区分两个Fragment。 ``` <!--fragment1.xml--> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="Fragment1" android:gravity="center"/> <!--fragment2.xml--> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="Fragment2" android:gravity="center"/> ``` 5. MainActivity中添加Fragment:在MainActivity中使用FragmentManager添加Fragment1。 ``` public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Fragment1 fragment1; private Fragment2 fragment2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragment1 = new Fragment1(); fragment2 = new Fragment2(); getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, fragment1).commit(); findViewById(R.id.btn_fragment1).setOnClickListener(this); findViewById(R.id.btn_fragment2).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_fragment1: getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment1).commit(); break; case R.id.btn_fragment2: getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment2).commit(); break; } } } ``` 6. 运行程序:点击按钮可以切换Fragment1和Fragment2。 注:以上代码仅供参考,可能存在语法错误或者逻辑问题

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值