Деятельность к фрагменту - PullRequest
2 голосов
/ 06 июля 2019

Я впервые использую Фрагменты и пытаюсь перейти от Активности к Фрагменту.Навигация должна обрабатываться OnClickListener (ImageView), который ведет меня к указанному фрагменту.Однако мой фрагмент занимает не весь экран.Я установил match-parent во входящем представлении, а также в кадре, который должен содержать фрагмент.В общем, я не уверен, что у меня возникло недопонимание XML или Java.Я предоставлю столько, сколько смогу ниже;

public class recipe_detail extends AppCompatActivity {


    TextView mIngredientsTV;
    RecyclerView ingredientsRecyclerView;
    RecyclerView stepsRecyclerView;
    ImageView mBakingGif;


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


        mBakingGif.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.add(R.id.frag_container, new VideoFragment());
                ft.commit();
            }
        });


}



public class VideoFragment extends Fragment {

    ImageView mThumbnailImage;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        return inflater.inflate(R.layout.video_fragment, container, true);

    }


    // This event is triggered soon after onCreateView().
    // Any view setup should occur here.  E.g., view lookups and attaching view listeners.
    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {


        mThumbnailImage = view.findViewById(R.id.exo_player_view);


        Drawable myDrawable = getResources().getDrawable(R.drawable.ic_launcher_background);
        mThumbnailImage.setImageDrawable(myDrawable);


    }

}

<!--Activity to be Replaced-->

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <RelativeLayout
        android:id="@+id/parent_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:id="@+id/ingredients_header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
            <TextView
                android:id="@+id/ingredients_text_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Ingredients_header"
                android:textColor="#000"
                android:textSize="18sp"
                android:textStyle="bold"
                tools:text="INGREDIENTS" />


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="10dp"
                android:background="#E0F2F1"
                android:orientation="vertical">


                <android.support.v7.widget.RecyclerView
                    android:id="@+id/ingredients_recycler_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />


            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="@string/steps_label"
                android:textColor="#000"
                android:textSize="20sp"
                android:textStyle="bold" />


            <ImageView
                android:id="@+id/video_thumbnail"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:layout_marginTop="20dp"
                android:background="@color/colorPrimaryDark" />

        </LinearLayout>


     <!-- Frame to hold fragment-->
        <FrameLayout
            android:id="@+id/frag_container"
            android:background="@android:color/darker_gray"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </RelativeLayout>

</ScrollView>

<!--Fragment to be infalted-->

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


    <ImageView
        android:id="@+id/exo_player_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <TextView
        android:id="@+id/description_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/exo_player_view" />


    <TextView
        android:id="@+id/short_description_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/description_tv" />

    <Button
        android:id="@+id/previous_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Prev" />

    <Button
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="Next" />

</RelativeLayout>

1 Ответ

1 голос
/ 06 июля 2019

Раздуваемый вами фрагмент всегда будет занимать столько же места, сколько и контейнер, в котором он находится.В вашем случае контейнер (FrameLayout) имеет высоту WRAP_CONTENT.Следовательно, фрагмент будет занимать только столько места, сколько нужно для отображения его содержимого.Что вы можете сделать, это установить высоту FrameLayout как MATCH_PARENT.Таким образом, фрагмент будет занимать все пространство экрана.

Просто примечание: при добавлении фрагментов поверх другого представления всегда не забывайте придавать макету вашего фрагмента некоторый цвет фона (по умолчанию он прозрачный).В противном случае ваше предыдущее представление также останется видимым через фрагмент.

РЕДАКТИРОВАТЬ (Объясняющий комментарий)

Вам необходимо переместить FrameLayout из ScrollView, например,

<FrameLayout 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">

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <RelativeLayout
            android:id="@+id/parent_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/ingredients_header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/ingredients_text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/Ingredients_header"
                    android:textColor="#000"
                    android:textSize="18sp"
                    android:textStyle="bold"
                    tools:text="INGREDIENTS" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="10dp"
                    android:background="#E0F2F1"
                    android:orientation="vertical">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/ingredients_recycler_view"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="20dp"
                    android:text="@string/steps_label"
                    android:textColor="#000"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <ImageView
                    android:id="@+id/video_thumbnail"
                    android:layout_width="match_parent"
                    android:layout_height="250dp"
                    android:layout_marginTop="20dp"
                    android:background="@color/colorPrimaryDark" />
            </LinearLayout>
        </RelativeLayout>
    </ScrollView>

    <!-- Frame to hold fragment-->
    <FrameLayout
        android:id="@+id/frag_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

Я переместил контейнер фрагментов из ScrollView.Однако, поскольку у действия / фрагмента может быть только 1 корневой элемент, я добавил FrameLayout в качестве родителя для ScrollView и FrameLayout.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...