0

I see a need in my project to move from ListView to RecyclerView. But right I am facing some issues on adapter implementation.

In my current ListView implementation I am using CustomView instead of inflating there. Here is my current getView()

public View getView(int position, View containerRow, ViewGroup parent) {
        return PanelViewFactory.createPanel(..)
}

But in the RecyclerView adapter there is no getView() method. We need to bind this custom view into respective view holder. But how can I do ths in current implementation since I m not inflating any layout in adapter. The view inflation is done in PanelViewFactory and i got only view instance here,. So please help me how to solve this issue.

Thanks in advance

Sayooj O
  • 15
  • 1
  • 6
  • Possible duplicate of [Use custom View in a RecyclerView Adapter?](https://stackoverflow.com/questions/42522121/use-custom-view-in-a-recyclerview-adapter) – Alex Peters Jan 07 '18 at 01:48

2 Answers2

1

You would inflate in onCreateViewHolder of your recycler adapter as follows:

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // Create a new view
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_row_layout, parent, false);
    return new ViewHolder(view, this);
}

This is where you can create your custom view.

Naveed
  • 2,942
  • 2
  • 25
  • 58
  • 1
    The question asks how to use a custom view inside a `RecyclerView`. Directly inflating XML is not using a custom view. – Alex Peters Jan 07 '18 at 01:46
  • There is no rule that says a custom view needs to be directly attached to the parent view only programmatically. A well defined custom view should render in the xml editor properly. I can create a layout file where my only view is the custom view `...` and then inflate it in my recycler view. I prefer this option because I can preview my custom view before deploying it on the device/emulator. – Naveed Jan 07 '18 at 19:12
  • I should clarify my earlier comment: directly inflating XML _inside the `RecyclerView.Adapter`_ is not using a custom view—certainly not in the case of the question that was asked, where a factory method provides a fully configured view already. – Alex Peters Jan 07 '18 at 23:28
1

You can apply the pattern of different ViewTypes in a RecyclerView.Adapter as well.

Step 1: Create your custom view in the onCreateViewHolder()

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = PanelViewFactory.createPanel(..);
    return new PanelViewHolder(view);
}

Here I'm assuming you'll have to create some kind of a ViewHolder for your custom PanelView. If you don't need a ViewHolder it most likely means you're not doing stuff in the most effective way in your current implementation, so it's a good opportunity to improve :)

Step 2: Bind data in your PanelViewHolder in the method

public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
     PanelViewHolder viewHolder = (PanelViewHolder)holder;
     viewHolder.name.setText(...);
     ....
}
Vesko
  • 3,750
  • 2
  • 23
  • 29