0

I'm working on an advanced LayoutManager subclass. It has some special requirements in that it needs to manually add custom views, not associated with those generated by the adapter.

I have tried this example code...

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);
    }
}

...but it throws an error in RecyclerView, here:

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);

Somehow I need to associate a ViewHolder to the view without going through the adapter, but I'm not sure how to do that. Not sure it's possible, but if anyone knows, it'll be you guys here! :)

So... is it possible?

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
  • Ah-HAH! I knew I wasn't crazy! ;-) – Mike M. Oct 18 '17 at 05:28
  • LOL! I thought I had it figured out. I originally went with a custom adapter that defined the header and footer there, but I've since shied away from that approach because I want the person to be able to specify the header and footer in the layout file by first specifying the LayoutManager attribute, then custom attributes to specify the header and footer resources, which are passed through to the LayoutManager constructor overload. That way, regardless of what adapter (or RecyclerView) they're using, they will get the header and footer as part of the layout. – Mark A. Donohoe Oct 18 '17 at 05:31

0 Answers0