簡體   English   中英

在Android中實現RecyclerView的適配器

[英]Implementing an adapter for RecyclerView in Android

我正在嘗試通過材料設計的圖像模式來實現靈活的空間。

為了做到這一點,我遵循了本教程

問題是,本教程使用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布局轉換為適配器,以便在此Android Developers示例之后使用RecyclerView ,但是我不知道該怎么做,因為我要組合的元素太多,並且該示例包含簡單的數組字符串。

我非常感謝您提供一個示例,以了解適配器與上面的布局相匹配的外觀,或者如果存在的話,還可以使用另一個更簡單的解決方案(也許RecyclerView不是最佳方向)。

有一個來自Google的設計支持庫。 嘗試以下網站: http : //hmkcode.com/material-design-app-android-design-support-library-appcompat/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM