joshua-r2
10/12/2017 - 5:33 PM

BasicRecyclerView

An easily readable RecyclerView Example with a custom layout for each "card"/view

public class YourStupidRecyclerAdapter extends RecyclerView.Adapter<YourStupidRecyclerAdapter.CustomViewHolderInThisFile>{

    //A simple collection of Data for this example
    public static String[] dataSet = {"Neat", "Cool", "Wow"};

    // 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 CustomViewHolderInThisFile extends RecyclerView.ViewHolder{
        public View datView;
        public CustomViewHolderInThisFile(SomeView v){
            super(v);
            datView=v;
        }
    }


    //Constructor
    public YourStupidRecyclerAdapter(String[] yourStupidData){
		dataSet=yourStupidData;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public CustomViewHolderInThisFile onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.yourCardViewOrLine, parent, false);
        // set the view's size, margins, paddings and layout parameters
        CustomViewHolderInThisFile customViewHolderInThisFile = new CustomViewHolderInThisFile(tvEstimator);
        return CustomViewHolderInThisFile;
    }

    // Replace the contents of a view (invoked by the layout manager)
	//This is how you populate the data for each item
    @Override
    public void onBindViewHolder(CustomViewHolderInThisFile holder, int position) {
        holder.datView.setText(dataSet[position]);
    }

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