片段不会隐藏

时间:2018-07-16 15:29:45

标签: java android

我有一个导航栏,应该在按下按钮时将其隐藏,但是什么也没发生。

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private TextView mavisTxt;
private Button testBtn;
private Fragment navigationBar;


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

}

private void initVariables() {
    navigationBar = new HeaderNav();
    mavisTxt = (TextView)findViewById(R.id.mavisTxt);
    testBtn = (Button)findViewById(R.id.testBtn);


    testBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                FragmentManager fm = getSupportFragmentManager();
                fm.beginTransaction()
                        .hide(navigationBar)
                        .commit();
                mavisTxt.setText("navbar is hidden");
        }
    });

}
}

这是片段的Java文件

public class HeaderNav extends Fragment {
    private static Button homeBtn, optionsBtn, connectionBtn, micBtn, aboutBtn;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.navbar, container, false);

        homeBtn = (Button)view.findViewById(R.id.homeBtn);
        optionsBtn = (Button)view.findViewById(R.id.optionsBtn);
        connectionBtn = (Button)view.findViewById(R.id.micBtn);
        micBtn = (Button)view.findViewById(R.id.homeBtn);
        aboutBtn = (Button)view.findViewById(R.id.aboutBtn);

        return view;
    }
}

和main_activity xml中的fragment标签:

<fragment
        android:id="@+id/navigationBar"
        android:name="com.example.egi.mavisme.HeaderNav"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout="@layout/navbar" />

我在做什么错? SO中的所有解决方案都不适合我。谁来帮帮我。

编辑:

我已经编辑了代码,现在隐藏了。添加自定义动画时出现另一个问题,我得到了

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.startViewTransition(android.view.View)' on a null object reference

这是我的代码:

public class MainActivity extends AppCompatActivity {
    private TextView mavisTxt;
    private Button testBtn;
    private HeaderNav navigationBar;

    private FragmentManager fm;
    private FragmentTransaction ft;


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

    }

    private void initVariables() {
        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        navigationBar = (HeaderNav)fm.findFragmentById(R.id.navigationBar);

        mavisTxt = (TextView)findViewById(R.id.mavisTxt);
        testBtn = (Button)findViewById(R.id.testBtn);


        testBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
                    ft.hide(navigationBar)
                    .commit();
                    if(navigationBar.isHidden()) {mavisTxt.setText("navbar is hidden");}
            }
        });

    }
}

1 个答案:

答案 0 :(得分:0)

这里有一些事情要注意-

  1. 使用ActionBar/ToolBar,这不是iOS
  2. 您必须先添加一个片段
  3. 您正在使用show call而不是将其隐藏

    testBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    FragmentManager fm = getSupportFragmentManager();
                    fm.beginTransaction()
                            .**show**(navigationBar)
                            .commit();
                    mavisTxt.setText("navbar is hidden");
            }
        });