滚动时,RecyclerView为项目设置了错误的高度

时间:2016-12-13 12:58:01

标签: java android android-layout android-recyclerview

我是一个Android新手,之前曾见过类似的问题。我已经遵循给定的建议并将layout_height设置为wrap_content而没有任何成功。 我究竟做错了什么?我的代码片段如下:MainActivity.java包含

@Override
    protected void onCreate(Bundle savedInstanceState) {
        /*this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);*/
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

        // Layout manager that allows the user to flip through the pages
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

        // getSupportFragmentManager allows use to interact with the fragments
        // MyFragmentPagerAdapter will return a fragment based on an index that is passed
        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),
                MainActivity.this));

        // Initialize the Sliding Tab Layout
        SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);

        slidingTabLayout.setCustomTabView(R.layout.tab_indicator);
        // Connect the viewPager with the sliding tab layout
        slidingTabLayout.setViewPager(viewPager);

    }

activity_main包含

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary" />
        <!--app:theme="@style/ActionBarThemeOverlay" />-->
    <!--app:popupTheme="@style/ThemeOverlay.AppCompat.Light"-->

    <!-- The sliding tab -->
    <com.src.xxx.stab.SlidingTabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary" />

    <!--android:layout_below="@+id/app_bar"-->

    <!-- The pager that allows us to swipe between fragments -->
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white" />

</LinearLayout>

每个tab_fragment包含

<?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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical">

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

</RelativeLayout>

3 个答案:

答案 0 :(得分:2)

如果设置换行内容,则根据数据设置高度,如果需要固定高度,请在dp中设置固定高度。示例

android:layout_height="50dp"

答案 1 :(得分:1)

行高应保持不变,以便为每行获得相同的高度,例如20dp而不是换行内容。

答案 2 :(得分:1)

只需更改您的商品视图的布局参数(不是activity_main或tab_fragment)。

android:layout_height=”wrap_content”

https://medium.com/@gabri.mariotti/recyclerview-23-2-0-and-item-height-15b08eb06573

相关问题