to begin I would thanks those who help noobi dev like me :p! Let's say that I have a Recyclerview in which every unit element has two images. Actually I am able to find the indice of the unit element clicked to fire the good evenment thanks to this little trick in MyAdapter:
#Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model, parent, false);
final MyHolder holder = new MyHolder(v);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onItemClick(v, holder.getAdapterPosition());
}
});
return holder;
}
Then I start the event this way in the activity:
MyAdapter adapter = new MyAdapter(this, countryCollection.getCountryArray(), new CustomItemClickListener() {
#Override
public void onItemClick(View v, int position) {
//Launch the events
ArrayList<String> languageCodeArray = countryCollection.getLanguageCodeArray();
ArrayList<String> countryIdArray = countryCollection.getCountryIdArray();
if(position >0){
String languageCode = languageCodeArray.get(position-1);
String countryId = countryIdArray.get(position-1);
Intent intent = new Intent(context, LetsSpeakActivity.class);
intent.putExtra("country_id", countryId);
intent.putExtra("language_code", languageCode);
startActivity(intent);
}
}
});
rv.setAdapter(adapter);
But how can I différentiate the two images of my RecyclerView in order to fire the good event? I've been searching for a while how to differentiate a click on the left or right side of the screen but it is obviously not the right way to do ! Any help would be lovely, I am really stuck...
Supposing you have defined both ImageView in your MyHolder class, you could set OnClickListener on both of them in your holder constructor :
public class MyHolder extends RecyclerView.ViewHolder {
public ImageView imageLeft;
public ImageView imageRight;
public CustomItemClickListener listenerLeft;
public CustomItemClickListener listenerRight;
public MyHolder(View v, CustomItemClickListener listenerLeft, CustomItemClickListener listenerRight) {
super(v);
this.listenerLeft = listenerLeft;
this.listenerRight = listenerRight;
imageLeft = v.findViewById(R.id.image_left);
imageRight = v.findViewById(R.id.image_right);
imageLeft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
listenerLeft.onItemClick(v, getPosition());
}
});
imageRight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
listenerRight.onItemClick(v, getPosition());
}
});
}
}
while injecting both listener in onCreateViewHolder :
#Override
public MyAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.model, parent, false);
return new MyHolder(v, mImageLeftListener, mImageRightListener);
}
You can introduce both of these listener in your Adapter constructor like you did with listener
Or if you want to pass only one listener, you can define your CustomItemClickListener interface with 2 methods (or just 1 with an additional param to differentiate the image clicked) :
public interface CustomItemClickListener {
void onLeftImageClick(View view, int position);
void onRightImageClick(View view, int position);
}
Related
I have an adapter class Where i Want to show a BLOCK USER dialog on particular position.
for example if i want to block position 1 of recyclerView .
So can any one help me porgrammatically?
Given below is the Code Of adapter Please check
I want to block user profile based on Recycler View position
if someone could help me in it please;
public class TableProfileAdapter extends RecyclerView.Adapter<TableProfileAdapter.MyViewHolder> {
Context context;
Activity getActivity;
List<TableProfile> profileUser;
OnRecItemClick onRecItemClick;
private boolean isScaleAnimationDone = false;
private boolean isTimerTextViewActionUpCalled = false;
public TableProfileAdapter(List<TableProfile> profileUser, Context context) {
this.profileUser = profileUser;
this.context = context;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.profile_item, parent, false);
return new MyViewHolder(view, onRecItemClick);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
TableProfile data = profileUser.get(position);
holder.tvUserName.setText(data.getProfileName());
Bitmap image = BitmapManager.byteToBitmap(data.getProfileImage());
holder.profilePic.setImageBitmap(image);
if (context instanceof AccessProfileManagementActivity) {
context.startActivity(new Intent(context,HomeActivity.class));
} else if (context instanceof ProfileManagement) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, ProfileDetailsActivity.class);
TableProfile model = profileUser.get(holder.getAdapterPosition());
intent.putExtra("DATA", model);
context.startActivity(intent);
notifyDataSetChanged();
((Activity) context).finish();
}
});
}
}
#Override
public int getItemCount() {
return profileUser.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
CircleImageView profilePic;
TextView tvUserName, birthday;
OnRecItemClick onRecItemClick;
RelativeLayout relativeLayout;
public MyViewHolder(#NonNull View itemView, OnRecItemClick onRecItemClick) {
super(itemView);
profilePic = itemView.findViewById(R.id.profileImage);
tvUserName = itemView.findViewById(R.id.userName);
relativeLayout = itemView.findViewById(R.id.relTop);
this.onRecItemClick = onRecItemClick;
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
onRecItemClick.onItemClick(getAdapterPosition());
}
}
public interface OnRecItemClick {
void onItemClick(int position);
}
}
As I'm new to android, I'm struggling to highlight Recyclerview clicked or current item. I have tried some workarounds but nothing helps. Basically I want to highlight selected item even after it is coming back from respective Fragment. Please check my code and help me to get done. Thanks.
public class ContentaAdapter extends RecyclerView.Adapter<ContentaAdapter.MyViewHolder> {
Context context;
ArrayList<String> ItemTitle;
ArrayList<String> ItemSource;
public ContentaAdapter(Context context, ArrayList<String> ItemTitle, ArrayList<String> ItemSource) {
this.context = context;
this.ItemTitle = ItemTitle;
this.ItemSource = ItemSource;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_items_layout, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.ItemTitle.setText(ItemTitle.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment contentdisplay = new ViewContentFragment();
Bundle bundle=new Bundle();
bundle.putStringArrayList("ItemTitle",ItemTitle);
bundle.putStringArrayList("ItemSource",ItemSource);
bundle.putInt("position",position);
bundle.putInt("ItemCounts",ItemTitle.size());
contentdisplay.setArguments(bundle);
((MainActivity)context).replaceFragment(contentdisplay);
}
});
}
#Override
public int getItemCount() {
return ItemTitle.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView ItemTitle;
public MyViewHolder(View itemView) {
super(itemView);
ItemTitle = (TextView) itemView.findViewById(R.id.item_title);
}
}
}
you need to to add isSelected state in to your list item data model and change it onClick.
And when you know state you can change background in ViewHolder
if (isSelected) {
// set selected background here
} else {
// set not selected background here
}
And instead of keeping two lists in adapter you should create one with model ArrayList<DataModel> similar to this:
class DataModel {
String ItemTitle;
String ItemSource;
Boolean isSelected;
}
also you shouldn't pass both list to other fragment, instead take only what you need, for example yourList.get(position);
I need to call an Interstitial in this Activity by clicking on an item in the list that comes from my Adapter.
public class SoundRecyclerAdapter extends RecyclerView.Adapter<SoundRecyclerAdapter.SoundViewHolder> {
private final ArrayList<SoundObject> soundObjects;
SoundRecyclerAdapter(ArrayList<SoundObject> soundObjects) {
this.soundObjects = soundObjects;
}
#NonNull
#Override
public SoundViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.sound_item, parent, false);
return new SoundViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull SoundViewHolder holder, int position) {
final SoundObject object = soundObjects.get(position);
final Integer soundID = object.getItemID();
holder.itemTextView.setText(object.getItemName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// I tried calling the interstitial here, but to no avail
EventHandlerClass.startMediaPlayer(v, soundID);
}
});
// Handle actions when the user presses a sound button
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
EventHandlerClass.popupManager(v, object);
return true;
}
});
}
#Override
public int getItemCount() {
return soundObjects.size();
}
class SoundViewHolder extends RecyclerView.ViewHolder {
final TextView itemTextView;
SoundViewHolder(View itemView) {
super(itemView);
itemTextView = itemView.findViewById(R.id.textViewItem);
}
}
}
Pass context as parameter of adapter's constructor:
private Context context;
SoundRecyclerAdapter(Context context, ArrayList<SoundObject> soundObjects) {
this.soundObjects = soundObjects;
this.context = context;
}
and use it in click handler
Use this code in the setOnClickListener() method:
mInterstitialAd = new InterstitialAd(SoundRecyclerAdapter.this);
Then add the adrequests and load the ad.
in my adapter I have the problem that if I have defined a String before the onClickListener I cannot change it later in the onClickListener. I can set it to invisible which works perfect however I cannot change it.
I want that if a user clicks on the image, a second image should replace the first one, but I cannot either change the image in a imageview, I suggest that it is the same problem as with the String which I cannot change.
Thanks for any help!
private List<cards> listItems;
private Context context;
public arrayAdapter(List<cards> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
return new ViewHolder(v);
}
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(#NonNull final ViewHolder viewHolder, int i) {
final cards currentItem = listItems.get(i);
viewHolder.name.setText(currentItem.getName());
viewHolder.comment.setText(currentItem.getComment());
Picasso.get().load(currentItem.getProfileImageUrl()).into(viewHolder.image);
viewHolder.name.setText("this work"); // here it works
viewHolder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Try this
currentItem.setName("New text");
notifyDataSetChanged();
//viewHolder.name.setText("New text"); // this does not work
// viewHolder.name.setVisibility(View.INVISIBLE); this does work
Picasso.get().load(currentItem.getProfileImageUrl2()).into(viewHolder.image); // does not work
}
});
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name, comment, howMany;
public ImageView image;
/**
* #param itemView
*/
ViewHolder(#NonNull View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
image = (ImageView) itemView.findViewById(R.id.image);
comment = (TextView) itemView.findViewById(R.id.commentText);
}
}
}
You should call notifyItemChanged with the corresponding position.
#Override
public void onBindViewHolder(final ViewHolder viewHolder, int position) {
final cards currentItem = listItems.get(i);
viewHolder.name.setText(currentItem.getName());
viewHolder.comment.setText(currentItem.getComment());
Picasso.get().load(currentItem.getProfileImageUrl()).into(viewHolder.image);
viewHolder.name.setText("this work"); // here it works
viewHolder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.name.setText("New text"); // this does not work
// viewHolder.name.setVisibility(View.INVISIBLE); this does work
Picasso.get().load(currentItem.getProfileImageUrl2()).into(viewHolder.image); // does not work
notifyItemChanged(position);
}
});
}
change value in you list at particular position and then notifyItemChanged(position);
in your click event write this:
currentItem.setName("abc");
notifyItemChanged(position)
In the data model you have keep a variable which indicates the current profile image.
for a example
class cards {
String name;
String comment;
....
boolean profileImageChanged;
}
then inside onBindViewHolder
#SuppressLint("SetTextI18n")
#Override
public void onBindViewHolder(#NonNull final ViewHolder viewHolder, int i) {
....
if(currentItem.profileImageChanged){
Picasso.get()
.load(currentItem.getProfileImageUrl()).into(viewHolder.image);
} else {
Picasso.get()
.load(currentItem.getProfileImageUrl2()).into(viewHolder.image);
}
viewHolder.name.setText("this work"); // here it works
viewHolder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewHolder.name.setText("New text"); // this does not work
// viewHolder.name.setVisibility(View.INVISIBLE); this does work
notifyItemChanged(position);
}
});
}
Hi you can go with this approach also.
#Override
public void onBindViewHolder(final ViewHolder viewHolder, int position) {
final cards currentItem = listItems.get(i);
viewHolder.name.setText(currentItem.getName());
viewHolder.comment.setText(currentItem.getComment());
Picasso.get().load(currentItem.getProfileImageUrl()).into(viewHolder.image);
viewHolder.name.setText("this work"); // here it works
viewHolder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewHolder viewNewHolder=viewHolder
viewNewHolder.name.setText("New text");
Picasso.get().load(currentItem.getProfileImageUrl2()).into(viewNewHolder.image);
}
});
}
I have used it its working.
*******All The Best*********
Hi I'm building PlaybackSupportFragment and after adding extra row below player controls, the Presenter's onClickEvent does not work, I mean does not catches at all.
ArrayObjectAdapter episodeRow = new ArrayObjectAdapter(new EpisodePresenter(mother));
for(Episode episode : episodes)
episodeRow.add(episode);
superAdapter.add(new ListRow(new HeaderItem(0, "Episodes"), episodeRow));
setAdapter(superAdapter);
and EpisodePresenter itself is (class of course extends android.support.v17.leanback.widget.Presenter):
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
TextView view = new TextView(parent.getContext());
view.setLayoutParams(new ViewGroup.LayoutParams(315, 175));
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.setBackgroundColor(context.getResources().getColor(R.color.default_background));
view.setTextColor(Color.WHITE);
view.setGravity(Gravity.CENTER);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, Object item) {
TextView vHolder = (TextView) viewHolder.view;
final Episode model = (Episode) item;
vHolder.setText(model.name);
vHolder.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Does not work at all
Toast.makeText(context, "dsadas", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public void onUnbindViewHolder(ViewHolder viewHolder) {
}
What is a solution to that?
Fixed by using setOnEditorActionListener instead of onClickListener