我无法渲染自定义视图内部回收站视图小部件。传统上,我们内部膨胀的RecylerView.Adapter类似观点在RecyclerView小部件中使用自定义视图
public RowLayoutViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.sample_view, viewGroup, false);
RowLayoutViewHolder rowLayoutViewHolder = new RowLayoutViewHolder(view);
return rowLayoutViewHolder;
}
这工作得很好,我们可以将数据绑定到里面onBindViewHolder(...)方法的看法。但是当我尝试创建ViewGroup的子类并像这样使用时,我得到一个空白(“黑色”)屏幕。
public ImageGalleryAdapter.RowLayoutViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
SampleView view = new SampleView(context);
RowLayoutViewHolder rowLayoutViewHolder = new RowLayoutViewHolder(view);
return rowLayoutViewHolder;
}
这里是我的SampleView类 -
public class SampleView extends ViewGroup {
public SampleView(Context context) {
super(context);
initView();
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
child.layout(l, t, l + 600, t + 200);
}
}
private void initView() {
inflate(getContext(), R.layout.sample_view, this);
TextView textView = (TextView) findViewById(R.id.sampleTextView);
textView.setTextColor(Color.WHITE);
textView.setText("Hello from textview");
}
}
这里是布局 -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/sampleTextView"
android:text="Sample TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
看着好像充气布局的源代码行为完全一样创建一个视图,除了通胀过程基于rootView将一个合适的LayoutParameter添加到子视图。我试着手动添加,但没有任何结果。
任何帮助将不胜感激。
确实看到了这个问题的任何解决方案,我有类似的问题,因为我用我的回收站视图查看持有人如何在其布局具有自定义视图,当我向后滚动,我滚动快回收视图只是错过了一些平局,并在屏幕上留下了一些前面的项目,从回收商视图 – Sniper
我找到了一个解决方案。因为我错过了那些回滚视图需要在滚动时重新填充所有内容的部分,所以我没有填充我的布局的每个部分,这非常重要,这就是为什么我看到其他子视图的数据。 – Sniper