stoping the SwipeMenuLIstView to Swipe - android

i am using Swipe menu list View from the Githu
https://github.com/baoyongzhang/SwipeMenuListView
According to my project requirement i need to stop some list View to Swipe
for example i want to stop the list item on the postion 5
how i would do that
mListView.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {
#Override
public void onSwipeStart(int position) {
if (positon == 5) {
Toast.makeText(getApplicationContext(), "you are not Allow to Swipe", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onSwipeEnd(int position) {
}
});

You can create a private instance of the listener and than set it to the mListView if a condition is met and if you want to not allow the swiping to be active you can simply set the onSwipeListener to null like this:
private SwipeMenuListView.OnSwipeListener swipeListener = new SwipeMenuListView.OnSwipeListener() {
#Override
public void onSwipeStart(int i) {
}
#Override
public void onSwipeEnd(int i) {
}
});
if (condtion) {
Toast.makeText(getApplicationContext(), "you are not Allow to Swipe", Toast.LENGTH_SHORT).show();
//disable swiping
mListView.setOnSwipeListener(null);
}else{
//allow swiping
mListView.setOnSwipeListener(swipeListener);
}

you can use BaseSwipListAdapter and change condition according your requirments.
#Override
public boolean getSwipEnableByPosition(int position) {
if(position==5){
return false;
}
return true;
}

Related

RecyclerView multiple selections and ActionMode

I'm working on a app that uses RecyclerView and ActionMode. On the RecyclerView item I have an Imageview (mMultipleSelectionBackground) that is set to GONE. Basically when I use long click / click on an item it will select it and highlight it (changing the ImageView to visible) (if multiple items are selected, it will change the ImageView to Visible on each particular one). I'm doing this in the Adapter class.
#Override
public boolean onLongClick(View view) {
int longClickedPosition = getAdapterPosition();
mLongClickListener.onToDoLongClick(longClickedPosition);
ToDo toDo = mToDos.get(longClickedPosition);
try {
/**
* Check to see if the item is selected
* #mMultipleSelections - use this to block the long click if the user already did it on an item
*/
if (!toDo.isSelected() && mMultipleSelections <= 0){
toDo.setSelected(true);
mMultipleSelections++;
selectedToDos.add(longClickedPosition);
mMultipleSelectionBackground.setVisibility(View.VISIBLE);
view.startActionMode(mActionModeCallback);
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
So far so good. The issue that I'm having is that when I'm using the onDestroyActionMode, I want the ImageView on all of the items to be reverted to GONE, but they are not. Only the first one is changed(and indeed, by this logic, it is normal).
#Override
public void onDestroyActionMode(ActionMode actionMode) {
Log.d(LOG_TAG, "DESTROY");
for (int x = 0; x < selectedToDos.size(); x++) {
mMultipleSelectionBackground.setVisibility(View.GONE);
mToDos.get(x).setSelected(false);
}
mMultipleSelections = 0;
}
My question is, how do I change the ImageView on all the items and not only the first one?
It will be better if you add one more extra boolean field inside your ToDo class which will be false by default for checking selection of that entry like
class ToDo{
...
private boolean isSelected;
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
...
}
So when you click or LongClick on your item just make that boolean value true/false accordingly and based on that value inside your adapter onBindViewHolder write following code
#Override
public void onBindViewHolder(#NonNull final UserViewHolder userViewHolder, int position) {
final Todo todo=mTodos.get(position);
if(todo.isSelected()){
viewHolder.imageView.setVisibility(View.VISIBLE);
}else{
viewHolder.imageView.setVisibility(View.GONE);
}
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
todo.setSelected(!todo.isSelected());
notifyDataSetChanged();
}
});
}
So Overall this boolean field will help you to easily manage your selection entries.

How can I have scrolling to last page working properly in a paginated scroll in android ListView

Attention! This question is not about dynamic loading of items into a very long ListView.
This is about adding PageUP and PageDown buttons to a ListView so that user can touch the button and scroll ListView page by page. Page means all fully and partially visible items on the screen.
I have partially implemented this in the following code, but my problem is that when I have lets say 10 items of approximately same height in the listview and 7 of them fit into the first page, then when I press PgDown button, user expects that item 8 to be on top of the screen (next page), but because there are only 10 items, ListView scrolls to the bottom of the list and because there is no extra scroll space I have item number 4 on top.
What is the best solution in this situation?
Should I add one item to the end of the list which will make the last page the height of the screen or there are any better options?
Here is my code:
public class cPaginatedListViewHelper {
Activity m_parentActivity;
private ListView mList;
//controls
private LinearLayout m_PagingLL;
//buttons
private ImageButton m_btnPrevPage;
private ImageButton m_btnNextPage;
private ImageButton m_btnExitPaginatedMode;
public cPaginatedListViewHelper(ListActivity mParent) {
this.m_parentActivity = mParent;
m_btnPrevPage=(ImageButton) mParent.findViewById(R.id.btnPrevPage);
m_btnNextPage=(ImageButton) mParent.findViewById(R.id.btnNextPage);
m_btnExitPaginatedMode =(ImageButton) mParent.findViewById(R.id.btnClosePage);
if(m_btnPrevPage!=null) {
m_btnPrevPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showSiblingPage(-1);
}
});
m_btnPrevPage.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mList.smoothScrollToPosition(0);
return true;
}
}
);
}
if(m_btnNextPage!=null) {
m_btnNextPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showSiblingPage(1);
}
});
m_btnNextPage.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mList.smoothScrollToPosition(mList.getCount());
return true;
}
}
);
}
m_btnExitPaginatedMode.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setEnabled(false);
m_PagingLL.setVisibility(View.GONE);
}
});
mList=mParent.getListView();
m_PagingLL = (LinearLayout) mParent.findViewById(R.id.pageControls);
}
public void updateControlsVisibility()
{
ViewTreeObserver observer = mList.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (willMyListScroll()) {
boolean psm = isEnabled();
//enable or disable
m_PagingLL.setVisibility( psm ? View.VISIBLE : View.GONE);
((View)mList).setVerticalScrollbarPosition(psm ? View.SCROLLBAR_POSITION_LEFT: View.SCROLLBAR_POSITION_RIGHT);
}
else
{
m_PagingLL.setVisibility(View.GONE);
((View)mList).setVerticalScrollbarPosition(View.SCROLLBAR_POSITION_RIGHT);
}
}
});
}
private boolean willMyListScroll() {
try {
int pos = mList.getLastVisiblePosition();
if (mList.getChildAt(pos).getBottom() > mList.getHeight()) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
private void showSiblingPage(int shift)
{
if(mList!=null) {
int iScrollPageHeight = mList.getHeight();
mList.scrollListBy(iScrollPageHeight * shift);
}
}
public void setEnabled(boolean psm) {
MyApp.Pref.edit().putBoolean("PSModeEnabled", psm).commit();
}
public boolean isEnabled(){
return MyApp.Pref.getBoolean("PSModeEnabled", false);
}
public void pagedScrollEnableDisable() {
boolean pagingEnabled = isEnabled();
pagingEnabled=!pagingEnabled;
setEnabled(pagingEnabled);
m_PagingLL.setVisibility( pagingEnabled ? View.VISIBLE : View.GONE);
updateControlsVisibility();
}
}
I finished up with using ListView's footer with variable height as shown in the following code:
LayoutInflater inflater = m_parentActivity.getLayoutInflater();
m_footerView = inflater.inflate(R.layout.listview_paged_overscroll, mList, false );
ViewGroup.LayoutParams lp =m_footerView.getLayoutParams();
if(m_tvPageNum!=null) recalcPagination();
if(lp!=null) lp.height = m_extraScrollFooterHeight;
int iFooters = mList.getFooterViewsCount();
if(iFooters==0) mList.addFooterView(m_footerView);

After clicking a button view is updating on both items of recyclerview

I have a RecyclerView with post cards. So there is a like button in every post card. When I tap on a like button. Sometimes it works fine and updates on current item. But after some scroll when I tap on like button it updates on another item simultaneously.
It doesn't send that data to server. But, only the view is updated. Then when I scroll up and down again. The data goes back to normal.
Problem is in the following onClick method:-
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child("like").exists()){
holder.like.setImageResource(R.drawable.ic_favorite_red_500_36dp);
holder.like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("like").removeValue();
}
});
}else if(dataSnapshot.child("dislike").exists()) {
holder.like.setImageResource(R.drawable.brokenheart);
holder.like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("dislike").removeValue();
}
});
}else{
holder.like.setImageResource(R.drawable.ic_favorite_border_red_500_36dp);
holder.like.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("dislike").removeValue();
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("like").setValue(true);
}
});
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
holder.like.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("like").removeValue();
FirebaseDatabase.getInstance().getReference().child("topics").child("likes").child(data.getId()).child(username).child("dislike").setValue(true);
return true;
}
});
Full Adapter class https://pastebin.com/UnFGahWT
Try the following:-
Set Your on click listeners in the public ViewHolder(View itemView) method and make Your ViewHolder class implement the View.OnClickListener.
In Adapter add:
public Topic getItem(int position) {
return topics.get(position);
}
In ViewHolder's onClick method add:
int position = getAdapterPosition();
if (position == RecyclerView.NO_POSITION) return;
item = getItem(position);
Thus You will get the exact object You need to change or do something with it.

how to disable the SwipeMenuLIstView to Swipe based on position?

i am using Swipe menu list View from the Github
https://github.com/baoyongzhang/SwipeMenuListView
According to my project requirement i need to stop list View to Swipe based on position.
For example i want to stop the list item to swipe on the position 2
how would i do that?
mListView.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {
#Override
public void onSwipeStart(int position) {
if (positon == 2) {
Toast.makeText(getApplicationContext(), "you are not Allow to Swipe", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onSwipeEnd(int position) {
}
});

How to keep selecting rows of RecyclerView during onClicks after pressing longClick?

I have a RecyclerView where I am selecting my rows only when I do onLongPress but what I want is that the first onLongClick will initiate the highlighting and then onClick on the row will select and de-select the particular rows and NOT onLongClick. After de-selecting the last selected row then onLongClick should initiate the highlighting again. I tried doing it with boolean variables. But it does not work. I am so confused. Any ideas?
This is what I have been upto.
if(onLongPressReceived) {
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!onClickAnotherPressReceived) {
onClickPressReceived = true;
mItems.get(holder.getAdapterPosition()).setmIsChecked(true);
notifyDataSetChanged();
onClickAnotherPressReceived = true;
}
}
});
holder.checkboxHolder.setVisibility(View.VISIBLE);
holder.mCheckBox.setChecked(mItems.get(position).getmIsChecked());
holder.mDeleteRow.setVisibility(View.VISIBLE);
if(mItems.get(holder.getAdapterPosition()).getmIsChecked()){
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_selected));
}else {
//holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_long_pressed));
holder.checkboxHolder.setVisibility(View.INVISIBLE);
holder.mDeleteRow.setVisibility(View.INVISIBLE);
}
if(onClickPressReceived && !onLongAnotherPressReceived)
{
holder.checkboxHolder.setVisibility(View.VISIBLE);
holder.mCheckBox.setChecked(mItems.get(position).getmIsChecked());
holder.mDeleteRow.setVisibility(View.VISIBLE);
if(mItems.get(holder.getAdapterPosition()).getmIsChecked()){
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_selected));
}else {
//holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_long_pressed));
holder.checkboxHolder.setVisibility(View.INVISIBLE);
holder.mDeleteRow.setVisibility(View.INVISIBLE);
}
}
//Checking whether a particular view is clicked or not
if(onClickAnotherPressReceived && !onLongAnotherPressReceived)
{
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_long_pressed));
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_unselected));
holder.mDeleteRow.setVisibility(View.INVISIBLE);
if (mItems.get(position).getmIsChecked()) {
holder.checkboxHolder.setVisibility(View.VISIBLE);
holder.mCheckBox.setChecked(true);
} else {
holder.checkboxHolder.setVisibility(View.GONE);
holder.mCheckBox.setChecked(false);
}
}
}
//Checking whether a particular view is clicked or not
else{
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_long_pressed));
holder.cardView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.list_unselected));
holder.mDeleteRow.setVisibility(View.INVISIBLE);
if (mItems.get(position).getmIsChecked()) {
holder.checkboxHolder.setVisibility(View.VISIBLE);
holder.mCheckBox.setChecked(true);
} else {
holder.checkboxHolder.setVisibility(View.GONE);
holder.mCheckBox.setChecked(false);
}
}
//Calls the interface method in Activity to respond to CheckBox changes
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
updateMainClass.updateListBackground(holder.getAdapterPosition(), b);
}
});
/**
* <p>Responds to long press made on any row</p>
* <p>Checks the item on which long press is made
* sets the onLongPressReceived status to true and notify the adapter to refresh the list.</p>
*/
holder.cardView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
if(onLongAnotherPressReceived) {
onLongPressReceived = true;
mItems.get(holder.getAdapterPosition()).setmIsChecked(true);
notifyDataSetChanged();
onLongAnotherPressReceived = false;
}
return true;
}
});
//Calls the interface in Activity to remove the item from the List.
holder.mDeleteRow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
updateMainClass.updateItemList(holder.getAdapterPosition());
}
});
}
I usually do this:
In your onBindViewHolder call bindView method of ViewHolder class:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
((ViewHolder) viewHolder).bindView(getItem(position));
}
Then in your viewHolder class do keep instance of your entity:
public class ViewHolder extends RecyclerView.ViewHolder {
YOUR_OBJECT entity;
public ViewHolder(View itemView) {
super(itemView);
//initialize view here
}
public void bindView(ChannelEntity entity) {
this.entity = entity; // use this entity for longClickListener
//setOnClickListener || OnLongClickListener Here
}
}

Categories

Resources