Android 5.0-将页眉/页脚添加到RecyclerView

发布于 2021-02-02 22:55:59

我花了一点时间试图找出一种将标头添加到的方法RecyclerView,但未成功。

这是我到目前为止所得到的:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...

    layouManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layouManager);

    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    headerPlaceHolder = inflater.inflate(R.layout.view_header_holder_medium, null, false);
    layouManager.addView(headerPlaceHolder, 0);

   ...
}

LayoutManager似乎是处理的配置对象RecyclerView的项目。由于找不到任何addHeaderView(View view)方法,因此决定使用LayoutManager的addView(View view, int position)方法,并将标头视图添加到第一个位置以用作标头。

Aaand这就是事情变得更糟的地方:

java.lang.NullPointerException: Attempt to read from field 'android.support.v7.widget.RecyclerView$ViewHolder android.support.v7.widget.RecyclerView$LayoutParams.mViewHolder' on a null object reference
    at android.support.v7.widget.RecyclerView.getChildViewHolderInt(RecyclerView.java:2497)
    at android.support.v7.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:4807)
    at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:4803)
    at com.mathieumaree.showz.fragments.CategoryFragment.setRecyclerView(CategoryFragment.java:231)
    at com.mathieumaree.showz.fragments.CategoryFragment.access$200(CategoryFragment.java:47)
    at com.mathieumaree.showz.fragments.CategoryFragment$2.success(CategoryFragment.java:201)
    at com.mathieumaree.showz.fragments.CategoryFragment$2.success(CategoryFragment.java:196)
    at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:41)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

NullPointerExceptions尝试addView(View view)创建Activity的不同时刻调用了几次之后(还尝试在设置所有内容(甚至是适配器的数据)后添加视图),我意识到我不知道这是否是正确的方法(并且它看起来不是)。

PS:另外,一个能够处理的解决方案GridLayoutManager,除了LinearLayoutManager将非常感激!

关注者
0
被浏览
420
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    我必须在其中添加页脚RecyclerView,在这里我分享了我的代码段,因为我认为它可能有用。请检查代码中的注释,以更好地了解整体流程。

    import android.content.Context;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    import java.util.ArrayList;
    
    public class RecyclerViewWithFooterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
        private static final int FOOTER_VIEW = 1;
        private ArrayList<String> data; // Take any list that matches your requirement.
        private Context context;
    
        // Define a constructor
        public RecyclerViewWithFooterAdapter(Context context, ArrayList<String> data) {
            this.context = context;
            this.data = data;
        }
    
        // Define a ViewHolder for Footer view
        public class FooterViewHolder extends ViewHolder {
            public FooterViewHolder(View itemView) {
                super(itemView);
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Do whatever you want on clicking the item
                    }
                });
            }
        }
    
        // Now define the ViewHolder for Normal list item
        public class NormalViewHolder extends ViewHolder {
            public NormalViewHolder(View itemView) {
                super(itemView);
    
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // Do whatever you want on clicking the normal items
                    }
                });
            }
        }
    
        // And now in onCreateViewHolder you have to pass the correct view
        // while populating the list item.
    
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
            View v;
    
            if (viewType == FOOTER_VIEW) {
                v = LayoutInflater.from(context).inflate(R.layout.list_item_footer, parent, false);
                FooterViewHolder vh = new FooterViewHolder(v);
                return vh;
            }
    
            v = LayoutInflater.from(context).inflate(R.layout.list_item_normal, parent, false);
    
            NormalViewHolder vh = new NormalViewHolder(v);
    
            return vh;
        }
    
        // Now bind the ViewHolder in onBindViewHolder
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    
            try {
                if (holder instanceof NormalViewHolder) {
                    NormalViewHolder vh = (NormalViewHolder) holder;
    
                    vh.bindView(position);
                } else if (holder instanceof FooterViewHolder) {
                    FooterViewHolder vh = (FooterViewHolder) holder;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        // Now the critical part. You have return the exact item count of your list
        // I've only one footer. So I returned data.size() + 1
        // If you've multiple headers and footers, you've to return total count
        // like, headers.size() + data.size() + footers.size()
    
        @Override
        public int getItemCount() {
            if (data == null) {
                return 0;
            }
    
            if (data.size() == 0) {
                //Return 1 here to show nothing
                return 1;
            }
    
            // Add extra view to show the footer view
            return data.size() + 1;
        }
    
        // Now define getItemViewType of your own.
    
        @Override
        public int getItemViewType(int position) {
            if (position == data.size()) {
                // This is where we'll add footer.
                return FOOTER_VIEW;
            }
    
            return super.getItemViewType(position);
        }
    
        // So you're done with adding a footer and its action on onClick.
        // Now set the default ViewHolder for NormalViewHolder
    
        public class ViewHolder extends RecyclerView.ViewHolder {
            // Define elements of a row here
            public ViewHolder(View itemView) {
                super(itemView);
                // Find view by ID and initialize here
            }
    
            public void bindView(int position) {
                // bindView() method to implement actions
            }
        }
    }
    

    上面的代码段在中添加了页脚RecyclerView。你可以检查此GitHub存储库,以检查添加页眉和页脚的实现。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看