【问题标题】:Implementing an adapter for RecyclerView in Android在 Android 中为 RecyclerView 实现适配器
【发布时间】:2015-09-29 20:50:58
【问题描述】:

我正在尝试用材料设计的图像模式来实现灵活的空间。

为此,我关注了this tutorial

问题是,本教程使用RecyclerView,而我想使用另一个视图,它只是一个简单的ScrollView 与相对视图:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingRight="20dp" >

        <TextView
            android:id="@+id/loggedInTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/separator1"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="15dp"
            android:text="@string/profile_logged_in_with_title"
            android:textSize="18sp" />

        <ImageView
            android:id="@+id/loggedInPlatformLogo"
            android:layout_width="95dp"
            android:layout_height="30dp"
            android:layout_alignBottom="@+id/loggedInTitle"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="-4dp"
            android:layout_marginLeft="30dp"
            android:layout_toRightOf="@+id/loggedInTitle"
            android:background="@drawable/facebook_logo"
            android:gravity="center_horizontal|center_vertical" />

        <View
            android:id="@+id/separator2"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_alignLeft="@+id/separator1"
            android:layout_below="@+id/loggedInTitle"
            android:layout_marginTop="17dp"
            android:background="@android:color/darker_gray" />

        <ImageView
            android:id="@+id/homeIcon"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignLeft="@+id/loggedInTitle"
            android:layout_below="@+id/separator2"
            android:layout_marginTop="10dp"
            android:background="@drawable/home_icon" />

        <TextView
            android:id="@+id/homeTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/homeIcon"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@+id/homeIcon"
            android:text="@string/profile_home_title"
            android:textSize="18sp"
            android:textStyle="bold" />

        <AutoCompleteTextView
            android:id="@+id/homeAddressEdit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/homeIcon"
            android:layout_below="@+id/homeIcon"
            android:layout_marginTop="2dp"
            android:ems="10"
            android:hint="@string/profile_home_address_hint"
            android:inputType="textAutoComplete"
            android:textSize="16sp" />

        <View
            android:id="@+id/separator3"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_alignLeft="@+id/separator1"
            android:layout_below="@+id/homeAddressEdit"
            android:layout_marginTop="8dp"
            android:background="@android:color/darker_gray" />

        <ImageView
            android:id="@+id/workIcon"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignLeft="@+id/homeIcon"
            android:layout_below="@+id/separator3"
            android:layout_marginTop="10dp"
            android:background="@drawable/work_icon" />

        <TextView
            android:id="@+id/workTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/workIcon"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@+id/workIcon"
            android:text="@string/profile_work_title"
            android:textSize="18sp"
            android:textStyle="bold" />

        <AutoCompleteTextView
            android:id="@+id/workAddressEdit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/workIcon"
            android:layout_below="@+id/workIcon"
            android:layout_marginTop="2dp"
            android:ems="10"
            android:hint="@string/profile_work_address_hint"
            android:inputType="textAutoComplete"
            android:textSize="16sp" />

        <View
            android:id="@+id/separator4"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_alignLeft="@+id/separator1"
            android:layout_below="@+id/workAddressEdit"
            android:layout_marginTop="8dp"
            android:background="@android:color/darker_gray" />

        <ImageView
            android:id="@+id/privacyIcon"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignLeft="@+id/workIcon"
            android:layout_below="@+id/separator4"
            android:layout_marginTop="13dp"
            android:background="@drawable/privacy_icon" />

        <TextView
            android:id="@+id/privacyTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/privacyIcon"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="15dp"
            android:layout_toRightOf="@+id/privacyIcon"
            android:text="@string/profile_privacy_title"
            android:textSize="18sp"
            android:textStyle="bold" />

        <Spinner
            android:id="@+id/privacySpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/privacyIcon"
            android:layout_below="@+id/privacyIcon"
            android:layout_marginBottom="4dp"

            android:layout_marginTop="-3dp"
            android:entries="@array/profile_privacy_settings"
            android:prompt="@string/profile_privacy_title" />

    </RelativeLayout>
</ScrollView>

所以我尝试将教程布局xml中的RecyclerView替换为对此视图的引用,但结果是我无法滚动视图。 我猜那是因为ScrollView 与灵活空间模式不兼容

下一次尝试是尝试将上述ScrollView 布局转换为适配器以使用RecyclerView following this Android Developers example,但我不知道该怎么做,因为我有太多元素要组合和示例由简单的字符串数组组成。

我真的很感激适配器应该是什么样子以匹配上面的布局,或者另一个更简单的解决方案(也许 RecyclerView 不是最好的方向),如果它存在的话。

【问题讨论】:

    标签: java android material-design android-recyclerview


    【解决方案1】:

    Google 提供了一个设计支持库。 试试这个网站: http://hmkcode.com/material-design-app-android-design-support-library-appcompat/

    【讨论】:

    • 这里的例子是卡片视图,显然我不需要卡片视图,因为它看起来与初始布局有很大不同......
    猜你喜欢
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多