Reputation: 2655
I have an overflow button inside a CardView
in Recyclerview
. Whenever I am clicking the button,I show a popup menu but also RecyclerView
is scrolling down one item. Can anyone please help me stop this unwanted scrolling?
Basically I am trying to replicate the same overflow button behavior as in Playstore.
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp">
<android.support.v7.widget.CardView
android:id="@+id/cv_tracks"
android:layout_width="match_parent"
android:layout_height="65dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<ImageView
android:id="@+id/ivTracks"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/unknown"
/>
<Button
android:id="@+id/imgbtn_overflow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:drawableTop="@drawable/ic_overflow"
android:paddingLeft="25dp"
android:paddingRight="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Adapter:
@Override
public void onBindViewHolder(final TracksViewHolder tracksViewHolder, int i) {
tracksViewHolder.imgBtnOverflow.setTag(i);
tracksViewHolder.imgBtnOverflow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Creating the instance of PopupMenu
final int position = (Integer) v.getTag();
PopupMenu popup = new PopupMenu(mContext,tracksViewHolder.imgBtnOverflow);
//Inflating the Popup using xml file
popup.getMenuInflater()
.inflate(R.menu.popup_menu_overflow, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
if(mPopUpClick!=null)
mPopUpClick.actionOnPopUpItemClick(position,item,songs.get(position));
return true;
}
});
popup.show(); //showing popup menu
}
});
}
UPDATE: Got the issue .When the popup menu displays,it slides the list down so as to display the whole dropdown. How to adjust popup to display up/down depending on the space available?
Upvotes: 15
Views: 3290
Reputation: 4805
I have same problem, and I use this way:
mLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false) {
@Override
public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate) {
return false;
}
};
mRecyclerView.setLayoutManager(mLayoutManager);
RecyclerView user layoutmanager to manage layout, so I think change layoutmanger behaver is the bettor solution.
Upvotes: 2
Reputation: 4805
requestRectangleOnScreen
not work for me, in my case, requestRectangleOnScreen
not called, when show popupMenu.
my solution is override requestChildRectangleOnScreen
, and reture false, work well.
public class PopupAncorRecyclerViewPager extends RecyclerView {
@Override
public boolean requestChildRectangleOnScreen(View child, Rect rect, boolean immediate) {
return false;
}
}
Upvotes: 1
Reputation: 6373
You can have your AnchorView override requestRectangleOnScreen()
and return false. This will prevent any parent ScrollView
from scrolling.
So, in your case, imgBtnOverflow
would be a custom ImageButton
, like so:
/**
* A custom {@link ImageButton} which prevents parent ScrollView scrolling when used as the
* anchor for a {@link android.support.v7.widget.PopupMenu}
*/
public class NonScrollImageButton extends ImageButton {
public NonScrollImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
return false;
}
}
Props to jankovd (https://gist.github.com/jankovd/19ef35efd1f00e9213fa)
An issue has been filed against the Support PopupMenu here: https://code.google.com/p/android/issues/detail?id=135439
Upvotes: 11
Reputation: 2655
Tried using android.widget.PopupMenu instead of android.support.v7.widget.PopupMenu and Voila, it works. So is it a bug in Support library. Can great developers out here confirm the same?
Upvotes: 32