recover EditText from a RecyclerView item (from the adabter to myActivity)? - java

hi guys I need help I'm using android studio java for the first time ...
this is my interface
enter image description here
I wanna recover the EditText value in a list when the user clicks in "valider la commande"

In order to retrieve that value, you should have an array declared in your recycler views adapter, in which you write each value whenever the user changes it.
You should also add a public method to retrieve that array from your adapter class. So on, in your button onClickListener you just retrieve that array from your adapter using the public method.
Here is an example:
class AsistenteAdapter(var names: ArrayList<String>): RecyclerView.Adapter<AsistenteAdapter.ViewHolder>() {
//DECLARE HERE YOUR ARRAY VARIABLE (SAME SIZE AS names.size)
class ViewHolder(var binding: ComponentApuntadoContainerBinding): RecyclerView.ViewHolder(binding.root){}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = ComponentApuntadoContainerBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
//ADD HERE YOUR LOGIC TO SAVE THE VALUE
//Put on text changed listener to your EDITTEXT, so that whenever the text is changed, the value is saved in your array
}
override fun getItemCount(): Int {
return names.size
}
public fun getValues(): //ARRAY {
return //YOUR ARRAY VARIABLE
}
}

Related

Android: Recyclerview with ItemTouchHelper.Callback flickering on lower items in list

I am currently trying to implement a RecyclerView list with drag and drop reordering. For this I use the ItemTouchHelper.SimpleCallback
class SoftkeyScreenListReorderHelperCallback(
private val adapter: SoftkeyScreenListAdapter
) : ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.START or ItemTouchHelper.END, 0) {
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
return adapter.itemMoved(viewHolder.bindingAdapterPosition, target.bindingAdapterPosition)
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {}
}
My Adapter got the itemMoved() method, which is called in the onMove() method in the callback. Here I just swap the items and notify the adapter about the change.
fun itemMoved(fromPosition: Int, toPosition: Int): Boolean {
Collections.swap(list, fromPosition, toPosition)
notifyItemMoved(fromPosition, toPosition)
return true
}
For my RecyclerView I implemented the following
binding.recyclerview.apply {
[...] // adapter init
myAdapter.setHasStableIds(true)
adapter = myAdapter
val touchHelper = ItemTouchHelper(SoftkeyScreenListReorderHelperCallback(adapter as SoftkeyScreenListAdapter))
touchHelper?.attachToRecyclerView(this)
(itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
setHasFixedSize(true)
}
It works, but I always get flickering for the items below (after) the new item position. Assume I have 5 Items {1,2,3,4,5} and want to swap 1 with 3, then 4 and 5 are flickering. 1, 2 and 3 don't.
I already set the recyclerview size fixed, enabled stable ids and disabled animations, but it does not help. Does anyone has a clue what could be the reason for that and how to fix?
try this
recyclerView.getItemAnimator().setChangeDuration(0);
Possibly, you are getting or loading your data from a place or using a library that requires some time. If that is the case, this answer could help
Try this:
After adapter initialization:
adapter.setHasStableIds(true);
In adapter class:
#Override
public long getItemId(int position) {
return itemList.get(position).hashCode();
}

Radio button are not working normally in my Recycler View. Multiple Radio Buttons got selected of the view which are not visible with the focused one

I'm using a Recycler View to show all the images from the galley or the external storage of a device in a Grid Layout Manager. And I'm using a Radio Button to show if the image is selected or not.
PROBLEM
Whenever I select or deselect a Radio Button from the visible Views in the Recycler View some other Views which are outside the Visible Screen got selected or deselected.
It is like I'm pressing on the same View of the Recycler View, but the images are different.
PROBLEM
well that's because of the recycler view concept of reusing the views instead of creating new views every time you scroll.
you see if you have 100 items you want to show in a recycler view and only 20 of them could appear to the user, recycler view creates only 20 view holder to represent the 20 items, whenever the user scroll recycler view will still have 20 view holder only but will just switch the data stored in this view holders rather than create new view holders.
now to handle selection of your items there's two ways to do this.
the naive way
hold selection in a boolean array inside the recycle view adapter.
whenever the user scrolls, the adapter calls onBindViewHolder to update the visible viewholder with the proper data.
so when onBindViewHolder gets called just set the radio button selection according the boolean array using the position sent in the method call
at the end of your usage to the recycler view you can create a getter method in the adapter to get the selection array list of boolean and pass the data based on it
public class PhotosGalleryAdapter extends RecyclerView.Adapter<PhotosGalleryViewHolder> {
ArrayList<Your_Data_ClassType> data;
ArrayList<Boolean> dataSelected ;
public PhotosGalleryAdapter(ArrayList<Your_Data_ClassType> data) {
this.data = data;
dataSelected = new ArrayList<>(data.size()) ;
}
...
#Override
public void onBindViewHolder(#NonNull PhotosGalleryViewHolder holder, int position) {
...
RadioButton radioButton = holder.getRadioButton()
radioButton.setChecked(dataSelected.get(position));
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
dataSelected.set(holder.getAbsoluteAdapterPosition() , isChecked) ;
}
});
...
}
}
the other way is to use a selection tracker and it should be the correct way to handle selections in a recycler view.
the problem with this way is it needs a lot of editing to the code and creating new classes to include as parameters in the selection tracker, but in the end you'll find it worth the time you spent on it.
in order to start with this way you need to do the following :
firstly, decide what should be a key (String-Long-Parcelable) so the tracker should use to differentiate between your data , the safest way is either String or Parcelable as I once tried Long and ended up with lots and lots of problems (in your case I will assume it's the photo's uri which will be of type string)
secondly, you need to create two new classes, one that extends ItemDetailsLookup, and the other extends ItemKeyProvider, and should use the key as their generic type (the type that is put between <> )
your two classes should look like this (that you might copy them straight forward)
the one that extends ItemKeyProvider :
public class GalleryItemKeyProvider extends ItemKeyProvider<String>{
PhotosGalleryAdapter adapter ;
/**
* Creates a new provider with the given scope.
*
* #param scope Scope can't be changed at runtime.
*/
public GalleryItemKeyProvider(int scope,PhotosGalleryAdapter m_adapter) {
super(scope);
this.adapter = m_adapter;
}
#Nullable
#Override
public String getKey(int position) {
return adapter.getKey(position);
}
#Override
public int getPosition(#NonNull String key) {
return adapter.getPosition(key);
}
}
the one that extends ItemDetailsLookup :
public class GalleryDetailsLookup extends ItemDetailsLookup<String> {
private final RecyclerView recView ;
public GalleryDetailsLookup(RecyclerView m_recView){
this.recView = m_recView;
}
#Nullable
#Override
public ItemDetails<String> getItemDetails(#NonNull MotionEvent e) {
View view = recView.findChildViewUnder(e.getX(), e.getY());
if (view != null) {
RecyclerView.ViewHolder holder = recView.getChildViewHolder(view);
if (holder instanceof PhotosGalleryViewHolder) {
return ((PhotosGalleryViewHolder) holder).getItemDetails();
}
}
return null;
}
}
thirdly, you should include this new two methods in your adapter to be used by the above classes
public class PhotosGalleryAdapter extends RecyclerView.Adapter<PhotosGalleryViewHolder> {
...
public String getKey(int position) {
return data.get(position).getUri();
}
public int getPosition(String key) {
for (int i = 0; i < data.size(); i++) {
if (data.get(i).getUri() == key) return i;
}
return 0;
}
...
}
forthly (if there's an english word called forthly), you should initialize the tracker with all the above classes that were created before and he will handle the rest, the tracker takes as parameters
a unique selection tracker id (if that will be the only selection tracker you will use then name it anything)
the ItemKeyProvider that we created
the DetailsLookup that we created
a String-Long-Parcelable Storage to store the keys that were selected in (in our case it will be a String Storage)
a Selection predicate, it's responsible to handle the way of selection you want to do, you want it to be able to (select only one item-multiple selection with no limits- based on a weird algorithm like even only or odd only), in my case I will use a default multiple selection one but if you want to alter it with another selection algorithm you should create a new class that extends SelectionPredicates and implement your way of selection, you could also just check the other default ones might be what you're looking for.
anyway, that's how the initialization should look (you should put this code wherever you initialize your recycler view at whether it's in fragment or activity method):
private void initRecycleView() {
...
SelectionTracker<String> tracker = new SelectionTracker.Builder<>("PhotosGallerySelection",
Your_Recycler_View,
new GalleryItemKeyProvider(ItemKeyProvider.SCOPE_MAPPED, photosAdapter),
new GalleryDetailsLookup(Your_Recycler_View),
StorageStrategy.createStringStorage())
.withSelectionPredicate(SelectionPredicates.createSelectAnything())
.build();
...
}
I didn't find a way to let me initialize the adapter with data and then create the tracker inorder to make the viewholders know about their selection or not, so in this case I firstly created the tracker and then made the adapter know about it's data using a setter and notifyDataSetChanged
what I mean by that is after creating the tracker instantly set the tracker and data to the adapter, so the initRecycleView should look like this
private void initRecycleView() {
...
SelectionTracker<String> tracker = new SelectionTracker.Builder<>("PhotosGallerySelection",
Your_Recycler_View,
new GalleryItemKeyProvider(ItemKeyProvider.SCOPE_MAPPED, photosAdapter),
new GalleryDetailsLookup(Your_Recycler_View),
StorageStrategy.createStringStorage())
.withSelectionPredicate(SelectionPredicates.createSelectAnything())
.build();
photosAdapter.setTracker(tracker);
photosAdapter.setData(data);
photosAdapter.notifyDataSetChanged();
...
}
Last but no least, you should handle how the view holders should know if they were selected or not, so you should let the adapter know about the tracker and its data by creating a setter method in it, that's how the adapter should look like in the end :
public class PhotosGalleryAdapter extends RecyclerView.Adapter<PhotosGalleryViewHolder> {
ArrayList<Your_Data_Class> data;
private SelectionTracker<String> tracker;
public PhotosGalleryAdapter() {
data = new ArrayList<>();
}
public ArrayList<Your_Data_Class> getData() {
return data;
}
public void setData(ArrayList<Your_Data_Class> m_data) {
this.data = m_data;
}
#Override
public ScheduleViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
...
}
#Override
public void onBindViewHolder(#NonNull PhotosGalleryViewHolder holder, int position) {
...
boolean isSelected = tracker.isSelected(data.get(i).getUri());
RadioButton radioButton = holder.getRadioButton;
radioButton.setChecked(isSelected);
}
#Override
public int getItemCount() {
return data.size();
}
public String getKey(int position) {
return data.get(position).getUri();
}
public int getPosition(String key) {
for (int i = 0; i < data.size(); i++) {
if (data.get(i).getUri() == key) return i;
}
return 0;
}
public void setTracker(SelectionTracker<String> m_tracker) {
this.tracker = m_tracker;
}
}
(as you may notice if you initialized the adapter with its data through the constructor, when he asks the tracker if there were an item selected or not, it will result in a NullPointerException as at the moment of initializing the adapter you still didn't initialize the tracker)
that way you could keep track of your selection the way google suggests in their documentation (which I honestly don't know why the made it very complicate like that).
if you want to know all the selected item in the end of your application/fragment use, you should call tracker.getSelection() which will return a Selection List for you to iterate on
There's a tiny problem/feature with the tracker that it won't start selecting the first item until you use a long press on it, that happens only in the first item you select, if you do want this feature (start selecting mode by long press) then leave it as it is
incase you don't want it you can make the tracker select a ghost key (any unique string key that means nothing to your data) at the beginning which should later enable the selection mode with a simple click on any photo
tracker.select("");
this also the way to make a default/old selection at the beginning, you could make a for loop and call tracker.select(Key) if you do want the tracker to start with few items being selected
N.B : incase you use the Ghost Key method you should watchout that the selection array that will get returned when you call tracker.getSelection() will also contain this Ghost Key.
at the end if you do have the curiosity of reading about selection tracker in the documentation follow this link
or maybe if you know how to read kotlin follow this two links
implementing-selection-in-recyclerview
a guide to recyclerview selection
I was stuck in the selection problem for days before I figure how to do all that so I hope you find your way through it.
Omar Shawky has covered the solutions.
With my answer I will stress on the reason why someone may face this sort of an issues with recycler views and how to avoid this common issue in the future (avoiding pitfalls).
Reason:
This issue happens because RecyclerView recycles views. So a RecyclerView item's view once inflated can get reused to show another off screen (to be scrolled to) item. This helps reduces re-inflation of views which otherwise can be taxing.
So if the radio button of an item's view is selected, and the same view gets reused to show some other item, then that new item can also have a selected radio button.
Solution:
The simplest solution for such issues is to have an if else logic in your ViewHolder to provide logic for both selected and de-selected cases. We also do not rely on information from radio button itself for initial setup (we do not use radioButton.isSelected() at the time of setup)
e.g code to write inside your ViewHolder class:
private boolean isRadioButtonChecked = false; // ViewHolder class level variable. Default value is unchecked
// Now while binding in your ViewHolder class:
// Setup Radio button (assuming there is just one radio button for a recyclerView item).
// Handle both selected and de-selected cases like below (code can be simplified but elaborating for understanding):
if (isRadioButtonChecked) {
radioButton.setChecked(true);
} else {
radioButton.setChecked(false);
}
radioButton.setOnCheckedChangeListener(
(radioButton, isChecked) -> isRadioButtonChecked = isChecked);
Do not do any of the following while setting up:
private boolean isRadioButtonChecked = false; // class variable
//while binding do not only handle select case. We should handle both cases.
if (isRadioButtonChecked) { // --> Pitfall
radioButton.setChecked(true);
}
radioButton.setOnCheckedChangeListener((radioButton, isChecked) -> isRadioButtonChecked = isChecked);
OR
// During initial setup do not use radio button itself to get information.
if (radioButton.isChecked()) { // --> Pitfall
radioButton.setChecked();
}

How to swipe back an item in RecyclerView after swipe

After swiping an item in RecyclerView I want it to go back without swiping it back manually.
Here is an swipeable item in RecyclerView.
Item in RecyclerView
Swiping...
After swipe event I want this item to go back, as if it was swiped not far enough, but event must happen. How can I do this?
After swipe
Here is my SwipeHelper, which keeps background static:
abstract class ProfileSwipeHelper : ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT
) {
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
return true
}
override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
if (viewHolder != null) {
ItemTouchHelper.Callback.getDefaultUIUtil().onSelected((viewHolder as ProfilesAdapter.ViewHolder).foreground)
}
}
override fun onChildDraw(
c: Canvas,
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
dX: Float,
dY: Float,
actionState: Int,
isCurrentlyActive: Boolean
) {
getDefaultUIUtil().onDraw(c, recyclerView,
(viewHolder as ProfilesAdapter.ViewHolder).foreground, dX, dY,
actionState, isCurrentlyActive)
}
override fun onChildDrawOver(
c: Canvas,
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder?,
dX: Float,
dY: Float,
actionState: Int,
isCurrentlyActive: Boolean
) {
getDefaultUIUtil().onDrawOver(
c, recyclerView,
(viewHolder as ProfilesAdapter.ViewHolder).foreground, dX, dY,
actionState, isCurrentlyActive)
}
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
getDefaultUIUtil().clearView((viewHolder as ProfilesAdapter.ViewHolder).foreground)
}
}
And here onSwiped event in main activity, only with Toast:
//Main Activity
val context : Context = this
val deleteSwipeHandler1 = object : ProfileSwipeHelper() {
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
Toast.makeText(context, "swiped", Toast.LENGTH_SHORT).show()
}
}
ItemTouchHelper(deleteSwipeHandler1).attachToRecyclerView(rv_profiles)
You can use Multiswipe library. Read complete explanations here. If you want to use Java read this. Below is
a concise explanation without using complete options of this library:
add jitpack and Multiswipe library to your project:
in settings.gradle or root build.gradle:
repositories {
//...
maven { url 'https://jitpack.io' }
}
in app's build.gradle:
dependencies {
implementation 'com.github.ygngy:multiswipe:1.2.1'
}
implement MultiSwipe in ViewHolder:
import androidx.recyclerview.widget.RecyclerView
import android.view.View
import com.github.ygngy.multiswipe.MultiSwipe
import com.github.ygngy.multiswipe.LeftSwipeList
import com.github.ygngy.multiswipe.RightSwipeList
class ViewHolder(private val view: View) : RecyclerView.ViewHolder(view), MultiSwipe {
var mLeftSwipeList: LeftSwipeList? = null
var mRightSwipeList: RightSwipeList? = null
// todo other ViewHolder codes...
fun bind() {
// Each swipe contains of at least an id and an icon
val likeSwipe = Swipe(
context = context, // context used to extract default colors and margins from resources
id = SWIPE_TO_LIKE_ID, // swipe id will be sent to onSwipeDone when swipe is completed
activeIcon = getDrawable(R.drawable.ic_like_24)!!, // swipe icon
activeLabel = getString(R.string.like), // OPTIONAL swipe label
acceptIcon = getDrawable(R.drawable.ic_like_accept_24)!!,// OPTIONAL icon used when swipe displacement is greater than "accept boundary"
acceptLabel = getString(R.string.like_accept),// OPTIONAL label used when swipe swipe displacement is greater than "accept boundary"
inactiveIcon = getDrawable(R.drawable.ic_disabled_like_24)!!// OPTIONAL icon used when this swipe could be next swipe
)
// Create other swipes (shareSwipe, copySwipe, ...) in a similar way.
// If row has left swipes, create left swipe list in the desired order like below:
mLeftSwipeList = LeftSwipeList (shareSwipe, copySwipe, cutSwipe)
// If row has right swipes, create right swipe list in the desired order like below:
mRightSwipeList = RightSwipeList (likeSwipe, editSwipe, delSwipe)
}
// Don't recreate swipes or any object here
override val leftSwipeList: LeftSwipeList?
get() = mLeftSwipeList
// Don't recreate swipes or any object here
override val rightSwipeList: RightSwipeList?
get() = mRightSwipeList
// Here handle swipe event and/or return some data to MultiSwipeListener
override fun onSwipeDone(swipeId: Int): Any? {
// Instead you may choose to only return data
// from this method to consume event at Activity or Fragment
when(swipeId) {
SWIPE_TO_SHARE_ID -> {
// todo share
}
SWIPE_TO_COPY_ID -> {
// todo copy
}
//...
}
return MyData()// return any data to Activity or Fragment
}
}
attach library to recyclerview at activity or fragment:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.github.ygngy.multiswipe.MultiSwipeListener
import com.github.ygngy.multiswipe.SwipeDirection
import com.github.ygngy.multiswipe.multiSwiping // importing extension function
class DemoActivity : AppCompatActivity() {
// todo other activity codes...
override fun onCreate(savedInstanceState: Bundle?) {
// todo other onCreate codes...
// attaching Multiswipe to RecycerView
recyclerView.multiSwiping(
swipeThreshold = 0.5f, // OPTIONAL, the fraction of view for complete swipe threshold
object: MultiSwipeListener { // OPTIONAL listener
// This method is called after onSwipeDone of ViewHolder
// and data is the returned value of onSwipeDone of ViewHolder
override fun onSwipeDone(swipeId: Int, data: Any?) {
// data is the return value of "ViewHolder.onSwipeDone"
// cast to data you returned from "ViewHolder.onSwipeDone"
val myData = data as MyData?
when(swipeId) {
SWIPE_TO_SHARE_ID -> shareItem(myData)
SWIPE_TO_COPY_ID -> copyItem(myData)
//...
}
}
/***
This method will be called when direction changes in each swipe.
This method could be used to hide on screen widgets such as FABs.
direction may be:
- START (when user opens start side of view),
- END (when user opens end side of view),
- NONE (when swipe is closing without user interaction)
***/
override fun swiping(direction: SwipeDirection, swipeListSize: Int) {
// here i hide FAB when user is swiping end side actively
if (direction == SwipeDirection.END) fab.hide() else fab.show()
}
})
}
}

is there a solution for accessing the first item in a recyclerView

I'm creating a user guide and one of my items that I want to describe in my user Guide is in my recycler View help me please I'm using this library : com.github.mreram:showcaseview:1.2.0
found the solution:
private var isPlayed = false
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.onFill(holder.adapterPosition)
if (position == 0 && !isPlayed) {
holder.guide()
isPlayed = false
}
}
You can access recyclerview's first item by checking position of t he item in onBindview method
public void onBindViewHolder(ViewHolder holder, int position) {
if(position==0)
// write your code here
else
// other code
}

smoothScrollTo(position) scrolls to bottom instead of targeted position in RecyclerView

My vertical RecyclerView (looking like a simple list) in Kotlin scrolls neatly to the next element when I press a button. 👍🏻
However when the next element is off screen (for example because in the meantime the user scrolled to a different position with a gesture) the problem is that this doesn't work anymore. Instead, it automatically scrolls all the way to the bottom of the RecyclerView.
Any idea how to fix that? I'd appreciate it!
Thanks in advance for your efforts.
I'm overriding SmoothScrollLinearLayoutManager:
class SmoothScrollLinearLayoutManager(private val mContext: Context, orientation: Int, reverseLayout: Boolean) : LinearLayoutManager(mContext, orientation, reverseLayout) {
override fun smoothScrollToPosition(recyclerView: RecyclerView, state: RecyclerView.State?,
position: Int) {
val smoothScroller = object : TopSnappedSmoothScroller(recyclerView.context) {
//This controls the direction in which smoothScroll looks for your view
override fun computeScrollVectorForPosition(targetPosition: Int): PointF {
return PointF(0f, 1f)
}
//This returns the milliseconds it takes to scroll one pixel.
protected override fun calculateSpeedPerPixel(displayMetrics: DisplayMetrics): Float {
return MILLISECONDS_PER_INCH / displayMetrics.densityDpi
}
}
smoothScroller.targetPosition = position
Log.i("Target", smoothScroller.targetPosition.toString())
startSmoothScroll(smoothScroller)
}
private open inner class TopSnappedSmoothScroller(context: Context) : LinearSmoothScroller(context) {
override fun computeScrollVectorForPosition(targetPosition: Int): PointF? {
return this#SmoothScrollLinearLayoutManager
.computeScrollVectorForPosition(targetPosition)
}
override fun getVerticalSnapPreference(): Int {
return LinearSmoothScroller.SNAP_TO_START
}
}
private open inner class CenterSnappedSmoothScroller(context: Context) : LinearSmoothScroller(context) {
override fun computeScrollVectorForPosition(targetPosition: Int): PointF? {
return this#SmoothScrollLinearLayoutManager
.computeScrollVectorForPosition(targetPosition)
}
override fun getVerticalSnapPreference(): Int {
return LinearSmoothScroller.SNAP_TO_END
}
}
// Scrolling speed
companion object {
private val MILLISECONDS_PER_INCH = 110f
}
}
The button triggers this function to scroll to the next element in the list (RecyclerView):
private fun fastForwardTapped() {
// Update selected position in RecyclerView
selectedPosition += 1
recyclerView.smoothScrollToPosition(selectedPosition)
}
It had to do with how I overrode LinearLayoutManager.
I ended up using this solution:
https://stackoverflow.com/a/36784136/8400139
It works.

Categories

Resources