Created
February 25, 2016 05:55
-
-
Save arifnoumankhan/75e8392d0868a08484b6 to your computer and use it in GitHub Desktop.
RecyclerView Custom Adapter which have one image 2 text views and a button for deleting rows from recyclerview.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class RecyclerViewCustomAdapter extends RecyclerView.Adapter<RecyclerViewCustomAdapter.SimpleViewHolder> { | |
| String[] dataset; | |
| Context context; | |
| ArrayList<String> datasetList=new ArrayList<>(); | |
| // 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 class SimpleViewHolder extends RecyclerView.ViewHolder { | |
| // each data item is just a string in this case | |
| public TextView txtvwEachItem; | |
| public TextView txtvwDescreption; | |
| public Button btnDelete; | |
| private ImageView imageView; | |
| public SimpleViewHolder(final View itemView) { | |
| super(itemView); | |
| txtvwEachItem= (TextView) itemView.findViewById(R.id.txtvw); | |
| txtvwDescreption= (TextView) itemView.findViewById(R.id.txtvwDescription); | |
| btnDelete= (Button) itemView.findViewById(R.id.btnDelete); | |
| imageView= (ImageView) itemView.findViewById(R.id.imageview); | |
| } | |
| } | |
| // Provide a suitable constructor (depends on the kind of dataset) | |
| RecyclerViewCustomAdapter(Context context,String[]dataset) | |
| { | |
| this.dataset=dataset; | |
| this.datasetList= new ArrayList<>(Arrays.asList(dataset)); | |
| this.context=context; | |
| } | |
| // Create new views (invoked by the layout manager) | |
| @Override | |
| public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
| View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.recylerview_item,parent,false); | |
| SimpleViewHolder viewHolder=new SimpleViewHolder(view); | |
| return viewHolder; | |
| } | |
| // Replace the contents of a view (invoked by the layout manager) | |
| @Override | |
| public void onBindViewHolder(SimpleViewHolder holder, final int position) { | |
| // - get element from your dataset at this position | |
| // - replace the contents of the view with that element | |
| holder.btnDelete.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| datasetList.remove(position); | |
| notifyItemRemoved(position); | |
| notifyItemRangeChanged(position,datasetList.size()); | |
| notifyItemRangeRemoved(position,datasetList.size()); | |
| Toast.makeText(context, ""+position, Toast.LENGTH_SHORT).show(); | |
| } | |
| }); | |
| holder.txtvwEachItem.setText(datasetList.get(position)); | |
| holder.txtvwDescreption.setText(""+(position+1)); | |
| holder.imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.image)); | |
| } | |
| @Override | |
| public int getItemCount() { | |
| return datasetList.size(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment