Reputation: 15
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
Upvotes: 0
Views: 4528
Reputation: 3760
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(...);
....
}
Upvotes: 1
Reputation: 3202
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.
Upvotes: 1