Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Using RecyclerView with default Adapters

Is it possible to use default Adapters for RecyclerView? ArrayAdapter, for example (without creating own classes?)

Is there sense to use RecyclerView with just one line of text per item? ListView can do it without any extra classes.

like image 321
rubynewbie Avatar asked Feb 10 '16 11:02

rubynewbie


1 Answers

Here is a default ArrayAdapter implementation for the RecyclerView

https://github.com/passsy/ArrayAdapter

Sample implementation:

public class UserAdapter extends ArrayAdapter<User, UserAdapter.ViewHolder> {

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView titleView;

        public ViewHolder(final View itemView) {
            super(itemView);
            titleView = (TextView) itemView.findViewById(R.id.title);
        }
    }

    @Nullable
    @Override
    public Object getItemId(@NonNull final User user) {
        return user.getId();
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final User item = getItem(position);
        holder.titleView.setText(item.getName());
    }

    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
        final View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.itemview_user, parent, false);
        return new ViewHolder(view);
    }
}
like image 179
passsy Avatar answered Oct 14 '22 10:10

passsy