如何在Android中显示/隐藏静态片段

时间:2019-04-11 20:28:00

标签: android android-fragments

我知道StackOverflow上有很多关于我的问题的答案,但是由于我的Fragments是静态的,因此它们都无法解决我的问题。

我的活动中有两个片段。第二个覆盖第一个,这是将显示/隐藏的片段。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".FeedActivity">

    <fragment
            android:id="@+id/listViewFragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:name="com.me.cscomponents.fragFeed.FeedFragment"
            tools:layout="@layout/fragment_feed"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@id/gh1"
            app:layout_constraintBottom_toBottomOf="parent"/>

    <fragment
            android:id="@+id/playerViewFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.me.cscomponents.fragPlayer.PlayerFragment"
            tools:layout="@layout/fragment_player"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@id/gh1"
            app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

在TV应用程序中,两个片段并排可见。但是,在移动应用中,默认情况下listViewFragment是可见的,当用户单击playerViewFragment中的项目时,listViewFragment会显示。当用户单击“后退”按钮时,该视图将变为隐藏状态(因此listViewFragment将变为可见)。

这是我的代码(但无法正常运行):

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_feed)

        listFragment = supportFragmentManager.findFragmentById(R.id.listViewFragment) as FeedFragment
        playerFragment = supportFragmentManager.findFragmentById(R.id.playerViewFragment) as PlayerFragment

        val d1: Disposable = listFragment.onClickSubject
            .subscribe {
                showOrHidePlayerFragment(true)
                playerFragment.streamCamera(it.camFeedUrl)
            }
        cd.add(d1)

        showOrHidePlayerFragment(false)
    }

    private fun showOrHidePlayerFragment(isVisible: Boolean) {
        val fm = supportFragmentManager.beginTransaction()
        if (isVisible) fm.show(playerFragment) else fm.hide(playerFragment)
        fm.commit()
    }

问题是playerFragment在启动活动时仍然可见。因此,我不知道如何解决该问题以及导致该问题的原因。

我当前的解决方法是将playerFragment包围在FrameLayout中,并使该视图可见/不可见。这种方法很好用。

2 个答案:

答案 0 :(得分:0)

标签fragment有一个ID,Fragment根视图将具有该ID。

所以会

findViewById(R.id.oneFragment).setVisibility(View.GONE);

例如,如果FragmentOne使用以下布局:

<LinearLayout>
    <!--Chile Viiews-->
</LinearLayout>

您正在使用标签片段,如下所示:

<fragment
id=oneFragment
name=FragmentOne/>

然后,线性布局最终将以oneFragment作为id。这就是标签片段附加标签后将标签片段ID分配给片段的根视图的方式。您可以阅读更多here

执行此操作的另一种方法是使用片段管理器,这里是单行代码:

getSupportFragmentManager().findFragmentById(R.id.fragment).getView().setVisibility(View.GONE);

答案 1 :(得分:0)

我认为您的编码几乎没有出错。尽量避免像

这样的硬编码片段
android:name="com.me.cscomponents.fragFeed.FeedFragment"

根据您的问题,我为您准备了一些东西。我只是实现了两个按钮的点击,而不是发布列表。传递了一个int(您可以根据需要传递。)。我尝试遵循一些最佳做法。为简单起见,我使用LinearLayout和RelativeLayout。

activity_test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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=".TestActivity">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

TestActivity.java

import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class TestActivity extends AppCompatActivity implements FeedFragment.OnItemClickedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, FeedFragment.getInstance(), FeedFragment.TAG).commit();
    }

    @Override
    public void onButtonClicked(int position) {
        getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fragment_container, PlayerFragment.getInstance(position), PlayerFragment.TAG).commit();
    }

    @Override
    public void onAttachFragment(Fragment fragment) {
        if (fragment instanceof FeedFragment) {
            FeedFragment feedFragment = (FeedFragment) fragment;
            feedFragment.setOnButtonClickedListener(this);
        }
    }
}

fragment_feed.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/button_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Feed 1" />
    <Button
        android:id="@+id/button_two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Feed 2"/>
</LinearLayout>

FeedFragment.java

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class FeedFragment extends Fragment {
    public static final String TAG = FeedFragment.class.getSimpleName();
    View view;
    OnItemClickedListener callback;
    Button buttonOne;
    Button buttonTwo;

    public static FeedFragment getInstance(){
        FeedFragment fragment = new FeedFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.feed_fragment, container, false);
        buttonOne = view.findViewById(R.id.button_one);
        buttonTwo = view.findViewById(R.id.button_two);
        init();
        return view;
    }

    private void init() {


        buttonOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.onButtonClicked(1);
            }
        });

        buttonTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callback.onButtonClicked(2);
            }
        });
    }

    public void setOnButtonClickedListener(OnItemClickedListener callback) {
        this.callback = callback;
    }


    public interface OnItemClickedListener {
        public void onButtonClicked(int position);
    }
}

fragment_player.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/demo_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

PlayerFragment.java

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class PlayerFragment extends Fragment {

    public static final String TAG = PlayerFragment.class.getSimpleName();
    private static final String ARG_POSITION = "position";

    private View view;
    private TextView demoTextView;
    private int position;

    public static PlayerFragment getInstance(int position){
        PlayerFragment fragment = new PlayerFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_POSITION, position);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(getArguments() != null ){
            position = getArguments().getInt(ARG_POSITION);
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_player, container, false);
        demoTextView = view.findViewById(R.id.demo_text_view);
        init();
        return view;
    }

    private void init() {
        demoTextView.setText("Player Fragment, clicked position "+ position);
    }
}

希望它会对您有所帮助。谢谢。