Java 如何将BaseAdapter转换为RecyclerAdapter

Java 如何将BaseAdapter转换为RecyclerAdapter,java,android-recyclerview,Java,Android Recyclerview,因此,我想将我的GridView更改为recycleraview,然后我不得不将BaseAdapter更改为RecyclerAdapter。 我已经尝试过进行更改,但我不知道如何将代码切换到RecyclerAdapter。 下面是我的BaseAdapter的外观: class AlbumAdapter extends BaseAdapter { private Activity activity; private ArrayList<HashMap< String,

因此,我想将我的
GridView
更改为
recycleraview
,然后我不得不将
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;
}
类AlbumAdapter扩展了BaseAdapter{ 私人活动; 私有数组列表>数据; 公共AlbumAdapter(活动a,ArrayList>d){ 活动=a; 数据=d; } public int getCount(){ 返回data.size(); } 公共对象getItem(int位置){ 返回位置; } 公共长getItemId(int位置){ 返回位置; } 公共视图getView(int位置、视图转换视图、视图组父视图){ AlbumViewHolder=null; if(convertView==null){ holder=新相册视图holder(); convertView=LayoutFlater.from(活动)。充气( R.layout.album_行,父级,false); holder.galleryImage=(ImageView)convertView.findViewById(R.id.galleryImage); holder.gallery\u count=(TextView)convertView.findViewById(R.id.gallery\u count); holder.gallery\u title=(TextView)convertView.findViewById(R.id.gallery\u title); convertView.setTag(支架); }否则{ holder=(AlbumViewHolder)convertView.getTag(); } 夹具图像设置ID(位置); 支架.库号\计数.设置ID(位置); 持有人。画廊名称。setId(位置); HashMapsong=newhashmap(); 宋=数据。获取(位置); 试一试{ holder.gallery_title.setText(song.get(Function.KEY_相册)); holder.gallery_count.setText(song.get(Function.KEY_count)); 滑翔(活动) .load(新文件(song.get(Function.KEY_PATH))//图片的Uri .进入(支架.galleryImage); }捕获(例外e){} 返回视图; } } 类相册视图持有者{ 图像视图画廊图像; TextView图库计数、图库标题; } 提前谢谢你

只需简单地使用它,根据您的需要随意修改,确保阅读评论,因为它们确实可以帮助您理解一些事情。

公共类AlbumAdapter扩展了RecyclerView.Adapter{ 私有静态最终字符串KEY\u ALBUM=“KEY\u ALBUM”; 私有静态最终字符串KEY\u COUNT=“KEY\u COUNT”; 私有静态最终字符串KEY\u PATH=“KEY\u PATH”; 私有项点击界面点击界面; //私人名单数据; 私有数组列表数据; //公共AlbumAdapter(itemClickInterface clickInterface,List data){//如果您的数据不是字符串数组,请忘记这一点。 //PublicAlbumAdapter(itemClickInterface clickInterface,ArrayList>data){//如果您还没有准备好传递onclick接口,请忘记这一点 公共相册适配器(ArrayList>data){ 这个数据=数据; //this.clickInterface=clickInterface;//只需忽略,因为您没有传递clicklister的接口 } @凌驾 public ViewHolder onCreateViewHolder(视图组父级,int-viewType){ View=LayoutInflater.from(parent.getContext()) .充气(R.layout.album_行,父级,假); 返回新的ViewHolder(视图); } @凌驾 公共无效onBindViewHolder(最终ViewHolder,内部位置){ 试一试{ 相册的最终字符串数据=this.data.get(位置).get(键相册); 用于计数的最终字符串数据=this.data.get(位置).get(键计数); _路径的最终字符串数据_=this.data.get(位置).get(键路径); holder.gallery\u title.setText(相册的数据); holder.gallery_count.setText(用于计数的数据); 滑翔(活动) .load(新文件(数据路径的数据路径))//图片的Uri .进入(支架.galleryImage); /* *您可以根据需要对此进行修改。 * */ //holder.galleryImage.setOnClickListener(新视图.OnClickListener(){ //@覆盖 //公共void onClick(视图v){ //单击接口。单击(数据路径);//这取决于您。 // } // }); }捕获(例外e){ e、 printStackTrace(); } } @凌驾 public int getItemCount(){ 返回data.size(); } /*-------------------------------------------------------------------------------------- |在此处获取对视图的引用 *--------------------------------------------------------------------------------------*/ 类ViewHolder扩展了RecyclerView.ViewHolder{ 图像视图画廊图像; TextView图库计数、图库标题; ViewHolder(视图项视图){ 超级(项目视图); galleryImage=itemView.findViewById(R.id.galleryImage); gallery\u count=itemView.findViewById(R.id.gallery\u count); gallery\u title=itemView.findViewById(R.id.gallery\u title); } } } 如何从我们的活动中拨打电话

ArrayList data=new ArrayList();
//我假设你已经输入了数据,所以你不会像我一样传递null。
cardlistview=findviewbyd(R.id.cardlistview);
albumAdapter=新的albumAdapter(您的\u arraysOfHashMap);
RecyclerView.LayoutManager mLayoutManager=新的LinearLayoutManager(getApplicationContext());
设置布局管理器(mLayoutManager);
设置适配器(albumAdapter);
只需使用此功能,即可随意修改以适合您的nee
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);