【问题标题】:how do I add header/footer on my Recyclerview in android如何在 android 的 Recyclerview 上添加页眉/页脚
【发布时间】:2018-08-16 19:00:30
【问题描述】:

我花了一些时间试图弄清楚如何将页眉/页脚添加到我的 recyclerview 中,但它不起作用。页脚显示但标题和列表项未显示在模拟器上。 我的适配器

package com.example.system2.tranxav.adapters;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.system2.tranxav.R;
import com.example.system2.tranxav.model.Problems;

import java.util.List;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{

Context context;
private List<Problems> mDataset;
private static final int TYPE_HEADER = 0;
private static final int TYPE_FOOTER = 1;
private static final int TYPE_ITEM = 2;

public class ViewHolder extends RecyclerView.ViewHolder{
    public TextView tvProblems, tvPrice;
    public Button btnProblem;

    public ViewHolder(View itemView) {
        super(itemView);

    }
}

public void add(int position, Problems item){
    mDataset.add(position, item);
    notifyItemInserted(position);
}

public void remove(Problems item){
    int position = mDataset.indexOf(item);
    mDataset.remove(position);
    notifyItemRemoved(position);
}

public MyAdapter(List<Problems> myDataset){
    mDataset = myDataset;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


    View itemView;
    Log.i("info", String.valueOf(viewType));
    if (viewType == TYPE_ITEM) {
        //Inflating recycle view item layout
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_problems, parent, false);
        ViewHolder vh = new ViewHolder(itemView);
        return  vh;
    } else if (viewType == TYPE_HEADER) {
        //Inflating header view
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.problem_recycler_header, parent, false);
        ViewHolder vh = new ViewHolder(itemView);
        return  vh;
    } else if (viewType == TYPE_FOOTER) {
        //Inflating footer view
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.problem_recycler_footer, parent, false);
        ViewHolder vh = new ViewHolder(itemView);
        return  vh;
    }
    throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    try {
    if (holder instanceof HeaderViewHolder) {
        HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
        headerHolder.tvHeader.setText("Please kindly ask the mechanic what the problem is and check them to continue");

    } else if (holder instanceof FooterViewHolder) {
        FooterViewHolder footerHolder = (FooterViewHolder) holder;

        footerHolder.btnProblem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("info", "the button is clicked");
            }
        });
    }
    else if (holder instanceof ItemViewHolder) {
        ItemViewHolder itemView = (ItemViewHolder) holder;
        itemView.tvPrice.setText(mDataset.get(position).getPrice());
        itemView.tvProblems.setText(mDataset.get(position).getProblems());
    }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
@Override
public int getItemCount() {
    return mDataset.size()+2;
}

public int getItemViewType(int position) {


    if (position == 0) {
        return TYPE_HEADER;
    } else if (position == mDataset.size() + 1) {
        return TYPE_FOOTER;
    }
    return TYPE_ITEM;
}

private class HeaderViewHolder extends MyAdapter.ViewHolder {
    TextView tvHeader;

    public HeaderViewHolder(View view) {
        super(view);
        tvHeader = (TextView) view.findViewById(R.id.tvHeader);
    }
}
private class FooterViewHolder extends MyAdapter.ViewHolder {
    Button btnSubmitProblem;

    public FooterViewHolder(View view) {
        super(view);
        btnSubmitProblem = (Button) view.findViewById(R.id.btnProblem);
    }
}

private class ItemViewHolder extends MyAdapter.ViewHolder {
    TextView tvProblems, tvPrice;

    public ItemViewHolder(View itemView) {
        super(itemView);
        tvProblems = (TextView) itemView.findViewById(R.id.tvProblems);
        tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
    }
}

problem_recycler_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tvHeader" />
 </LinearLayout>

footer_problem_recycler.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
    android:text="Button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btnProblem" />
 </LinearLayout>

activity_problems.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:orientation="vertical"
android:paddingBottom="@dimen/zerodimen"
android:paddingLeft="@dimen/zerodimen"
android:paddingRight="@dimen/zerodimen"
android:paddingTop="16dp"
tools:context="com.example.system2.tranxav.ProblemsActivity">
<android.support.v7.widget.RecyclerView
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:paddingBottom="0dp"
    android:layout_marginBottom="0dp"
    android:scrollbars="none"
    android:id="@+id/problem_recycle_view"
    xmlns:android="http://schemas.android.com/apk/res/android" />

<TextView
    android:id="@+id/tvProblems"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:textSize="16dp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/tvPrice"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:textStyle="bold" />

</LinearLayout>

我已经检查了代码以知道问题出在哪里,但我无法弄清楚。我正在从我的数据集中获取数据,但它没有显示在模拟器上。

【问题讨论】:

  • 你传递给adapterList&lt;Problems&gt; mDataset;的大小是多少
  • @AbdulKawee 大小是动态的。我是从本地主机那里得到的
  • 请在此处输入您的活动代码
  • 是的,它是动态的,但检查它是否不是您传递的空列表

标签: android android-recyclerview recyclerview-layout


【解决方案1】:

我觉得onBindViewHolder的位置有点小问题

你能不能像这样改变你的代码并试一试。

else if (holder instanceof ItemViewHolder) {
        ItemViewHolder itemView = (ItemViewHolder) holder;
        itemView.tvPrice.setText(mDataset.get(position - 1).getPrice());
        itemView.tvProblems.setText(mDataset.get(position - 1).getProblems());
      } 

您的代码的问题是您返回相同的viewholder,您应该在onCreateViewHolder 中更改这样的代码

    View itemView;
    Log.i("info", String.valueOf(viewType));
    if (viewType == TYPE_ITEM) {
        //Inflating recycle view item layout
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_problems, parent, false);
        ViewHolder vh = new ViewHolder(itemView);
        return  vh;
    } else if (viewType == TYPE_HEADER) {
        //Inflating header view
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.problem_recycler_header, parent, false);
        HeaderViewHolder vh = new HeaderViewHolder(itemView);
        return  vh;
    } else if (viewType == TYPE_FOOTER) {
        //Inflating footer view
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.problem_recycler_footer, parent, false);
        FooterViewHolder vh = new FooterViewHolder(itemView);
        return  vh;
    }

【讨论】:

  • 页眉和页脚显示,但列表不显示。请你再检查一下代码。 mDataset 不为空
  • @thanks 有效,但是当我滚动到底部尝试调用虚拟方法 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' 时它向我显示此错误空对象引用
  • 如果问题仍然存在,请将其作为单独的问题发布,但我建议您阅读此stackoverflow.com/questions/218384/…
【解决方案2】:

我遇到了这个问题,为了简单起见,我对页脚行使用了与所有其他行相同的布局。

所以我做了两个相对布局,一个在另一个之下。顶部布局包含回收器,底部布局包含动态添加的页脚。将它们放在一个相对布局中,然后将其放入 ScrollView。

然后我稍微定制了recyclerview,让它不垂直覆盖整个屏幕,这样两个布局都是可见的。

最终布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:scrollbars="vertical"
    android:background="@color/colorBG">

    <RelativeLayout
        android:id="@+id/layContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/layTop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_alignParentTop="true">

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

        <RelativeLayout
            android:id="@+id/layBottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_alignParentBottom="true"
            android:layout_below="@id/layTop"
            android:background="@color/colorFooterBG">

            <!-- footer will be added here -->
        </RelativeLayout>
    </RelativeLayout>
</ScrollView>

以及设置recycler时fragment的onCreateView中的这段代码:

private void setupFooterRecycler() {
    // use a linear layout manager
    RecyclerView.LayoutManager layManUS = new LinearLayoutManager(setupActivity().get());
    recycler.setLayoutManager(layManUS);
    recycler.addItemDecoration(new DividerItemDecoration(
            setupActivity().get(), LinearLayoutManager.VERTICAL));
    recycler.setNestedScrollingEnabled(true);
    // set fixed height
    DisplayMetrics displaymetrics = new DisplayMetrics();
    setupActivity().get().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    // set recycler height to some percent of screen, needed to experiment
    int a =  (displaymetrics.heightPixels*70)/100;
    recycler.getLayoutParams().height =a;
    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    recycler.setHasFixedSize(true);
}

下面是我如何在片段的 onCreateView 中添加页脚,特别感谢acoustic

View footerView = inflater.inflate(R.layout.footer_item,
            (ViewGroup) rootView.findViewById(R.id.layBottom));

【讨论】:

    猜你喜欢
    • 2014-12-14
    • 2015-05-13
    • 2020-11-30
    • 1970-01-01
    • 2011-11-27
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多