如何在RecyclerView上设置标题?

时间:2017-06-17 13:12:02

标签: android header android-recyclerview

我的Android应用使用RecyclerView列出特定文件夹中的音频文件。但是,我想在RecyclerView顶部显示一个标题,指示当前文件夹TextView,但我不知道该怎么做。

有没有一种简单的方法可以实现这一目标?

这是我的适配器代码:

package vmc.songbook;

import android.content.Context;
import android.media.MediaMetadataRetriever;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.io.File;
import java.util.Date;

public class RecordingsAdapter extends RecyclerView.Adapter<RecordingsAdapter.ViewHolder> {

// Attributes
protected File[] files;             // Files to include in the RecyclerView
protected LayoutInflater inflater;  // This class creates the layout based on the XML file

/** Constructor */
public RecordingsAdapter(Context context, File[] files) {
        this.files = files;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

/**  ViewHolder is needed to store the variables of each file */
public static class ViewHolder extends RecyclerView.ViewHolder {
    public TextView file_name, file_duration, file_date;
    public ViewHolder(View itemView) {
        super(itemView);
        file_name = (TextView) itemView.findViewById(R.id.file_name);
        file_duration = (TextView) itemView.findViewById(R.id.file_duration);
        file_date = (TextView) itemView.findViewById(R.id.file_date);
    }
}

/** Inflating the ViewHolder based on the XML file*/
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = inflater.inflate(R.layout.file_element, null);
    return new ViewHolder(v);
}

/** Maximum number of items */
@Override public int getItemCount() {
    return files.length;
}

/** Customizing the ViewHolder */
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // Setting the date
    Date lastModDate = new Date(files[position].lastModified());
    holder.file_date.setText(lastModDate.toString());
    // Setting the audio duration
    String duration = getDuration(files[position]);
    holder.file_duration.setText(duration);
    // Setting the audio name
    holder.file_name.setText(files[position].getName());
}
}

由于我使用Basic Activity作为主要活动,我的content_main.xml如下:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>

这是我目前的展示:

Current Display

这就是我想要实现的目标:

Wanted display

编辑:如果我在LinearLayout中的RecyclerView上创建一个简单的TextView,布局会在操作栏上自行设置...

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以做的最简单的事情就是在回收者视图上方添加一个文本视图:)

<?xml version="1.0" encoding="utf-8"?>
<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:id="@+id/title_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="title" />

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

</LinearLayout>

在您的活动类中,将该文本视图设置为当前文件夹路径文本。

title_text_view.setText("path/to/folder");

答案 1 :(得分:0)

使用此代码段就像魅力一样,当然还有滚动: https://github.com/cundong/HeaderAndFooterRecyclerView