How to click a View which is visible behind a RecyclerView's invisible ViewHolder item? - android-recyclerview

I have a full screen RecyclerView which will have one invisible ViewHolder Item, like below
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
....
if (position == 6) {
viewHolder.itemView.setMinimumHeight(Resources.getSystem().getDisplayMetrics().heightPixels);
viewHolder.itemView.setVisibility(View.GONE);
viewHolder.setIsRecyclable(false);
}
...
}
Once the position 6 shows up on the screen, I can see the ImageView behind it and I'd like to be able to click on that. I have added an event handler to that ImageView but it is not being triggered. It seems RecyclerView is preventing the click event to bubble down. Is there any way to click a View thru invisible/gone RecyclerView ViewItem?

Since I asked the question, I have tried multiple techniques/methods which could/should pass the click/tap event down to the view hierarchy but nothing worked. The feature I was trying to build in the app was very complex and the app itself became very complicated overtime. Too many views on top of each other and global event handlers made the implementation harder.
So I have decided as last resort that to have an empty/transparent view holder in the RecyclerView which listens the click and touch events and based on the coordinates of the touch event, I fire different action. Here is the code:
private float[] lastTouchDownXY = new float[2];
public MyView getMyView(final Context context) {
MyView view = new MyView(context);
view.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
lastTouchDownXY[0] = motionEvent.getRawX();
lastTouchDownXY[1] = motionEvent.getRawY();
}
return false;
}
});
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final float x = lastTouchDownXY[0];
final float y = lastTouchDownXY[1];
int[] mLocButton = new int[2];
// mButton is the button in the background and visible thru transparent viewholder
mButton.getLocationOnScreen(mLocButton);
final int left = mLocButton[0];
final int top = mLocButton[1];
if (x > (left - mOffset) && x < (left + mOffset + mButtonWidth) &&
y > (top - mOffset) && y < (top + mOffset + mMuteUnmuteButtonHeight)) {
// mButton clicked
} else {
// entire view clicked except mButton clickable area
}
}
});
return view;
}

Related

change item color on click in recyclerview android

I am trying to change the color of the items on click when the action mode is active. The problem is that e.g if there are five items in a recyclerview and you click one, scroll down and select sixth item and destroy the action mode. The next time you start selecting, that sixth item has automatically changed its color without you selecting it. I don't know why it is happening.
public static List<ModelClass> items = new ArrayList<>();
boolean isSelectMode = false;
boolean isActionModeEnabled = false;
public static List<ModelClass> selectList = new ArrayList<>();
#Override
public void onBindViewHolder(#NonNull MyAdapter.MyViewHolder holder, int
position) {
holder.bind(items.get(position));
ModelClass modelClass = items.get(position);
if (modelClass.isChecked() && isActionModeEnabled){
holder.row.setBackgroundColor(Color.GREEN);
modelClass.setChecked(true);
} else {
holder.row.setBackgroundColor(Color.TRANSPARENT);
modelClass.setChecked(false);
}
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public MyViewHolder(#NonNull View itemView) {
super(itemView);
row = itemView.findViewById(R.id.row);
public void bind(ModelClass model) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (isActionModeEnabled) {
isSelectMode = true;
s = items.get(getAdapterPosition());
if (!selectList.contains(s)){
selectList.add(s);
row.setBackgroundColor(Color.GREEN);
model.setChecked(true);
} else {
selectList.remove(s);
row.setBackgroundColor(Color.TRANSPARENT);
model.setChecked(false);
}
}
});
}
The problem is going to be in your view holder binding:
if (modelClass.isChecked() && isActionModeEnabled){
holder.row.setBackgroundColor(Color.GREEN);
modelClass.setChecked(true);
} else {
holder.row.setBackgroundColor(Color.TRANSPARENT);
modelClass.setChecked(false);
}
Remember that view holders are reused. That means that they will retain their internal state unless you change them. Your item list will also remember its state. Make sure you cover all the possible states of the item list and the reused view holders in the code above: You are probably missing a combination.
I recommend that you set a break point in the code above to make sure it is doing what you want. It should become obvious to you once you take a closer look.

how to know if a certain view item is visible [duplicate]

I need to know which elements are currently displayed in my RecyclerView. There is no equivalent to the OnScrollListener.onScroll(...) method on ListViews. I tried to work with View.getGlobalVisibleRect(...), but that hack is too ugly and does not always work too.
Someone any ideas?
First / last visible child depends on the LayoutManager.
If you are using LinearLayoutManager or GridLayoutManager, you can use
int findFirstVisibleItemPosition();
int findFirstCompletelyVisibleItemPosition();
int findLastVisibleItemPosition();
int findLastCompletelyVisibleItemPosition();
For example:
GridLayoutManager layoutManager = ((GridLayoutManager)mRecyclerView.getLayoutManager());
int firstVisiblePosition = layoutManager.findFirstVisibleItemPosition();
For LinearLayoutManager, first/last depends on the adapter ordering. Don't query children from RecyclerView; LayoutManager may prefer to layout more items than visible for caching.
For those who have a logic to be implemented inside the RecyclerView adapter, you can still use the #ernesto approach combined with an on scrollListener to get what you want as the RecyclerView is consulted.
Inside the adapter you will have something like this:
#Override
public void onAttachedToRecyclerView(#NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
if(manager instanceof LinearLayoutManager && getItemCount() > 0) {
LinearLayoutManager llm = (LinearLayoutManager) manager;
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(#NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int visiblePosition = llm.findFirstCompletelyVisibleItemPosition();
if(visiblePosition > -1) {
View v = llm.findViewByPosition(visiblePosition);
//do something
v.setBackgroundColor(Color.parseColor("#777777"));
}
}
});
}
}
Finally, I found a solution to know if the current item is visible, from the onBindViewHolder event in the adapter.
The key is the method isViewPartiallyVisible from LayoutManager.
In your adapter, you can get the LayoutManager from the RecyclerView, which you get as parameter from the onAttachedToRecyclerView event.
You can use recyclerView.getChildAt() to get each visible child, and setting some tag convertview.setTag(index) on these view in adapter code will help you to relate it with adapter data.
Addendum:
The proposed functions findLast...Position() do not work correctly in a scenario with a collapsing toolbar while the toolbar is expanded.
It seems that the recycler view has a fixed height, and while the toolbar is expanded, the recycler is moved down, partially out of the screen. As a consequence the results of the proposed functions are too high. Example: The last visible item is told to be #9, but in fact item #7 is the last one that is on screen.
This behaviour is also the reason why my view often failed to scroll to the correct position, i.e. scrollToPosition() did not work correctly (I finally collapsed the toolbar programmatically).
Every answer above is correct and I would like to add also a snapshot from my working codes.
recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
// Some code when initially scrollState changes
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
// Some code while the list is scrolling
LinearLayoutManager lManager = (LinearLayoutManager) recyclerView.getLayoutManager();
int firstElementPosition = lManager.findFirstVisibleItemPosition();
}
});
Following Linear / Grid LayoutManager methods can be used to check which items are visible.
int findFirstVisibleItemPosition();
int findLastVisibleItemPosition();
int findFirstCompletelyVisibleItemPosition();
int findLastCompletelyVisibleItemPosition();
and if you want to track is item visible on screen for some threshold then you can refer to the following blog.
https://proandroiddev.com/detecting-list-items-perceived-by-user-8f164dfb1d05
For StaggeredGridLayoutManager do this:
RecyclerView rv = findViewById(...);
StaggeredGridLayoutManager lm = new StaggeredGridLayoutManager(...);
rv.setLayoutManager(lm);
And to get visible item views:
int[] viewsIds = lm.findFirstCompletelyVisibleItemPositions(null);
ViewHolder firstViewHolder = rvPlantios.findViewHolderForLayoutPosition(viewsIds[0]);
View itemView = viewHolder.itemView;
Remember to check if it is empty.
You can find the first and last visible children of the recycle view and check if the view you're looking for is in the range:
var visibleChild: View = rv.getChildAt(0)
val firstChild: Int = rv.getChildAdapterPosition(visibleChild)
visibleChild = rv.getChildAt(rv.childCount - 1)
val lastChild: Int = rv.getChildAdapterPosition(visibleChild)
println("first visible child is: $firstChild")
println("last visible child is: $lastChild")
For those who are looking for an answer in Kotlin:
fun getVisibleItem(recyclerView : RecyclerView) {
recyclerView.addOnScrollListener(object: RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
if(newState == RecyclerView.SCROLL_STATE_IDLE) {
val index = (recyclerView.layoutManager.findFirstVisibleItemPosition
//use this index for any operation you want to perform on the item visible on screen. eg. log(arrayList[index])
}
}
})
}
You can explore other methods for getting the position as per your use case.
int findFirstCompletelyVisibleItemPosition()
int findLastVisibleItemPosition()
int findLastCompletelyVisibleItemPosition()
if the visible item position is different from the item position toast message will show on the screen.
myRecyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(#NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
LinearLayoutManager manager= (LinearLayoutManager) myRecyclerview.getLayoutManager();
assert manager != null;
int visiblePosition = manager.findLastCompletelyVisibleItemPosition();
if(visiblePosition > -1&&a!=visiblePosition) {
Toast.makeText(context,String.valueOf(visiblePosition),Toast.LENGTH_SHORT).show();
//do something
a=visiblePosition;
}
}
#Override
public void onScrolled(#NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
//Some code while the list is scrolling
}
});

How to hide/show a view in the Activity by clicking a button in the Android Cardview?

I am working on a commercial app as an internship. In one of its activity, I have tab view with two fragments. In each fragment, I'm using the card view to hold the views.
The card view has one image view, two text views, and a button and in the bottom of the activity below the tab view, there is a button which has its visibility mode as "GONE".
Now What I want is, Whenever I click on the button in the card view, the button at the bottom of the activity should hide/show for respective clicks.
Cardcaptionadapter.java
public CaptionedImagesAdapterMenu.ViewHolder onCreateViewHolder(
ViewGroup parent, int viewType){
CardView cv = (CardView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_captioned_image_menu, parent, false);
return new ViewHolder(cv);
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position){
CardView cardView = holder.cardView;
ImageView imageView = (ImageView)cardView.findViewById(R.id.info_image);
Drawable drawable = ContextCompat.getDrawable(cardView.getContext(), imageIds[position]);
imageView.setImageDrawable(drawable);
imageView.setContentDescription(captions[position]);
TextView textView = (TextView)cardView.findViewById(R.id.info_text);
textView.setText(captions[position]);
TextView textView1 = cardView.findViewById(R.id.info_menu);
textView1.setText(desc[position]);
TextView textView2 = cardView.findViewById(R.id.info_price);
textView2.setText("₹ " + price[position]);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null) {
listener.onClick(position);
}
}
});
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(holder.button.getText().equals("ADD")){
holder.button.setText("ADDED");
holder.button.setBackgroundColor(Color.parseColor("#00ff00"));
SharedPreferences sharedPref = context.getSharedPreferences("ADD",0);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("ADDED", 1);
editor.apply();
}
else {
holder.button.setText("ADD");
holder.button.setBackgroundColor(Color.parseColor("#ffffff"));
SharedPreferences sharedPref = context.getSharedPreferences("ADD",0);
SharedPreferences.Editor editor = sharedPref.edit();
editor.clear();
editor.apply();
}
}
});
}
MenuActivity.java
SharedPreferences preferences = getSharedPreferences("ADD",0);
int addvalue = preferences.getInt("ADDED",0);
if(addvalue==1){
orderbutton.setVisibility(View.VISIBLE);
}
else{
orderbutton.setVisibility(View.GONE);
}
orderbutton.setOnClickListener(view -> {
orderbutton.setVisibility(View.GONE);
paybutton.setVisibility(View.VISIBLE);
});
you should add MenuActivity.java code inside public void onBindViewHolder(ViewHolder holder, final int position){ } method also.

MediaPlayer inside an adapter playing the wrong track

I'm fairly new to Android development, and I added a MediaPlayer in a list of items so that the pronunciation is played for each word listed.
However, the wrong track is playing, I can't figure out how to link the right track to each item in the list view.
The ID changes when I'm scrolling for example as I can see in logs, so I think there's something I misunderstood.
What I fail to understand is why it works for the ImageView for example, but not for the audio file.
public class WordsAdapter extends ArrayAdapter<Words>{
private int colorview;
private View listItemView;
private Words currentWords;
public WordsAdapter(Context context, ArrayList<Words> words, int color){
super(context, 0, words);
colorview = color;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_view_black, parent, false);
}
currentWords = getItem(position);
ImageView image = (ImageView) listItemView.findViewById(R.id.imageicon);
if(currentWords.hasImage()) {
image.setImageResource((currentWords.getImageID()));
image.setVisibility(View.VISIBLE);
}else image.setVisibility(View.GONE);
TextView defaultTextView = (TextView) listItemView.findViewById(R.id.text_default);
defaultTextView.setText(currentWords.getDefaultWord());
TextView miwokTextView = (TextView) listItemView.findViewById(R.id.text_miwok);
miwokTextView.setText(currentWords.getMiwokWord());
View linearWords = listItemView.findViewById(R.id.linear_words);
linearWords.setBackgroundResource(colorview);
Button ButtonPlay = (Button) listItemView.findViewById(R.id.playButton);
ButtonPlay.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
MediaPlayer player = MediaPlayer.create(v.getContext(), currentWords.getAudioFile());
Log.v("Audio id : ", player.getTrackInfo().toString());
player.start();
}
});
return listItemView;
}
}
I found the solution.
The variable currentWords needs to be declared inside the method and set to final.
public View getView(int position, View convertView, ViewGroup parent) {
[...]
final Words currentWords = getItem(position);
[...]
}

AutoCompleteTextView OnItemClickListener null param (landscape mode on HTC Desire S)

My Problem : I have an AutoCompleteTextView with an OnItemClickListener. This has been working fine for 18 months, but I have now noticed it throws a NullPointerException when I select an item in landscape mode on my HTC Desire S. (There is no error in portrait mode or on any other phone or emulator I've tested it on).
The AdapterView<?> av parameter is coming through as null. Why would this be, and how can I get around it?
Code :
myAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.myAutoCompleteTextView);
myAutoCompleteTextView.setSingleLine();
myAutoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> av, View v, int index, long arg) {
String selectedItem = (String)av.getItemAtPosition(index);
//Do stuff with selected item ...
}
}
Error :
java.lang.NullPointerException
at uk.co.myCompany.mobile.android.myCompanymobile.pages.groups.AbstractGroupSelectionPage$3.onItemClick(AbstractGroupSelectionPage.java:228)
at android.widget.AutoCompleteTextView.onCommitCompletion(AutoCompleteTextView.java:993)
at com.android.internal.widget.EditableInputConnection.commitCompletion(EditableInputConnection.java:76)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:368)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:86)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4385)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at dalvik.system.NativeStart.main(Native Method)
Extra Code - my custom adapter inner class :
/**
* An inner class to simply make a custom adapter in which we can alter the on-screen look of selected groups.
*/
private class SelectedGroupAdapter extends ArrayAdapter<Group> {
private ArrayList<Group> items;
private int layout;
public SelectedGroupAdapter(Context context, int layout, ArrayList<Group> items) {
super(context, layout, items);
this.items = items;
this.layout = layout;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(layout, null);
}
Group o = items.get(position);
//Display the group name and number of contacts
if (o != null) {
TextView groupName = (TextView) v.findViewById(R.id.groupName);
TextView noOfContacts = (TextView) v.findViewById(R.id.noOfContacts);
if (groupName != null) {
groupName.setText(o.getGroupName());
}
if(noOfContacts != null) {
if (o.isDynamic())
noOfContacts.setText(getString(R.string.dynamic));
else {
int contactsCount = o.getGroupSize();
if(contactsCount == 1) noOfContacts.setText(contactsCount + " " + getString(R.string.contact));
else noOfContacts.setText(contactsCount + " " + getString(R.string.contacts));
}
}
}
return v;
}
}
My hunch is that since you are declaring android:configChanges="orientation" in your manifest, then when you rotate the old OnItemClickListener is still sticking around, and since you technically have a new layout, the AdapterView that was being used prior to orientation change doesn't exist anymore, thus is null when you click on an item.
There's 2 things I think that would solve this if this is the case:
Remove the orientation option in your manifest. Any events you place in configChanges tells Android "I'm taking care of this configuration change, so let me handle it" as opposed to letting Android handle it. The normal operation for Android in the event of an orientation change is to destroy and recreate your Activity (it will take care of repopulating some Views with data automatically).
If you decide you need to handle orientation changes, then override onConfigurationChanged() and set the OnItemClickListener to the new AdapterView object (ListView, GridView, whichever you are using) that should have been recreated in the onCreate method.