Home > Net >  Convert baseadapter to recyclerview adapter
Convert baseadapter to recyclerview adapter

Time:08-02

Convert baseadapter to recyclerview adapter i have a code of baseadapterview but i need recyclerview adapter my view is showing vertical scrollview and i need horizontal .................................................................................................................................................................... as mentioned above please help me to get rid of this problem

public class imgAdapter extends BaseAdapter {
private final ArrayList<HashMap<String, String>> list;
private final LayoutInflater inflater;
private final int layout;

public imgAdapter(Context context, ArrayList<HashMap<String, String>> list, int layout) {
inflater = LayoutInflater.from(context);
this.list = list;
this.layout = layout;
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int i) {
return list.get(i).get("file");
}

@Override
public long getItemId(int i) {
return 0;
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
if (v == null) v = inflater.inflate(layout, viewGroup, false);
TextView titleView = v.findViewById(R.id.home_item_titleView);
titleView.setText(list.get(i).get("name"));
ImageView imageView = v.findViewById(R.id.home_item_imageView)
   }
  }

CodePudding user response:

Here's a detailed guide about using RecyclerView -> Create dynamic lists with RecyclerView. I recommend reading it with understanding.

And here's a dummy Kotlin example of the adapter you're looking for:

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class ImgAdapter(
    private val layoutId: Int
) : RecyclerView.Adapter<ImgAdapter.ImgViewHolder>() {

    private val items = mutableListOf<HashMap<String, String>>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ImgViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
        return ImgViewHolder(view)
    }

    override fun onBindViewHolder(holder: ImgViewHolder, position: Int) {
        holder.setupWith(items[position])
    }

    override fun getItemCount(): Int {
        return items.size
    }

    // TODO: call it when your items list is ready
    fun setItems(newItems: List<HashMap<String, String>>) {
        items.apply {
            clear()
            addAll(newItems)
        }
        notifyDataSetChanged() // TODO: change the HashMap to some model class and use DiffUtil here
    }

    class ImgViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        private val titleView by lazy { itemView.findViewById<TextView>(R.id.home_item_titleView) }
        private val imageView by lazy { itemView.findViewById<ImageView>(R.id.home_item_imageView) }

        fun setupWith(item: HashMap<String, String>) {
            titleView.text = item["name"]
            // TODO: setup other views
        }
    }
}

CodePudding user response:

In order to get a horizontal recycler view just make changes to the layout manager in main activity.

LinearLayoutManager lm = new LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false);

In your adapter, you have to implement recycler view adapter functions. You can take help from the android documentation

Here is a sample code of the recyclerView adapter :


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

    private String[] localDataSet;

    /**
     * Provide a reference to the type of views that you are using
     * (custom ViewHolder).
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView textView;

        public ViewHolder(View view) {
            super(view);
            // Define click listener for the ViewHolder's View

            textView = (TextView) view.findViewById(R.id.textView);
        }

        public TextView getTextView() {
            return textView;
        }
    }

    /**
     * Initialize the dataset of the Adapter.
     *
     * @param dataSet String[] containing the data to populate views to be used
     * by RecyclerView.
     */
    public CustomAdapter(String[] dataSet) {
        localDataSet = dataSet;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        // Create a new view, which defines the UI of the list item
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.text_row_item, viewGroup, false);

        return new ViewHolder(view);
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {

        // Get element from your dataset at this position and replace the
        // contents of the view with that element
        viewHolder.getTextView().setText(localDataSet[position]);
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return localDataSet.length;
    }
}

  • Related