簡體   English   中英

如何在SwipeRefreshLayout中添加多個視圖,其中一個是recyclerview?

[英]How to add multiple views inside SwipeRefreshLayout, with one of them being recyclerview?

我在SwipeRefreshLayout中有一個recyclerview。 在recyclerview結束時,我想添加一個按鈕,但我無法這樣做。 這里的代碼:

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/refresh"
    android:layout_width="wrap_content"
    android:layout_below="@+id/toolbar"
    android:paddingBottom="50dp"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/load_more"
        android:layout_alignParentBottom="true"
        android:textColor="@color/md_black_1000"
        android:text="TEST"/>

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

該按鈕不會出現在recyclerview的末尾。

SwipeRefreshLayout只能有一個孩子。

RecyclerViewButton添加到垂直LinearLayout並將其放入SwipeRefreshLayout

<android.support.v4.widget.SwipeRefreshLayout>
    <LinearLayout>
        <android.support.v7.widget.RecyclerView>
        <Button>
    </LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>

要在RecyclerView添加Button作為最后一項,您需要編輯adapter

1)增加行數

@Override
public int getItemCount() {
    return pictureArrayList.size() + 1;
}

2)列表結束時顯示Button

@Override
public void onBindViewHolder(final ExampleHolder holder, final int position) {
    final Picture picture = pictureArrayList.get(position);
    if (position <= pictureArrayList.size()) {
        holder.title.setVisibility(View.VISIBLE);
        holder.button.setVisibility(View.GONE);
        holder.title.setText(picture.getName());
        holder.imageView.setImageResource(picture.getImage());
    } else {
        holder.title.setVisibility(View.GONE);
        holder.button.setVisibility(View.VISIBLE);
    }
}

您需要創建一個包含不同視圖項的RecyclerView,具體取決於您要執行的操作,可以選擇使用列表底部的視圖按鈕,我有一篇博文,其中我寫了一篇關於RecyclerView(西班牙語)的博客。

http://erikcaffrey.github.io/2015/10/05/recyclerview/

你可以看到代碼!

https://github.com/erikcaffrey/RecyclerView-Examples

暫無
暫無

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

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