How to change VISIBILITY of textview of specific row in Recyclerview android
You should store Property [visibility] inside model and make decision based on that.
class StationData {
...
private int visibility = View.VISIBLE;
public int getVisibility() {
return visibility;
}
public void setVisibility(int visibility) {
this.visibility = visibility;
}
...
}
Handle visibility here and update view.
@Override
public void onBindViewHolder(@NonNull final SuggestionHomeAdapter.ViewHolder holder, final int position) {
final StationData stationData = listItem.get(position);
holder.station_name.setVisibility(stationData.getVisibility());
...
}
call updateVisibility
from activity with position and visibility.
public void updateVisibility(int position, int visibility) {
listItem.get(position).setVisibility(visibility);
notifyItemChanged(position);
}
How to set the visibility of a view after recyclerview becomes empty
Create an interface
in new file:
public interface onShoppingItemRemovedListener{
void onRecyclerViewEmpty();
}
In your adapter
do this:
class YourAdapter extends .........{
//add this
private onShoppingItemRemovedListener listener;
.....
.....
//in the constructor
public YourAdapter (onShoppingItemRemovedListener listener,..............){
this.listener = listener;
......
......
......
}
//when you delete item do this
private void removeItem(int position, Context context) {
shoppingCartList.remove(position);
if (shoppingCartList.isEmpty()) {
Toast.makeText(context, "Nu mai exista niciun produs in cos", Toast.LENGTH_LONG).show();
//at this point the recyclerview is empty, so notify the fragment
listener.onRecyclerViewEmpty();
}
}
}
Finally in the fragment
implement the interface
and pass to the adapter
:
class YourFragement extends ........ implements onShoppingItemRemovedListener{
//now when you initialize the adapter pass the listener like this
adapter = new YourAdapter(this,...........);
.....
.....
//override this
@Override
public void onRecyclerViewEmpty(){
//this is triggered when the recycler view is completely empty
constraintLayout.setVisibility(View.GONE);
recyclerView.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
btnAdd.setVisibility(View.VISIBLE);
}
}
Dynamically Changing Toolbar Item Visibility in on RecyclerView Click (Without the LongClick)
So I found a solution. I added this:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_choose_contacts_action, menu);
if (adapter.getSelectedItemCount() == 0) {
menu.findItem(R.id.add).setVisible(false);
} else {
menu.findItem(R.id.add).setVisible(true);
}
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
getSupportActionBar().setDisplayShowHomeEnabled(true);
return true;
}
.....
// Checking previously selected contacts
if (selectedContacts != null) {
new Handler(Looper.getMainLooper()).postDelayed(() -> {
List<Integer> selectedIndexes = new ArrayList<>();
// Finding indexes of selected contact ids
for (int i = 0; i < contactList.size(); i++) {
if (selectedContacts.contains(contactList.get(i).getUserId())) {
selectedIndexes.add(i);
}
}
// Triggering click on each selected item
for (int j = 0; j < selectedIndexes.size(); j++) {
recyclerView.getChildAt(selectedIndexes.get(j)).performClick();
}
invalidateOptionsMenu();
}, 500);
......
@Override
public void onClick(View view, int position) {
// invalidateOptionsMenu(); (resetting onCreateOptionsMenu) executed with 600 millis delay, otherwise it will happen before the 500 millis
// delay when checking previously selected contacts
new Handler(Looper.getMainLooper()).postDelayed(() -> invalidateOptionsMenu(), 600);
}
changing visibility of a recyclerview item changes other item's visibility
That happens because its a recycler view (so the view gets recycled as you scroll and the visibility of the like or unlike sticks there.
Modify your item object to include a boolean isLiked
and set that to true or false on the onClick:
holder.like.setOnClickListener{
item.setIsLiked(true);
}
holder.unlike.setOnClickListener{
item.setIsLiked(false);
}
and set the view visibility based on that:
holder.like.visibility=item.getIsLiked() ? View.VISIBLE : View.GONE;
holder.unlike.visibility=item.getIsLiked() ? View.GONE : View.VISIBLE;
note: setIsLiked(boolean like) and getIsLiked() could be different names if you use auto-generated getters and setters;
do same for shopping notshopping
Adding dynamically textviews to RecyclerView ViewHolder mess up the views
Adding views dynamically into a recycler view is a BAD idea. THe point of a recycler view is it recycles the same view structure over multiple items. So when it recycles, you've suddenly added that view to a new item that doesn't need it. Not having dynamic views is almost a pre-requisite of having a recycler view.
A better way is to play with visibilities. Put the text view in all views, but make it GONE. In your bind, you'd then set the visibility of the text view for every item- either to GONE or VISIBLE. THis will get you the effect you want.
Another way would be by using different view types for with and without the extra view, but that tends to be more complicated.
Related Topics
Flutter:Renderbox Was Not Laid Out: Renderrepaintboundary#58C65 Relayoutboundary=Up1 Needs-Paint
Java.Lang.Runtimeexception: Canvas: Trying to Draw Too Large(144000000Bytes) Bitmap
App Restarts Rather Than Resumes
How to Prevent User Go Back to Protected Page After Logout
Clear Cache in Android Application Programmatically
How to Get the Height of Recyclerview Item in "Onbindviewholder"
Setting Webview to View Desktop Site and Not Mobile Site
Move to Another Edittext When Soft Keyboard Next Is Clicked on Android
Pdf Not Open in Webview from Url
Retrofit 2 Json Response Json Array and Object
Images Are Not Visible in React-Native App Release Build
Missing Android Platform(S) Detected
How to Dynamically Hide a Menu Item in Bottomnavigationview
Android:Strike Out Text With Bold or Thicker Line Than Default Strike_Thru_Text_Flag
Alarm Manager Won't Work When the App Is Killed in the Background and Device Is Locked
How to Access Data/Data Folder in Android Device