How to disable recycler view item selection - android

movieListRecyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
// true: consume touch event
// false: dispatch touch event
return false;
}
});
but once disabled I am not able to enable it back ,is there any way to make it work like toggle which will enable and disable RecyclerView
also recyclerview.setEnabled(false) is not working

This will work:
Use boolean variable and setting default value to false and change value on button click.
boolean touch = false;
movieListRecyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
// true: consume touch event
// false: dispatch touch event
return touch;
}
});
and on your button click or whatever click change boolean value;
onClick(){
touch = !touch;
}

If you want to disable click on whole items of recyclerview then add below lines in parent view of row item.
android:clickable="false"
android:focusable="false"

Related

Is there a way to enable RecyclerView Touch Events back?

I'm disabling RecyclerView Touch Events by:
recyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
return true;
}
});
When I want to enable it back I'm just changing return statement to false, I'm adding OnItemTouchListener to RecyclerView again which returns false this time, like I've found on Stackoverflow. Unfortunately after doing this I can't swipe recycler items or scroll RecyclerView. How to enable Touch Events back?

Disable onClicklistner while other touch listner is active

I am trying to implement UI like google keep.
In the main layout there is frame layout in which there is a FAM(Floating Actions Menu) and a blackshadow view which is visible when FAM is expanded.
What i want is that when i touch the shadow view it should Collapse the FAM. To do that i have implemented onTouchlistner on shadowview.
shadowview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(fam.isExpanded()){
fam.collapse();
}
return false;
}
});
But what happens is that when i touch the area with MyCardGridView's card (Which open's another activity) open's another activity. Which should not happen.
You should try to use return true; instead of return false;, because as the documentation says :
Returns:
True if the listener has consumed the event, false otherwise.
Maybe the issue here is caused by return false

Disable / Enable Scroll for ListView in android

I have a ListView inside ScrollView. I can enable scroll of ListView by
listView.getParent().requestDisallowInterCeptTouchEvent(true);
But the problem is when i scroll up in listView and it reaches top it should scroll to parent view i.e. parent scroll has to work . How can i do this ? any suggestion please.
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return true; // Indicates that this has been handled by you and will not be forwarded further.
}
return false;
}
});
OR
To make the View unselectable just get the view and .setClickable(false)
OR
listView.setScrollContainer(false);
You can override ScrollView class and insert these methods inside:
private boolean isScrollEnabled = true;
public void enableScroll(boolean isScrollEnabled ) {
this.isScrollEnabled = isScrollEnabled ;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isScrollEnabled) {
return super.onInterceptTouchEvent(ev);
} else {
return false;
}
}
This is the cleanest solution to achieve this. You only call scrollView.enableScroll(true) to enable scrolling or scrollView.enableScroll(false) to disable it.
I would suggest to embed your upper view i.e any viewgroup above list view into listview header. ListView has a method, listview.addHeaderView(). That way you would be able to scroll your list (Whole View) even on small size display and you don't need scrollview.

android: onClickListener for relativelayout

I have a relativelayout and have coded the onTouchListener to highlight background as follows:
relative3.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View arg0, MotionEvent event)
{
if(event.getAction()==MotionEvent.ACTION_DOWN )
{
relative3.setBackgroundColor(getResources().getColor(R.color.tran_grey));
}
if((event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL))
{
relative3.setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
return true;
}
});
and to perform an action upon onClick
relative3.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
custom_toast("Redirecting...");
}
});
Question:
The relativelayout could show highlight upon ACTION_DOWN and turn back to transparent upon ACTION_UP. Yet it is clicked, the toast simply does not appear as coded.
How could that be modified? Thanks!
Make sure the layout has the property android:clickable="true"
The proper way to do the color change is to use a background drawable xml with a pressed and normal state. If you do this in code, use getActionMasked() instead of getAction()
Return false so so that your OnTouchListener doesn't consumed the event, so the clicklistener will work..
relative3.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View arg0, MotionEvent event)
{
if(event.getAction()==MotionEvent.ACTION_DOWN )
{
relative3.setBackgroundColor(getResources().getColor(R.color.tran_grey));
}
if((event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL))
{
relative3.setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
// change to false.
return false;
}
});
More info : http://developer.android.com/reference/android/view/View.OnTouchListener.html#onTouch(android.view.View, android.view.MotionEvent)
You need to return false in the onTouch method which tells the OnTouchListener that the click has not been consumed. Afterwards, it forwards the click event to onClick method of OnClickListener.

Why getAction method always return ACTION_DOWN?

I am using onTouchListener for a layout. I want to take the click outside the layout. I set onTouchListetener for the layout. But motion event always shows ACTION_DOWN. Even i touchOutside the view, It is not showing ACTION_OUTSIDE. Could anyone help me to find out why it is not showing constant ACTION_OUTSIDE. Here is the code i am using
Layout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("action",event.getAction()+"");
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
Toast.makeText(getApplicationContext(), "check", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
If you return false, then you're signifying that you do not wish to receive further touch events. You need to return true to continue getting motion events.
Very simple to fix by adding this:
override fun onTouchEvent(event: MotionEvent): Boolean {
return true
}
The event listener itself listens to one event action at a time. The first of course is the ACTION.DOWN, in which your toast shows.
Just like what Jason Robinson and user936414, you have to return it to true so that the object or the listener could here the second event action, ACTION.OUTSIDE.
You will get ACTION_OUTSIDE when the touch is outside the activity and the flag FLAG_WATCH_OUTSIDE_TOUCH is set. Refer http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_WATCH_OUTSIDE_TOUCH

Categories

Resources