I've expandable row layouts in RecyclerView, normally they're expanded.
Atteched ItemTouchHelper.SimpleCallback to the RecyclerView for moving the rows with onMove(). Everythings is right and moving the rows correctly.
ItemTouchHelper.SimpleCallback:
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
int fromPosition = viewHolder.getAdapterPosition();
int toPosition = target.getAdapterPosition();
Collections.swap(adapter.getShelves(), fromPosition, toPosition);
adapter.notifyItemMoved(fromPosition, toPosition);
return true;
}
// Minifying the rows before the onMove is started:
#Override
public boolean isLongPressDragEnabled() {
adapter.minifyRow(position);
return true;
}
It should minify the rows when long pressing, before onMoving() is started. Then should start the moving without any interrupting. The problem is starting dragging and minifiying the row when long press but interrupting the dragging at the moment. So it doesn't dragging or moving the row. How to simulate onMove with notify?
In adapter class:
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
boolean rowIsExpanded = rows.get(position).isExpanded();
if(rowIsExpanded){
holder.rowLayout.setVisibility(View.VISIBLE);
} else {
holder.rowLayout.setVisibility(View.GONE);
}
}
public void minifyRow(boolean situation, int adapterPos){
RowModel row = rows.get(adapterPos);
row.setExpanded(situation);
notifyItemChanged(adapterPos);
}
You shouldn't call adapter.minifyRow(position); inside isLongPressDragEnabled it only flags to check your adapter enable long-press or not. You should override onSelectedChanged. Remove method in isLongPressDragEnabled and Try this
#Override
public void onSelectedChanged(#Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
if (viewHolder != null && actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
((YourViewHolder)viewHolder).rowLayout.setVisibility(View.GONE);
}
}
Related
Problem
In my acitvity i have two recyclerViews showing popular movies and top rated movies respectively.
When I click on a movie in a recyclerView, that movie is sent to an activity showing its details.
I go back to the home and if I click on a movie of another recyclerView, the previously clicked movie is shown.
I need to show the details of any clicked movie in any recyclerView.
I have a single viewHolder (MovieViewHolder)
public class MovieViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//Widgets
TextView title, rating;
ImageView imageView;
//Click listener
OnMovieListener onMovieListener ;
public MovieViewHolder(#NonNull View itemView, OnMovieListener onMovieListener) {
super(itemView);
this.onMovieListener = onMovieListener;
title = itemView.findViewById(R.id.movie_title);
imageView = itemView.findViewById(R.id.movie_img);
rating = itemView.findViewById(R.id.rating_bar);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
onMovieListener.onMovieClick(getAdapterPosition());
}
An interface
public interface OnMovieListener {
void onMovieClick(int position);
void onCategoryClick(String category);
}
This two Adapter:
This for popular movies
public class MovieRecyclerView extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<MovieModel> mMovies;
private final OnMovieListener onMovieListener;
public MovieRecyclerView(OnMovieListener onMovieListener) {
this.onMovieListener = onMovieListener;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_item,
parent, false);
return new MovieViewHolder(view, onMovieListener);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
((MovieViewHolder) holder).title.setText(mMovies.get(position).getTitle());
((MovieViewHolder) holder).rating.setText(String.valueOf((float) (mMovies.get(position).getVote_average())));
//ImageView: Using Glide Library
Glide.with(holder.itemView.getContext())
.load("https://image.tmdb.org/t/p/w500/"
+ mMovies.get(position).getPoster_path())
.into(((MovieViewHolder) holder).imageView);
}
#Override
public int getItemCount() {
if (mMovies != null) {
return mMovies.size();
}
return 0;
}
public void setmMovies(List<MovieModel> mMovies) {
this.mMovies = mMovies;
notifyDataSetChanged();
}
//Getting the id of the movie clicked
public MovieModel getSelectedMovie(int position) {
if (mMovies != null) {
if (mMovies.size() > 0) {
return mMovies.get(position);
}
}
return null;
}
This for top_rated
public class MovieLatestRecyclerView extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<MovieModel> mMoviesLatest;
private OnMovieListener onMovieListener;
public MovieLatestRecyclerView(OnMovieListener onMovieListener) {
this.onMovieListener = onMovieListener;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_item,
parent, false);
return new MovieViewHolder(view, onMovieListener);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
((MovieViewHolder)holder).title.setText(mMoviesLatest.get(position).getTitle());
((MovieViewHolder)holder).rating.setText(String.valueOf((float) (mMoviesLatest.get(position).getVote_average())));
//ImageView: Using Glide Library
Glide.with(holder.itemView.getContext())
.load("https://image.tmdb.org/t/p/w500/"
+ mMoviesLatest.get(position).getPoster_path())
.into(((MovieViewHolder)holder).imageView);
}
#Override
public int getItemCount() {
if(mMoviesLatest != null) {
return mMoviesLatest.size();
}
return 0;
}
public void setmMoviesLatest(List<MovieModel> mMoviesLatest) {
this.mMoviesLatest = mMoviesLatest;
notifyDataSetChanged();
}
//Getting the id of the movie clicked
public MovieModel getSelectedMovie(int position){
if(mMoviesLatest != null){
if(mMoviesLatest.size() > 0){
return mMoviesLatest.get(position);
}
}
return null;
}
In MainActivity i configure the recyclerViews with this method
private void ConfigureRecyclerView() {
movieRecyclerViewAdapter = new MovieRecyclerView(this);
movieLatestRecyclerViewAdapter = new MovieLatestRecyclerView(this);
recyclerView_popular.setAdapter(movieRecyclerViewAdapter);
recyclerView_popular.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
recyclerView_latest.setAdapter(movieLatestRecyclerViewAdapter);
recyclerView_latest.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
//RecyclerView Pagination
//Loading next page of api response
recyclerView_popular.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(#NonNull RecyclerView recyclerView, int newState) {
if (!recyclerView.canScrollVertically(1)) {
//Here we need to display the next search result
movieListViewModel.searchNextPage();
}
}
});
recyclerView_latest.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(#NonNull RecyclerView recyclerView, int newState) {
if (!recyclerView.canScrollVertically(1)) {
//Here we need to display the next search result
movieListViewModel.searchNextPage();
}
}
});
}
And i use this for passing the selected movie to another activity
#Override
public void onMovieClick(int position) {
//WE don't need position of the movie in recyclerView
// We need the ID of the movie in order to get all it's details
Intent intent = new Intent(this, MovieDetails.class);
intent.putExtra("movie", movieRecyclerViewAdapter.getSelectedMovie(position));
startActivity(intent);
}
Whoever you are reading my question I thank you for your time and I apologize for the certain banality and length.
At this line:
intent.putExtra("movie", movieRecyclerViewAdapter.getSelectedMovie(position));
You are getting a movie from movieRecyclerViewAdapter. This adapter is only used with the 'Popular' movie recycler view.
onMovieClick is used for both the 'Popular' and the 'Top Rated' recycler views. So, clicking an item from 'Top Rated' will attempt to use an item from 'Popular'.
To fix this, a click from 'Top Rated' should not attempt to get an item from movieRecyclerViewAdapter.
You could do something like change onMovieClick(int position) to onMovieClick(MovieModel movie), so you don't have to worry about knowing which adapter to get the movie from.
UPDATE When I Log.i("adapterPos", String.valueOf(adapter.adapterPos)); in onSwiped, it returns -1 even before setting it to -1...
I have a recyclerView, as well as a custom adapter to display information about an exercise.
When a user taps on a recyclerView item, the items background is set to green, and the save button turns into an update button, so that the user can update their existing recorded exercises.
I also have an onSwiped method which is implemented inside my fragment. When a user swipes an item, it is deleted from the database.
When a user swipes to delete an item, I would like to set the background for ALL items back to the default (grey).
How could this be done?
Note: if item 2 is selected and the user decides to swipe item 1, even after item 1 is deleted, item 2 is still currently selected.
Record Exercise Fragment (Relevant code)
#Override
public void onExerciseClicked(int position) {
if (recyclerItemClicked == false) {
saveBtn.setText("Update");
clearBtn.setVisibility(View.GONE);
recyclerItemClicked = true;
double selectedWeight = adapter.getWeight(position);
String selectedWeightString = Double.toString(selectedWeight);
editTextWeight.setText(selectedWeightString);
int selectedReps = adapter.getReps(position);
String selectedRepsString = Integer.toString(selectedReps);
editTextReps.setText(selectedRepsString);
} else {
clearBtn.setVisibility(View.VISIBLE);
saveBtn.setText("Save");
recyclerItemClicked = false;
}
}
public void initRecyclerView() {
adapter = new CompletedExercisesListAdapter2(allExercises, this);
recyclerView.setAdapter(adapter);
new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);
}
public void setExercises(List<Log_Entries> exercises) {
allExercises.clear();
allExercises.addAll(exercises);
}
ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int logID = allExercises.get(viewHolder.getAdapterPosition()).getLog_id();
logViewModel.deleteByID(logID);
clearBtn.setVisibility(View.VISIBLE);
saveBtn.setText("Save");
Log.i("adapterPos", String.valueOf(adapter.adapterPos)); // this is always -1 for some reason
// backup the currently selected position
int selectedPos = adapter.adapterPos;
// change the currently selected position to -1
adapter.adapterPos = -1;
adapter.notifyItemChanged(selectedPos);
}
};
Adapter
public class CompletedExercisesListAdapter2 extends RecyclerView.Adapter {
private OnExerciseClickListener onExerciseClickListener;
private List<Log_Entries> allCompletedExercises = new ArrayList<>();
public int adapterPos = -1;
public boolean isSelected = false;
public CompletedExercisesListAdapter2(ArrayList<Log_Entries> allCompletedExercises, OnExerciseClickListener onExerciseClickListener) {
this.allCompletedExercises = allCompletedExercises;
this.onExerciseClickListener = onExerciseClickListener;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view;
if (viewType == 0) {
view = layoutInflater.inflate(R.layout.new_completed_exercise_item, parent, false);
return new ViewHolderOne(view, onExerciseClickListener);
}
view = layoutInflater.inflate(R.layout.completed_exercise_item, parent, false);
return new ViewHolderTwo(view, onExerciseClickListener);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == 0) {
ViewHolderOne viewHolderOne = (ViewHolderOne) holder;
if (adapterPos == position) {
viewHolderOne.relativeLayout.setBackgroundColor(Color.parseColor("#567845"));
}
else {
viewHolderOne.relativeLayout.setBackgroundResource(R.color.dark_grey);
}
viewHolderOne.textViewExerciseName.setText(String.valueOf(allCompletedExercises.get(position).getChildExerciseName()));
viewHolderOne.textViewSetNumber.setText(String.valueOf(viewHolderOne.getAdapterPosition() + 1));
viewHolderOne.textViewWeight.setText(String.valueOf(allCompletedExercises.get(position).getTotal_weight_lifted()));
viewHolderOne.textViewReps.setText(String.valueOf(allCompletedExercises.get(position).getReps()));
} else if (getItemViewType(position) == 1) {
ViewHolderTwo viewHolderTwo = (ViewHolderTwo) holder;
if (adapterPos == position) {
viewHolderTwo.relativeLayout.setBackgroundColor(Color.parseColor("#567845"));
} else {
viewHolderTwo.relativeLayout.setBackgroundResource(R.color.dark_grey);
}
viewHolderTwo.textViewSetNumber.setText(String.valueOf(viewHolderTwo.getAdapterPosition() + 1));
viewHolderTwo.textViewWeight.setText(String.valueOf(allCompletedExercises.get(position).getTotal_weight_lifted()));
viewHolderTwo.textViewReps.setText(String.valueOf(allCompletedExercises.get(position).getReps()));
}
}
#Override
public int getItemCount() {
return allCompletedExercises.size();
}
#Override
public int getItemViewType(int position) {
// if list is sorted chronologically
if (position == 0) {
return 0;
}
if (allCompletedExercises.get(position).getChildExerciseName().equals(allCompletedExercises.get(position - 1).getChildExerciseName())) {
return 1;
} else {
return 0;
}
}
public class ViewHolderOne extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView textViewExerciseName;
private TextView textViewSetNumber;
private TextView textViewWeight;
private TextView textViewReps;
OnExerciseClickListener mOnExerciseClickListener;
private RelativeLayout relativeLayout;
public ViewHolderOne(#NonNull View itemView, OnExerciseClickListener onExerciseClickListener) {
super(itemView);
textViewExerciseName = itemView.findViewById(R.id.textView_ExerciseName3);
textViewSetNumber = itemView.findViewById(R.id.textView_Set_Number56);
textViewWeight = itemView.findViewById(R.id.textView_weight78);
textViewReps = itemView.findViewById(R.id.textView_repss0);
mOnExerciseClickListener = onExerciseClickListener;
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.exercise_item_relative);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
onExerciseClickListener.onExerciseClicked(getAdapterPosition());
if (isSelected) {
adapterPos = -1;
isSelected = false;
} else {
adapterPos = getAdapterPosition();
isSelected = true;
}
notifyDataSetChanged();
}
}
class ViewHolderTwo extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView textViewSetNumber;
private TextView textViewWeight;
private TextView textViewReps;
OnExerciseClickListener mOnExerciseClickListener;
private RelativeLayout relativeLayout;
public ViewHolderTwo(#NonNull View itemView, OnExerciseClickListener onExerciseClickListener) {
super(itemView);
textViewSetNumber = itemView.findViewById(R.id.textView_Set_Number);
textViewWeight = itemView.findViewById(R.id.textView_weight);
textViewReps = itemView.findViewById(R.id.textView_repss);
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.exercise_item_rel);
mOnExerciseClickListener = onExerciseClickListener;
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
onExerciseClickListener.onExerciseClicked(getAdapterPosition());
if (!isSelected) {
adapterPos = getAdapterPosition();
isSelected = true;
} else {
adapterPos = -1;
isSelected = false;
}
notifyDataSetChanged();
}
}
public interface OnExerciseClickListener {
void onExerciseClicked(int position);
}
public double getWeight(int position) {
double weight = allCompletedExercises.get(position).getTotal_weight_lifted();
return weight;
}
public int getReps(int position) {
int reps = allCompletedExercises.get(position).getReps();
return reps;
}
}
In onSwipe(), just reset the adapterPos of your adapter object to -1 and call notifyItemChanged() on that position.
// backup the currently selected position
int selectedPos = adapter.adapterPos;
// change the currently selected position to -1
adapter.adapterPos = -1;
adapter.isSelected = false;
adapter.notifyItemChanged(selectedPos);
Note: onSwipe() is called after the ViewHolder being swiped fully.
I think the only thing you need to do for this to work is to make sure that notifyDataSetChanged() is executed once after you set adapterpos= -1.
notifyDataSetChanged() will reexecute onBindViewHolder for the entire adapter as opposed to notifyItemChanged() which should result in all items evaluating to a gray background.
you are actually already doing this in your viewholders onClick method in order to prevent multiple items to have a green background at once. so yust execute the method again after a swipe. so just do the following:
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int logID = allExercises.get(viewHolder.getAdapterPosition()).getLog_id();
logViewModel.deleteByID(logID);
clearBtn.setVisibility(View.VISIBLE);
saveBtn.setText("Save");//<-- why if no item is selected?
// backup the currently selected position
//int selectedPos = adapter.adapterPos;<-- why would you do this, nothing is not shown as selected
// change the currently selected position to -1
adapter.adapterPos = -1;
adapter.notifyDataSetChanged();//now everything will be gray
}
this will work for sure but it will mess with the delete animation you might be using after the swipe. following the previous answer another code that might work is the following:
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int copyOfAdapterPos=adapterPos;
adapterPos=-1;
notifyItemChanged(copyOfAdapterPos);
int logID = allExercises.get(viewHolder.getAdapterPosition()).getLog_id();
logViewModel.deleteByID(logID);
clearBtn.setVisibility(View.VISIBLE);
saveBtn.setText("Save");//<-- why if no item is selected?
adapter.notifyItemRemoved(viewHolder.getAdapterPosition());//now everything will be gray
}
i havnt tested it but if it works it will look nicer
I have a fragment with a recyclerView.
A user is allowed to delete an exercise logEntry by swiping right.
A user can also update a logEntry by clicking on the recyclerView item.
(This highlights the current selected item green, hides the clear button and converts the save button to an update button.)
To deselect the item , the user can click on the recyclerView a second time.
Unfortunately, if a user was to select an item and then decides to swipe to delete an item, the recyclerView item which takes the same position as the originally selected item is remains highlighted green.
When a user swipes to delete an item, I would like the background of all recyclerView items to be reset to grey.
How could this be done?
Relevant Fragment Code
OnExerciseClicked
#Override
public void onExerciseClicked(int position) {
if (recyclerItemClicked == false) {
saveBtn.setText("Update");
clearBtn.setVisibility(View.GONE);
recyclerItemClicked = true;
double selectedWeight = adapter.getWeight(position);
String selectedWeightString = Double.toString(selectedWeight);
editTextWeight.setText(selectedWeightString);
int selectedReps = adapter.getReps(position);
String selectedRepsString = Integer.toString(selectedReps);
editTextReps.setText(selectedRepsString);
} else {
clearBtn.setVisibility(View.VISIBLE);
saveBtn.setText("Save");
recyclerItemClicked = false;
}
}
OnSwiped
ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int logID = allExercises.get(viewHolder.getAdapterPosition()).getLog_id();
logViewModel.deleteByID(logID);
clearBtn.setVisibility(View.VISIBLE);
saveBtn.setText("Save");
adapter.notifyDataSetChanged();
}
};
Adapter
public class CompletedExercisesListAdapter2 extends RecyclerView.Adapter {
private OnExerciseClickListener onExerciseClickListener;
private List<Log_Entries> allCompletedExercises = new ArrayList<>();
private int adapterPos = -1;
public boolean flag = false;
public CompletedExercisesListAdapter2(ArrayList<Log_Entries> allCompletedExercises, OnExerciseClickListener onExerciseClickListener) {
this.allCompletedExercises = allCompletedExercises;
this.onExerciseClickListener = onExerciseClickListener;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view;
if (viewType == 0) {
view = layoutInflater.inflate(R.layout.new_completed_exercise_item, parent, false);
return new ViewHolderOne(view, onExerciseClickListener);
}
view = layoutInflater.inflate(R.layout.completed_exercise_item, parent, false);
return new ViewHolderTwo(view, onExerciseClickListener);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == 0) {
ViewHolderOne viewHolderOne = (ViewHolderOne) holder;
if (adapterPos == position) {
viewHolderOne.relativeLayout.setBackgroundColor(Color.parseColor("#567845"));
} else {
viewHolderOne.relativeLayout.setBackgroundResource(R.color.dark_grey);
}
viewHolderOne.textViewExerciseName.setText(String.valueOf(allCompletedExercises.get(position).getChildExerciseName()));
//viewHolderOne.textViewSetNumber.setText(String.valueOf(allCompletedExercises.get(position).getSet_number()));
viewHolderOne.textViewSetNumber.setText(String.valueOf(viewHolderOne.getAdapterPosition() + 1));
viewHolderOne.textViewWeight.setText(String.valueOf(allCompletedExercises.get(position).getTotal_weight_lifted()));
viewHolderOne.textViewReps.setText(String.valueOf(allCompletedExercises.get(position).getReps()));
} else if (getItemViewType(position) == 1) {
ViewHolderTwo viewHolderTwo = (ViewHolderTwo) holder;
if (adapterPos == position) {
viewHolderTwo.relativeLayout.setBackgroundColor(Color.parseColor("#567845"));
} else {
viewHolderTwo.relativeLayout.setBackgroundResource(R.color.dark_grey);
}
// viewHolderTwo.textViewSetNumber.setText(String.valueOf(allCompletedExercises.get(position).getSet_number()));
viewHolderTwo.textViewSetNumber.setText(String.valueOf(viewHolderTwo.getAdapterPosition() + 1));
viewHolderTwo.textViewWeight.setText(String.valueOf(allCompletedExercises.get(position).getTotal_weight_lifted()));
viewHolderTwo.textViewReps.setText(String.valueOf(allCompletedExercises.get(position).getReps()));
}
}
#Override
public int getItemCount() {
return allCompletedExercises.size();
}
#Override
public int getItemViewType(int position) {
// if list is sorted chronologically
if (position == 0) {
return 0;
}
if (allCompletedExercises.get(position).getChildExerciseName().equals(allCompletedExercises.get(position - 1).getChildExerciseName())) {
return 1;
} else {
return 0;
}
}
class ViewHolderOne extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView textViewExerciseName;
private TextView textViewSetNumber;
private TextView textViewWeight;
private TextView textViewReps;
OnExerciseClickListener mOnExerciseClickListener;
private RelativeLayout relativeLayout;
public ViewHolderOne(#NonNull View itemView, OnExerciseClickListener onExerciseClickListener) {
super(itemView);
textViewExerciseName = itemView.findViewById(R.id.textView_ExerciseName3);
textViewSetNumber = itemView.findViewById(R.id.textView_Set_Number56);
textViewWeight = itemView.findViewById(R.id.textView_weight78);
textViewReps = itemView.findViewById(R.id.textView_repss0);
mOnExerciseClickListener = onExerciseClickListener;
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.exercise_item_relative);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
onExerciseClickListener.onExerciseClicked(getAdapterPosition());
if (flag) {
adapterPos = -1;
flag = false;
} else {
adapterPos = getAdapterPosition();
flag = true;
}
notifyDataSetChanged();
}
}
class ViewHolderTwo extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView textViewSetNumber;
private TextView textViewWeight;
private TextView textViewReps;
OnExerciseClickListener mOnExerciseClickListener;
private RelativeLayout relativeLayout;
public ViewHolderTwo(#NonNull View itemView, OnExerciseClickListener onExerciseClickListener) {
super(itemView);
textViewSetNumber = itemView.findViewById(R.id.textView_Set_Number);
textViewWeight = itemView.findViewById(R.id.textView_weight);
textViewReps = itemView.findViewById(R.id.textView_repss);
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.exercise_item_rel);
mOnExerciseClickListener = onExerciseClickListener;
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
onExerciseClickListener.onExerciseClicked(getAdapterPosition());
if (!flag) {
adapterPos = getAdapterPosition();
flag = true;
} else {
adapterPos = -1;
flag = false;
}
notifyDataSetChanged();
}
}
public interface OnExerciseClickListener {
void onExerciseClicked(int position);
}
public double getWeight(int position) {
double weight = allCompletedExercises.get(position).getTotal_weight_lifted();
return weight;
}
public int getReps(int position) {
int reps = allCompletedExercises.get(position).getReps();
return reps;
}
}
I highly recommend using a library like Groupie to abstract away the complicated state management of RecyclerView. Otherwise you'll find this imperative style quickly becomes unmaintainable. The future is declarative UI (Compose etc). This way it is much easier to keep track of Items rather than positions.
It's quite hard to follow your code (you should use descriptive names for things, like isSelected instead of flag). However I think your problem lies in your bind function not returning false here:
if (adapterPos == position)
after you swipe. Use break points to work out why
I am trying to add this function to my RecyclerView, that when I swipe one item either to the left or the right it gets deleted.
But when I execute my App I can't even swipe one of the items.
Appreciate any help
new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(mRecyclerView);
ItemTouchHelper.SimpleCallback itemTouchHelperCallback= new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
mExampleList.remove(viewHolder.getAdapterPosition());
mAdapter.notifyDataSetChanged();
saveData();
}
By the way this RecyclerView is not located in my MainActivity but in a Fragment that can be opened from the Drawer menu in the MainActivity, so maybe the problem lies there?
Okay so I have finally found the problem.
I just had to initialize the ItemTouchHelper in the method where the RecyclerViewis built instead of in the onCreate method.
So this is what the code looks like:
private void buildRecyclerView() {
mRecyclerView = view.findViewById(R.id.recyclerview);
mLayoutManager = new LinearLayoutManager(getActivity());
mAdapter = new ExampleAdapter(mExampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
new ItemTouchHelper(itemTochHelperCallback).attachToRecyclerView(mRecyclerView);
mRecyclerView.setAdapter(mAdapter);
}
ItemTouchHelper.SimpleCallback itemTochHelperCallback=new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
mExampleList.remove(viewHolder.getAdapterPosition());
mAdapter.notifyDataSetChanged();
}
};
So I have a recyclerview loaded with Firebase Firestore Documents and I want to see if it is beeing swiped up or down. Depending on the direction I want to make the adapter.startListening(); again and stop if he swipes down. I also want to hide a button if the user scrolls down. I just need to figure out how can I see if he does that ?
This piece of code was working with left and right but for some reason won´t with up and down ?
new ItemTouchHelper( new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.UP | ItemTouchHelper.DOWN) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
if(direction == ItemTouchHelper.UP){
Toast.makeText(getActivity(), "You swiped up", Toast.LENGTH_LONG).show();
}
}
});
Here the complete code if it some reason interfers with smth don´t know
public class HomeFragment extends Fragment {
View myFragment;
private ImageButton postButton;
CollectionReference postsRef;
FirebaseFirestore db;
private PostsAdapter adapter;
FirebaseAuth mAuth;
RecyclerView recyclerView;
public static ProfileFragment getInstance() {
return new ProfileFragment();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragment = inflater.inflate(R.layout.fragment_home, container, false);
postButton = myFragment.findViewById( R.id.postButton);
recyclerView = myFragment.findViewById(R.id.postlist);
mAuth = FirebaseAuth.getInstance();
db = FirebaseFirestore.getInstance();
postsRef = db.collection("posts");
SetupRecyclerView();
postButton.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
SendUserToPostActivity();
}
});
new ItemTouchHelper( new ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.UP | ItemTouchHelper.DOWN) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
if(direction == ItemTouchHelper.UP){
Toast.makeText(getActivity(), "You swiped up", Toast.LENGTH_LONG).show();
}
}
});
adapter.setOnConfirmClickListener( new PostsAdapter.onConfirmClickListener() {
#Override
public void onConfirmClick(DocumentSnapshot documentSnapshot, int position) {
Posts post = documentSnapshot.toObject(Posts.class);
String id = documentSnapshot.getId();
if(post.getUid().equals(mAuth.getCurrentUser().getUid())){
adapter.deleteItem(position);
Toast.makeText(getActivity(), "Post deleted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "You cannot delete this", Toast.LENGTH_LONG).show();
}
}
});
return myFragment;
}
#Override
public void onStart(){
super.onStart();
adapter.startListening();
}
#Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
private void SetupRecyclerView() {
Query query = postsRef.orderBy("time", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Posts> options = new FirestoreRecyclerOptions.Builder<Posts>().setQuery(query, Posts.class).build();
adapter = new PostsAdapter(options);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(adapter);
}
private void SendUserToPostActivity() {
Intent postIntent = new Intent(getActivity(), PostActivity.class);
startActivity(postIntent);
}
}
You're using the wrong listener. An ItemTouchHelper applies to the individual items within the recyclerview, rather than the view as whole.
What you're looking for is OnScrollListener:
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);
//This is where you can track a scroll change
}
});
dy in the onScrolled method refers to the change in y position on a scroll event, likewise for dx.
In that method, let's say you wanted to check if the recyclerview reached the limit of scrolling either up or down, then within that onScroll method (or wherever you're checking that), you can add a call to recyclerView.canScrollVertically(int). The int will be a value of -1 for UP, 1 for DOWN, and 0 for FALSE, i.e. cannot scroll.
Checking if the recyclerview reached the bottom:
if(!recyclerView.canScrollVertically(1)){
//The recyclerview can no longer scroll downwards, you have reached the bottom
}
Editing to add: Your itemTouchHelper also didn't work correctly because vertical directions are to be specified in the first parameter of the callback, and horizontal directions in the second parameter. You passed 0 for the first parameter, so it sees no vertical direction as an option