首页 > 解决方案 > recyclerview 上的第一项堆叠到选项卡布局

问题描述

recyclerview 上的第一项堆叠到选项卡布局

我已经检查了 xml 文件,但一切都很好(对我来说),但视图仍然堆积如山。我也收到这样的错误消息

PlayingFragment:onResponse:hasil pemanggilanretrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@1c95dee5

这是我的 PlayingFramgment 课程 onResponse 里面的内容

private void bacaData(){
        progressBar.setVisibility(View.VISIBLE);
        final RecyclerView rvCategory = getActivity().findViewById(R.id.playing_daftar);
        rvCategory.setLayoutManager(new LinearLayoutManager(getActivity()));

        ApiInterface apiInterface = ApiClient.getRetrofit(getContext()).create(ApiInterface.class);
        Call<MovieResponse> call = apiInterface.getNowPlaying();
        call.enqueue(new Callback<MovieResponse>() {
            @Override
            public void onResponse(Call<MovieResponse> call, Response<MovieResponse> response) {
                MovieResponse data = response.body();
                if (data.getResults().size() == 0){
                    Toast.makeText(getContext(), "Maaf data yang anda cari tidak ditemukan",
                            Toast.LENGTH_SHORT).show();
                    progressBar.setVisibility(View.GONE);
                } else {
                    rvCategory.setAdapter(new MoviesAdapter(data.getResults(), R.layout.list_movie_row, getContext()));
                    Log.d(TAG, "onResponse: hasil pemanggil" + call);
                    progressBar.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFailure(Call<MovieResponse> call, Throwable t) {
                Toast.makeText(getContext(), "Gagal", Toast.LENGTH_SHORT).show();
                Log.d(TAG, t.toString());
                progressBar.setVisibility(View.GONE);
            }
        });
    }

我发布了 xml 布局 fragment_playing

<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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".fragment.PlayingFragment">

    <!-- TODO: Update blank fragment layout -->

        <android.support.v7.widget.RecyclerView
            android:id="@+id/playing_daftar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/progressMainBar"
        android:layout_centerInParent="true"/>

</RelativeLayout>

fragment_playing 是 fragment_home 的一部分,这里是代码

<?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="wrap_content"
    android:orientation="vertical"
    tools:context=".BlankFragment">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways"
            app:tabGravity="fill"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabMode="fixed"
            app:tabSelectedTextColor="#ffffff">

        </android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/appbar"
            android:padding="@dimen/activity_vertical_margin" />
    </FrameLayout>

</LinearLayout>

我把 fragment_home 放到 content_main

<RelativeLayout 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:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">

    <android.support.v4.view.ViewPager
        android:layout_below="@+id/toolbar"
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>

content_main 是 app_bar_main 的一部分

<RelativeLayout 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:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <android.support.design.widget.TabLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimaryDark"
            app:tabTextColor="@color/colorAccent"
            app:tabSelectedTextColor="@color/colorAccent"
            android:id="@+id/tabs">

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/tab_np"
                android:text="@string/now_playing"
                android:textColor="@color/colorAccent"/>

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/up_coming"
                android:id="@+id/tab_up"/>

        </android.support.design.widget.TabLayout>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

</RelativeLayout>

标签: android

解决方案


由于您使用 RelativeLayout 作为根组件,因此您必须为 RecyclerView 提供顶部边距:

android:layout_marginTop="104dp"

编辑:

如果您使用 coordinatorLayout 作为 Root 布局,情况会有所不同。

编辑

以下问题在您的代码中很明显:

1- Fragment 主页中的 AppBarLayout 是额外的,因为您已经在活动的主页布局中有一个 Appbar。

2-根据android官方文档使用CoordinatorLayout而不是RelativeLayout。通过使用 RelativeLayout,您将失去 AppBarLayout 的一些功能和布局管理

3- 由于您刚刚在 app_bar_main 中包含了 content_main,因此无需使用tools:context=".MainActivity"tools:showIn="@layout/app_bar_main"

4- 用合并标签替换 content_main 中的 RelativeLayout

在上述更改之后,您刚刚删除了可能对边距和填充有副作用的额外布局,然后将下面的属性应用于您的视图寻呼机。我希望这些更改将有效,并且您的代码中没有其他隐藏项:

android:layout_marginTop="104dp"

推荐阅读