Skip to content

Instantly share code, notes, and snippets.

@esabook
Created May 15, 2019 15:38
Show Gist options
  • Select an option

  • Save esabook/c5e854336a4040e2c79ec3057dbf3014 to your computer and use it in GitHub Desktop.

Select an option

Save esabook/c5e854336a4040e2c79ec3057dbf3014 to your computer and use it in GitHub Desktop.
recycleview usage for horizontal scrolled item
public class __ReportImageAttachmentAdapter extends RecyclerView.Adapter<__ReportImageAttachmentAdapter.ViewHolder> {
public final static String TEMP_URI_IMAGE_ATTACHMENT = "tmpAttach://";
public static final int ACTION_CLICK = 0;
public static final int ACTION_DELETE = -1;
public static final int ACTION_SAVE = 1;
public static final int ACTION_CHANGED = 3;
private final ArrayList<String> mItems;
private ActionListener mListener;
public __ReportImageAttachmentAdapter(ArrayList<String> items) {
mItems = items;
}
/**
* @param listener
*/
public void setListener(ActionListener listener) {
this.mListener = listener;
}
/**
* @param parent
* @param viewType
* @return
*/
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
/**
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String url = mItems.get(position);
if (url.contains(TEMP_URI_IMAGE_ATTACHMENT)) {
url = url.replace(TEMP_URI_IMAGE_ATTACHMENT, "");
}
// show delete button
Handler h = new Handler(Looper.getMainLooper());
Runnable r = () -> holder.mBinding.delete.setVisibility(View.GONE);
holder.mBinding.image.setOnLongClickListener(v -> {
holder.mBinding.delete.setVisibility(View.VISIBLE);
h.removeCallbacks(r);
h.postDelayed(
r,
5000);
return true;
});
// hide delete button or open action
String finalUrl = url;
holder.mBinding.image.setOnClickListener(v -> {
if (holder.mBinding.delete.getVisibility() == View.VISIBLE) {
holder.mBinding.delete.setVisibility(View.GONE);
} else {
if (mListener != null) mListener.OnAction(ACTION_CLICK, position, finalUrl);
}
});
// delete action
holder.mBinding.delete.setOnClickListener(v -> {
if (mListener == null) return;
mListener.OnAction(ACTION_DELETE, position, finalUrl);
mListener.OnAction(__ReportImageAttachmentAdapter.ACTION_CHANGED, -1, null);
});
//load image data
Uri uri = Uri.parse(url);
Glide.with(holder.mBinding.getRoot())
.load(uri)
.into(holder.mBinding.image);
}
public void notifyDataSetChanged2() {
notifyDataSetChanged();
if (mListener != null)
mListener.OnAction(__ReportImageAttachmentAdapter.ACTION_CHANGED, -1, null);
}
/**
* @return
*/
@Override
public int getItemCount() {
return mItems.size();
}
@Retention(RetentionPolicy.SOURCE)
@IntDef({ACTION_DELETE, ACTION_CLICK, ACTION_SAVE, ACTION_CHANGED})
public @interface ActionType {
}
/**
*
*/
public interface ActionListener {
void OnAction(@ActionType int actionType, int itemPosition, String value);
}
/**
*
*/
public class ViewHolder extends RecyclerView.ViewHolder {
ReportImageAttachmentItemBinding mBinding;
ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.__report_image_attachment_item, parent, false));
mBinding = DataBindingUtil.bind(itemView);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment