动态RecyclerView标头

时间:2015-08-11 17:35:42

标签: android android-recyclerview recycler-adapter

我正在编写一个应用程序,我需要在RecyclerView中使用标题。我想像listview(addHeaderView)一样动态添加它,并在标题中更改数据(例如刷新时)。

提前谢谢!

3 个答案:

答案 0 :(得分:0)

如果我清楚地理解这个问题,我想它并不那么难。使用Recycler视图适配器可以使用不同的视图持有者类来描述不同数据的不同视图。 为了正确实现,适配器只是覆盖了管理视图持有者的创建和绑定的方法。

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private String[] mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                               .inflate(R.layout.my_text_view, parent, false);
        // set the view's size, margins, paddings and layout parameters
        ...
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.mTextView.setText(mDataset[position]);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

然后动态添加由特定视图持有者描述的标头,您只需将一个元素添加到表示数据的结构中,并使用一些逻辑将其与显示的普通视图区分开来,例如使用在执行此操作后,重新加载适配器的数据,它将再次绑定视图持有者的位置。如果您需要更多细节,请告诉我有关具体情况的更多信息,我会举一个例子。

答案 1 :(得分:0)

我在我的适配器中发现错误,它在getItemCount()中,我用两个数据(对于标题和项目)犯了一个错误,所以它总是返回0并且没有任何反应。我修好了,现在正在工作!顺便说一句,对每个试图帮助我的人都这样!再见:)

答案 2 :(得分:0)

将标头添加到RecyclerView的最简单方法是使用CoordinatorLayout

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- Replace this ImageView with your header view -->
        <ImageView
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            app:layout_scrollFlags="scroll" />

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

    <!-- This can be any scrolling view -->
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>
相关问题