您是否可以手动将视图添加到不是来自适配器的RecyclerView?

时间:2017-10-18 05:22:48

标签: android android-recyclerview recyclerview-layout

我正在使用高级LayoutManager子类。它有一些特殊要求,因为它需要手动添加自定义视图,而不是与适配器生成的视图相关联。

我试过这个示例代码......

public class TestLayoutManager extends RecyclerView.LayoutManager{

    public TestLayoutManager(Context context){

        TextView textView = new TextView(context);
        textView.setLayoutParams(generateDefaultLayoutParams());
        textView.setText("Hello World");
        layoutDecorated(textView, 0, 0, 200, 100);
        ignoreView(textView);

        // When this line is called, it throws the error
        this.addView(textView);
    }
}

...但它在RecyclerView中抛出一个错误,在这里:

private void addViewInt(View child, int index, boolean disappearing) {

    final ViewHolder holder = getChildViewHolderInt(child);

    // Null-pointer exception here because 'holder' is null
    if (disappearing || holder.isRemoved()) { 

        // these views will be hidden at the end of the layout pass.
        mRecyclerView.mViewInfoStore.addToDisappearedInLayout(holder);

不知何故,我需要将ViewHolder与视图关联,而无需通过适配器,但我不知道该怎么做。不确定它是否有可能,但如果有人知道,那么你们会在这里! :)

所以......有可能吗?

0 个答案:

没有答案