【发布时间】:2014-11-11 11:43:23
【问题描述】:
我正在尝试使用ItemDecorator 将一些分隔符添加到RecyclerView。这是执行此操作的代码片段。
//...
public abstract C onInflateViewHolder(Context ctx);
public abstract void onBindViewHolder(C holder, int index);
@Override
public void onDraw(Canvas c, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
for (int i = 0; i < parent.getChildCount(); i++) {
C view = onInflateViewHolder(mContext);
onBindViewHolder(view, i);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
//parent.addView(view.itemView, i, p);
view.itemView.draw(c);
}
}
//...
如您所见,我尝试使用parent.addView(view.itemView, i, p);、parent.addView(view.itemView, i);,最后使用view.itemView.draw(c);。
这是实现:
mItemDecorator =
new GenericDecorator<ChatDateSeparatorViewHolder>(getActivity(), GenericDecorator.VERTICAL_LIST) {
@Override
public ChatDateSeparatorViewHolder onInflateViewHolder(Context ctx) {
View v = LayoutInflater.from(ctx).inflate(R.layout.chat_message_separator, null);
return new ChatDateSeparatorViewHolder(v);
}
@Override
public void onBindViewHolder(ChatDateSeparatorViewHolder holder, int index) {
if (index < mAdapter.getItemCount()-1) {
ChatMessage previous = mAdapter.getMessage(index);
ChatMessage current = mAdapter.getMessage(index+1);
long startTime = previous.getSendDate().getTime();
long endTime = current.getSendDate().getTime();
long diffTime = endTime - startTime;
long diffDays = diffTime / (1000 * 60 * 60 * 24);
if (diffDays > 0) {
holder.text.setText(Converters.format(current.getSendDate(), getActivity()));
} else {
holder.root.setVisibility(View.GONE);
}
} else {
holder.root.setVisibility(View.GONE);
}
}
};
顺便说一句,C 是Recycler.ViewHolder,正如您在实现中看到的那样。
一切似乎都很好,但是在尝试添加视图时它会崩溃。当我将 addView 与 LayoutParams 广告 at android.support.v7.widget.RecyclerView$LayoutParams.getViewPosition(RecyclerView.java:6957) 一起使用时,我得到的例外是 NullpointerException
我只需要知道如何在不使用适配器的情况下以编程方式将View 添加到RecyclerView。
编辑:
我正在尝试在RecyclerView 中添加分隔符,此分隔符将是我从ViewHolder 获得的视图,一切都已完成,我唯一需要知道的是如何以编程方式添加视图(我从我的ViewHolder 获得)到RecyclerView
这是应用程序模型的屏幕截图,因此您会有更好的想法:
写着“Hoy”(今天是西班牙语)的那一行是分隔符之一。
【问题讨论】:
-
ItemDecoration 是嗯,一个装饰,所以你为什么要这样做:view.itemView.draw(c); ?
-
只是随机的一行代码,我试过了,因为我已经尝试过
addView(View,Index)和addView(View,Index,LayoutParams),我这样做只是为了试一试。 -
添加视图?在项目装饰?为什么?你想达到什么目标?
-
我编辑了这个问题,所以你会更好地了解发生了什么。
标签: java android draw android-recyclerview