如何为RecyclerView创建自定义适配器?

时间:2020-03-11 10:48:32

标签: android android-recyclerview android-adapter

我遵循了the official Android tutorialthis tutorial,但是它们确实有所不同,我不知道如何创建自定义适配器。

我有一个RecyclerView,我想在其中显示一些音乐,因此在我的自定义适配器中,我做了:

   public class SongPlayListAdapter extends RecyclerView.Adapter<SongPlayListAdapter.MyViewHolder> {
    private ArrayList<String> songList;
    private Context context;
    private int viewRes;
    private Resources res;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class MyViewHolder extends RecyclerView.ViewHolder {  
        /* here it's wrong but I don't know what to do 
        R.id.title_song_playlist TextView mTitleSongPlaylistTextView;
        R.id.group_playlist TextView mGroupSongPlaylistTextView;
        R.id.preview_song_playlist ImageView mPreviewSongPlaylistImageView; */

        // each data item is just a string in this case
        public TextView textView;
        public MyViewHolder(TextView v) {
            super(v);
            textView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public SongPlayListAdapter(Context context, int viewResourceId,
                               ArrayList<String> songList) {
        this.songList = songList;
        this.context = context;
        this.viewRes = viewResourceId;
        this.res = context.getResources();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // create a new view
        TextView title_song = (TextView);

        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    }

    @Override
    public int getItemCount() {
        return songList.size();
    }
}

问题是我不知道如何填充我的RecyclerView,也不知道重写方法中应该包含什么。我用一个普通列表来做,而我的适配器是这样的:

          View view = convertView;
        if (view == null) {
            LayoutInflater layoutInflater = (LayoutInflater)
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(viewRes, parent, false);
        }
        final String song= songList.get(position);
        if (song != null) {
            final TextView title = (TextView) view.findViewById(R.id.title_song);
            final TextView group = (TextView) view.findViewById(R.id.group_song);
            final ImageView image= (ImageView) view.findViewById(R.id.image_song);
            title.setText(song);
            description.setText(song);
        }
        return view;
    }

那么我如何设法修复适配器?我真的迷路了,本教程也没有帮助

2 个答案:

答案 0 :(得分:2)

您可以使用类似这样的持有人,在绑定数据中,您将为每个行项目设置数据。可以按照下面的示例

public class RestaurantListAdapter extends RecyclerView.Adapter<RestaurantListAdapter.RestaurantViewHolder> {
    private ArrayList<Restaurant> mRestaurants = new ArrayList<>();
    private Context mContext;

    public RestaurantListAdapter(Context context, ArrayList<Restaurant> restaurants) {
        mContext = context;
        mRestaurants = restaurants;
    }

    public class RestaurantViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.restaurantImageView) ImageView mRestaurantImageView;
        @Bind(R.id.restaurantNameTextView) TextView mNameTextView;
        @Bind(R.id.categoryTextView) TextView mCategoryTextView;
        @Bind(R.id.ratingTextView) TextView mRatingTextView;

        private Context mContext;

        public RestaurantViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            mContext = itemView.getContext();
        }

        public void bindRestaurant(Restaurant restaurant) {
            mNameTextView.setText(restaurant.getName());
            mCategoryTextView.setText(restaurant.getCategories().get(0));
            mRatingTextView.setText("Rating: " + restaurant.getRating() + "/5");
        }
    }
}

答案 1 :(得分:1)

这是自定义适配器。我希望它能满足您的需求,并且通过创建它,我没有犯任何错误。搜索一些使用RecyclerView的Room或Retrofit教程。如果您多次执行Recycler,它将变得更加清晰。

public class CustomAdapter extends ListAdapter<CustomItem, CustomAdapter.CustomHolder> {
    private OnItemClickListener listener;

    @NonNull
    @Override
    public CustomHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.custom_item, parent, false);

        return new CustomHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull CustomHolder holder, int position) {
        CustomItem currentCustomItem = getItem(position);
        holder.textViewTitle.setText(currentCustomItem.getTitle());
        holder.textViewDescription.setText(currentCustomItem.getDescription());
    }

    public CustomItem getCustomItemAt(int position) {
        return getItem(position);
    }

    class CustomHolder extends RecyclerView.ViewHolder {
        private TextView textViewTitle;
        private TextView textViewDescription;

        public NoteHolder(@NonNull View itemView) {
            super(itemView);
            textViewTitle = itemView.findViewById(R.id.text_view_title);
            textViewDescription = itemView.findViewById(R.id.text_view_description);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (listener != null && position != RecyclerView.NO_POSITION) {
                        listener.onItemClick(getItem(position));
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(Note note);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}