在Card视图中无法使用侧边栏android

时间:2018-04-03 13:53:50

标签: android android-recyclerview android-cardview

我有一个显示项目列表的卡片视图。我尝试将卡片一个在另一个下方对齐,但我无法实现它。我现在面临的另一个问题是我无法访问侧边栏项目。我可以通过向右滑动看到侧边栏,导航栏上缺少侧边栏的图标。

以下是代码段:

activity_top_ten_items.xml

<android.support.v4.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_teams"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

</android.support.v4.widget.DrawerLayout>

app_bar_teams.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="match_parent"
    tools:context="com.neonsec.ctftime.ctf_timemobile.MainActivity">

    <include
        layout="@layout/content_top_ten_teams"
        android:layout_width="match_parent"
        android:layout_height="667dp"
        tools:layout_editor_absoluteY="59dp" />

    <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="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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


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

content_top_10_items.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="@dimen/card_margin"
        android:elevation="3dp"
        card_view:cardCornerRadius="@dimen/card_album_radius">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/thumbnail"
                android:layout_width="match_parent"
                android:layout_height="@dimen/album_cover_height"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginTop="59dp"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:clickable="true"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/tname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/thumbnail"
                android:paddingLeft="@dimen/album_title_padding"
                android:paddingRight="@dimen/album_title_padding"
                android:paddingTop="@dimen/album_title_padding"
                android:textSize="@dimen/album_title" />

            <TextView
                android:id="@+id/tpoints"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/tname"
                android:paddingBottom="@dimen/songs_count_padding_bottom"
                android:paddingLeft="@dimen/album_title_padding"
                android:paddingRight="@dimen/album_title_padding"
                android:textSize="@dimen/songs_count" />


        </RelativeLayout>

    </android.support.v7.widget.CardView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

要将卡片对齐到另一张卡片,您需要使用带卡片的RecyclerView列表。这将是一个低于另一个的CardView列表。看看这个:https://www.androidtutorialpoint.com/material-design/android-cardview-tutorial/,或者:http://www.truiton.com/2015/03/android-cardview-example/  或者这个https://code.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465

sidenav 项目可在此处找到:res/menu/activity_main_drawer.xml。确保你在那里定义了物品。

sidenav的nav_bar上的按钮以这种方式以编程方式在控制器上创建:

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionbar = getSupportActionBar();
    actionbar.setDisplayHomeAsUpEnabled(true);
    actionbar.setHomeAsUpIndicator(R.drawable.ic_menu);

请参阅:https://developer.android.com/training/implementing-navigation/nav-drawer.html

希望它解决!!