如何将BaseAdapter转换为RecyclerAdapter

时间:2019-06-30 21:20:10

标签: java android-recyclerview recycler-adapter

因此,我想将GridView更改为RecyclerView,并因此不得不将BaseAdapter更改为RecyclerAdapter。 我已经尝试进行更改,但是不知道如何将代码切换到RecyclerAdapter。 这是我的BaseAdapter的样子:

class AlbumAdapter extends BaseAdapter {
    private Activity activity;
    private ArrayList<HashMap< String, String >> data;
    public AlbumAdapter(Activity a, ArrayList < HashMap < String, String >> d) {
        activity = a;
        data = d;
    }
    public int getCount() {
        return data.size();
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        AlbumViewHolder holder = null;
        if (convertView == null) {
            holder = new AlbumViewHolder();
            convertView = LayoutInflater.from(activity).inflate(
                    R.layout.album_row, parent, false);

            holder.galleryImage = (ImageView) convertView.findViewById(R.id.galleryImage);
            holder.gallery_count = (TextView) convertView.findViewById(R.id.gallery_count);
            holder.gallery_title = (TextView) convertView.findViewById(R.id.gallery_title);

            convertView.setTag(holder);
        } else {
            holder = (AlbumViewHolder) convertView.getTag();
        }
        holder.galleryImage.setId(position);
        holder.gallery_count.setId(position);
        holder.gallery_title.setId(position);

        HashMap < String, String > song = new HashMap < String, String > ();
        song = data.get(position);
        try {
            holder.gallery_title.setText(song.get(Function.KEY_ALBUM));
            holder.gallery_count.setText(song.get(Function.KEY_COUNT));

            Glide.with(activity)
                    .load(new File(song.get(Function.KEY_PATH))) // Uri of the picture
                    .into(holder.galleryImage);

        } catch (Exception e) {}
        return convertView;
    }
}


class AlbumViewHolder {
    ImageView galleryImage;
    TextView gallery_count, gallery_title;
}

在此先感谢您!

1 个答案:

答案 0 :(得分:1)

  
    

只需使用此功能,即可随时进行修改以满足您的需要,请务必阅读注释,因为它们确实可以帮助您了解一些内容。

  
public class AlbumAdapter extends RecyclerView.Adapter<AlbumAdapter.ViewHolder> {

    private static final String KEY_ALBUM = "KEY_ALBUM";
    private static final String KEY_COUNT = "KEY_COUNT";
    private static final String KEY_PATH = "KEY_PATH";

    private itemClickInterface clickInterface;
    //private List<String> data;
    private ArrayList<HashMap<String, String >> data;

//    public AlbumAdapter(itemClickInterface clickInterface, List<String> data) { // Forget about this if your data is not an array of Strings.
//    public AlbumAdapter(itemClickInterface clickInterface, ArrayList<HashMap< String, String >> data) { // Forget about this if you're not ready to pass an onclick interface
    public AlbumAdapter(ArrayList<HashMap< String, String >> data) {
        this.data = data;
//        this.clickInterface = clickInterface; //Simply ignore since you're not passing an interface of clicklister
    }

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

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        try {
            final String data_for_albums = this.data.get(position).get(KEY_ALBUM);
            final String data_for_counts = this.data.get(position).get(KEY_COUNT);
            final String data_for_paths = this.data.get(position).get(KEY_PATH);

            holder.gallery_title.setText(data_for_albums);
            holder.gallery_count.setText(data_for_counts);

            Glide.with(activity)
                    .load(new File(data_for_paths)) // Uri of the picture
                    .into(holder.galleryImage);



            /*
            * You can modify this as you want.
            * */
//            holder.galleryImage.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    clickInterface.click(data_for_paths); // This depends on you.
//                }
//            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

    /*--------------------------------------------------------------------------------------
     |                          GET REFERENCES TO VIEWS HERE
     *--------------------------------------------------------------------------------------*/
    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView galleryImage;
        TextView gallery_count, gallery_title;

        ViewHolder(View itemView) {
            super(itemView);
            galleryImage = itemView.findViewById(R.id.galleryImage);
            gallery_count = itemView.findViewById(R.id.gallery_count);
            gallery_title = itemView.findViewById(R.id.gallery_title);
        }
    }
}
  
    

如何通过我们的活动致电

  
ArrayList<HashMap<String, String >> data = new ArrayList<>();
        //I'm assuming you already feed your data, so you're not passing null like me here.

        cardlistview = findViewById(R.id.cardlistview);

        albumAdapter = new AlbumAdapter(your_arraysOfHashMap);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        cardlistview.setLayoutManager(mLayoutManager);
        cardlistview.setAdapter(albumAdapter);