I am using recyclerview, in my list item I have button. On that button click I am hiding and showing imageview, now if user click on list item and open imageview and then scrolldown and scrollup, the item in which user opened imageview is getting closed. This is my adapter code:
public class NotiDetailsListAdapter extends RecyclerView.Adapter<NotiDetailsListAdapter.ViewHolder> {
Context context;
private List<NotificationListData> adaBankDetailsList;
String bnkName = "", bnkAccNum = "", userName = "", bankIfscCode = "", bankType = "";
private int selectedPosition = -1;
boolean flag = true;
private NotiDetailsListAdapter(Context conytext, List<NotificationListData> bankDetails) {
this.context = conytext;
this.adaBankDetailsList = bankDetails;
}
@NonNull
@Override
public NotiDetailsListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view;
view = inflator.inflate(R.layout.notification_list_item_layout, parent, false);
return new NotiDetailsListAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final NotiDetailsListAdapter.ViewHolder holder, int pos) {
bnkResponse = adaBankDetailsList.get(pos);
try {
holder.notification_main_text.setText(bnkResponse.getNotificationTitle());
holder.notification_sub_text.setText(bnkResponse.getNotificationMessage());
String mydates=bnkResponse.getNotificationSendTime();
String[] str_array1 = mydates.split("-");
if(str_array1[0] != null && str_array1[0].length() > 0 && str_array1[1] != null && str_array1[1].length() > 0) {
stringa1 = str_array1[0];
stringb1 = str_array1[1];
}
else
{
stringa1 = str_array1[0];
stringb1 = "";
}
String[] timearray = stringb1.split(":");
if(timearray[0] != null && timearray[0].length() > 0 && timearray[1] != null && timearray[1].length() > 0) {
stringt1 = timearray[0];
stringt2 = timearray[1];
}
else
{
stringt1 = timearray[0];
stringt2 = "";
}
if(mydates.contains("PM"))
{
stringampm="PM";
}
else if(mydates.contains("AM"))
{
stringampm="AM";
}
System.out.println("Dates "+stringampm+" "+stringt2);
holder.notification_date.setText(stringa1);
holder.notification_time.setText(stringt1+":"+stringt2+" "+stringampm);
holder.notification_iv.setVisibility(View.GONE);
holder.notification_main_text.setSingleLine(false);
holder.notification_main_text.setEllipsize(TextUtils.TruncateAt.END);
int n1 = 1; // the exact number of lines you want to display
holder.notification_main_text.setLines(n1);
holder.notification_main_text.setMaxEms(12);
holder.notification_sub_text.setSingleLine(false);
holder.notification_sub_text.setEllipsize(TextUtils.TruncateAt.END);
int n = 1; // the exact number of lines you want to display
holder.notification_sub_text.setLines(n);
holder.iv_arrow.setImageResource(R.mipmap.down);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return adaBankDetailsList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView notification_main_text, notification_sub_text, notification_date, notification_time;
ImageView iv_arrow,notification_iv;
RelativeLayout parentRL,notification_rl;
LinearLayout arrow_click;
public ViewHolder(View itemView) {
super(itemView);
notification_main_text = itemView.findViewById(R.id.notification_main_text);
notification_sub_text = itemView.findViewById(R.id.notification_sub_text);
notification_date = itemView.findViewById(R.id.notification_date);
notification_time = itemView.findViewById(R.id.notification_time);
iv_arrow = itemView.findViewById(R.id.iv_arrow);
notification_iv = itemView.findViewById(R.id.notification_iv);
parentRL = itemView.findViewById(R.id.parentRL);
notification_rl = itemView.findViewById(R.id.notification_rl);
arrow_click = itemView.findViewById(R.id.arrow_click);
// Store the context
// Attach a click listener to the entire row view
parentRL.setOnClickListener(this);
iv_arrow.setOnClickListener(this);
/* itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});*/
}
@Override
public void onClick(View view) {
int position = getAdapterPosition();
switch (view.getId()){
case R.id.parentRL:
try {
if (adaBankDetailsList.get(position).getNotificationLevel().equalsIgnoreCase("promotion")) {
Intent notificationIntent = new Intent(NotificationListActivity.this, InviteEarnWebViewActivity.class);
notificationIntent.putExtra("notificationClick","true");
//notificationIntent.putExtra("notifId",(bnkResponse.getLevelId()));
notificationIntent.putExtra("searchQuery", adaBankDetailsList.get(position).getCustomPageUrl());
notificationIntent.putExtra("headerName", adaBankDetailsList.get(position).getCustomPageTitle());
notificationIntent.putExtra("showShareEarn","N");
startActivity(notificationIntent);
}else if (adaBankDetailsList.get(position).getNotificationLevel().equalsIgnoreCase("update")) {
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse("market://details?id=com.milmila.android.milmila_home"));
startActivity(notificationIntent);
}else if (adaBankDetailsList.get(position).getNotificationLevel().equalsIgnoreCase("home")) {
Intent notificationIntent = new Intent(NotificationListActivity.this, BottomNavigationActivity.class);
notificationIntent.putExtra("notificationClick","true");
//notificationIntent.putExtra("notifId",(bnkResponse.getLevelId()));
startActivity(notificationIntent);
}else if (adaBankDetailsList.get(position).getNotificationLevel().equalsIgnoreCase("invite")) {
Intent notificationIntent = new Intent(NotificationListActivity.this, InviteEarnWebViewActivity.class);
notificationIntent.putExtra("notificationClick","true");
// notificationIntent.putExtra("notifId",(bnkResponse.getLevelId()));
notificationIntent.putExtra("searchQuery", Constants.BASE_URL+"customerInvite/inviteEarn.html?customerId=#sharedPreferenceUserId#");
notificationIntent.putExtra("headerName","Invite & Earn");
notificationIntent.putExtra("showShareEarn","Y");
startActivity(notificationIntent);
}else if (adaBankDetailsList.get(position).getNotificationLevel().equalsIgnoreCase("share_earn")) {
Intent notificationIntent = new Intent(NotificationListActivity.this, InviteEarnWebViewActivity.class);
notificationIntent.putExtra("notificationClick","true");
// notificationIntent.putExtra("notifId",(bnkResponse.getLevelId()));
notificationIntent.putExtra("searchQuery",Constants.BASE_URL+"double-dhamaka.html?customerId=#sharedPreferenceUserId#");
notificationIntent.putExtra("headerName","Gift your friend 100");
notificationIntent.putExtra("showShareEarn","Y");
startActivity(notificationIntent);
} else if (adaBankDetailsList.get(position).getNotificationLevel().equalsIgnoreCase("product")) {
Intent notificationIntent = new Intent(NotificationListActivity.this, ProductDetailsActivity.class);
notificationIntent.putExtra("notificationClick","true");
notificationIntent.putExtra(STRING_IMAGE_PRODUCTID,(adaBankDetailsList.get(position).getLevelId()));
// notificationIntent.putExtra(STRING_IMAGE_PRODUCTID, levelId);
// notificationIntent.putExtra(STRING_IMAGE_PRODUCT_URL, "");
startActivity(notificationIntent);
}else if (adaBankDetailsList.get(position).getNotificationLevel().equalsIgnoreCase("Earnings")) {
Intent notificationIntent = new Intent(NotificationListActivity.this, BottomNavigationActivity.class);
notificationIntent.putExtra("notificationClick","true");
// notificationIntent.putExtra("notifId",(bnkResponse.getLevelId()));
notificationIntent.putExtra("mmcash", "Y");
startActivity(notificationIntent);
} else if (adaBankDetailsList.get(position).getNotificationLevel().equalsIgnoreCase("group")) {
Intent notificationIntent = new Intent(NotificationListActivity.this, GroupProductListActivity.class);
notificationIntent.putExtra("notificationClick","true");
notificationIntent.putExtra("categoryID",(adaBankDetailsList.get(position).getLevelId()));
// notificationIntent.putExtra("categoryID", levelId);
notificationIntent.putExtra("categoryName", adaBankDetailsList.get(position).getGroupName());
notificationIntent.putExtra("limit", 10);
// notificationIntent.putExtra("headerImage", promotionUrl);
startActivity(notificationIntent);
}else if (adaBankDetailsList.get(position).getNotificationLevel().equalsIgnoreCase("reseller")) {
Intent notificationIntent = new Intent(NotificationListActivity.this, BottomNavigationActivity.class);
notificationIntent.putExtra("notificationClick","true");
// notificationIntent.putExtra("notifId",(bnkResponse.getLevelId()));
notificationIntent.putExtra("resell", "Y");
startActivity(notificationIntent);
} else if (adaBankDetailsList.get(position).getNotificationLevel().equalsIgnoreCase("normal")) {
Intent notificationIntent = new Intent(NotificationListActivity.this, MyOrdersActivity.class);
notificationIntent.putExtra("notificationClick","true");
// notificationIntent.putExtra("notifId",(bnkResponse.getLevelId()));
startActivity(notificationIntent);
} else {
Intent notificationIntent = new Intent(NotificationListActivity.this, BottomNavigationActivity.class);
notificationIntent.putExtra("notificationClick","true");
// notificationIntent.putExtra("notifId",(bnkResponse.getLevelId()));
startActivity(notificationIntent);
}
} catch (Exception e) {
Intent notificationIntent = new Intent(NotificationListActivity.this, BottomNavigationActivity.class);
startActivity(notificationIntent);
}
break;
case R.id.iv_arrow:
Glide.with(getApplicationContext())
.load(adaBankDetailsList.get(position).getNotificationImage())
.apply(new RequestOptions()
.placeholder(R.drawable.no_image))
.into(notification_iv);
if(flag==true)
{
notification_main_text.setSingleLine(true);
notification_main_text.setEllipsize(TextUtils.TruncateAt.END);
int n1 = 1; // the exact number of lines you want to display
notification_main_text.setLines(n1);
notification_main_text.setMaxEms(12);
iv_arrow.setImageResource(R.mipmap.down);
notification_iv.setVisibility(View.GONE);
notification_sub_text.setSingleLine(true);
notification_sub_text.setEllipsize(TextUtils.TruncateAt.END);
int n = 1; // the exact number of lines you want to display
notification_sub_text.setLines(n);
flag = false;
}
else if(flag==false)
{
notification_main_text.setSingleLine(false);
int n1 = 2; // the exact number of lines you want to display
notification_main_text.setLines(n1);
notification_main_text.setMaxEms(10);
notification_main_text.setVisibility(View.VISIBLE);
iv_arrow.setImageResource(R.mipmap.up);
notification_sub_text.setVisibility(View.VISIBLE);
if(adaBankDetailsList.get(position).getNotificationImage().equals("null"))
{
notification_iv.setVisibility(View.GONE);
}
else
{
notification_iv.setVisibility(View.VISIBLE);
}
notification_sub_text.setSingleLine(false);
int n = 2; // the exact number of lines you want to display
notification_sub_text.setLines(n);
flag = true;
}
break;
}
}
}
}
Sometime Adapter
might cause position issue while scrolling the Recyclerview
. You can achieve the same by tag your ImageView
into the Button.
holder.your_button.setTag(holder.your_imageview);
holder.your_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ImageView selImageView = (ImageView) v.getTag();
if(selImageView.getVisibility() == View.VISIBLE) {
selImageView.setVisibility(View.INVISIBLE)
} else {
selImageView.setVisibility(View.VISIBLE)
}
}
});
Above will only trigger your view of the same position. Because we have set that ImageView
to your Buttons
tag and targeting only that view.