I have Recylerview on my Activity.I can disable recylerview items selection using below code
recyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
// true: consume touch event
// false: dispatch touch event
return touch;
}
});
But I want below functionality to show disable/enable look and feel
Enabled RV:
on Switching off,Disabled all or some items in RecylerView as below
I guess its kind some transparent view over `RecyclerView' as items are still clickable because I can see ripple effect on items click but functionality not works on disable. how can achieve this behavior in my application
After disabling the recyclerview from above code, change the opacity of each row and setEnable(False) for each row item and while enabling the recyclerview remove the opacity and setEnable(true).
inside the changelistner for toogle button refresh the recyclerview with your changes of opacity and all.
for you information i am adding a simple code:-
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b){
tvText.setEnabled(true);
tvText.setText("enabled");
tvText.setAlpha(1);
}else{
tvText.setText("disabled");
tvText.setAlpha(0.4f);
tvText.setEnabled(false);
}
}
});
refresh your recyclerview from if else loop and pass the enable/disable values to it and change the each row's alpha.
just add transparent view over recycler view and Set android:clickable="true" in xml to your foreground view.
Related
Do i necessairly need to change my recyclerView to expandableRecyclerView for doing animation of expand + animation of arrow drop down -> arrow up? Is there any way to implement simple slide down animation?
On button drop down arrow click i change visibility of textView and imageView from GONE to VISIBLE (also changing src of arrow button in code)
From:
To:
Here is some code just in case
taskViewHolder.showDesc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(taskViewHolder.description.getVisibility()==View.GONE){
taskViewHolder.showDesc.setImageDrawable(resources.getDrawable(R.drawable.drop_up_arrow));
taskViewHolder.imgDesc.setVisibility(View.VISIBLE);
taskViewHolder.description.setText(task.getDescription());
taskViewHolder.description.setVisibility(View.VISIBLE);
}
else {
taskViewHolder.showDesc.setImageDrawable(resources.getDrawable(R.drawable.drop_down_arrow));
taskViewHolder.description.setVisibility(View.GONE);
taskViewHolder.imgDesc.setVisibility(View.GONE);
}
}
});
Have a Look on my old project, I had to do something like you have told:
https://bitbucket.org/MauzerTheCat/music-example-app/src/master/
I recently implemented a small RecyclerView setup and tried out the recyclerview-selection library. So far, the selection works fine but I also connected a click handler on the recyclerview items and now every time I long-press an item to activate the selection mode, it also counts as a simple tap and the activity changes (because that is what I programmed it to do on item tap). I managed to avoid this by adding a simple boolean to my recyclerview adapter, which is called when the selection mode starts:
void setIgnoreClicks(boolean b) {
this.ignoreClicks = b;
}
and then in the bind-function of my viewholder:
void bind(MyModelClass m) {
...
view.setOnClickListener(() -> {
if(!adapter.isIgnoreClicks()) {
...
}
});
}
now when the selection mode ends, the boolean is set back to false and the taps go through again.
The problem is that when only one item selected and you tap on it to deselt it, the selection mode is also exited - which is fine, except that that tap is now not ignored anymore and so, the activity changes. What I want basically is to ignore that last tap too. Is there some way to stop the event if the selection mode is still active?
Thanks
Ok, solved this myself. What I did was to add a touch listener to my recyclerview which sets the ignoreClick to true if the actionmode is active and no item is clicked:
modelList.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
#Override
public boolean onInterceptTouchEvent(#NonNull RecyclerView rv, #NonNull MotionEvent e) {
if (e.getAction() != MotionEvent.ACTION_UP)
return false;
if(actionMode != null)
ignoreClick = rv.findChildViewUnder(e.getX(), e.getY()) != null; // ignore click if child is null (not clicked on a child, but the empty background of the recycler view)
return false;
}
...
});
and then in my item click handler:
private void showDetails(final Model model) {
if(ignoreClick)
ignoreClick = false; // the click is ignored, reset to false
else if (!selectionTracker.hasSelection()) {
final Intent intent = new Intent(MainActivity.this, ModelViewActivity.class);
intent.putExtra(Codes.DATA_MODEL, model);
startActivityForResult(intent, Codes.INTENT_MODEL_SHOW);
}
}
TL;DR I have a RecyclerView of EditTexts. When the user is focused on EditText #1 and taps on EditText #2, I want EditText #2 to get focus but I don't want the ReyclerView to scroll. How can I achieve this?
I'm trying to work with a RecyclerView populated with a bunch of EditTexts. When I'm focused on one EditText and I click on another, the RecyclerView scrolls so that the second is at the top of the screen. I want to disable this auto-scrolling from the RecyclerView, but I still want the user to be able to scroll, and I still want the second EditText to be focused on so the user can start typing. How can I achieve this?
I've already tried the following solutions:
https://stackoverflow.com/a/8101614/4077294, but with a RecyclerView.OnItemTouchListener. I called recyclerView.requestFocusFromTouch in onInterceptTouchEvent.
Behavior: Scrolled to the top of the tapped EditText all the time.
Clearing the focus from any EditText whenever it was focused on, via
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, bool hasFocus) {
if (hasFocus) {
v.clearFocus();
}
}
});
Behavior: The keyboard never showed up, and the RecyclerView still scrolled to the top.
Disabling scrolling altogether as in this question is not acceptable because I still want the user to be able to scroll.
I ended up with this solution from #pskink:
recylerView.setLayoutManager(new LinearLayoutManager(this) {
#Override
public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate) {
return false;
}
});
It seems to work perfectly, but #pskink has mentioned that this could have problems when using arrow keys. He's posted another solution here: https://pastebin.com/8JLSMkF7. If you have problems with the above solution, you may try the alternative solution at the link. For now, I'm sticking with the one I just posted here.
UPDATE
Since support-library v25.3.0 you should also override another requestChildRectangleOnScreen method in LayoutManager:
#Override
public boolean requestChildRectangleOnScreen(RecyclerView parent, View child, Rect rect, boolean immediate, boolean focusedChildVisible) {
return false;
}
RecyclerView will scroll to focused item.
Try recyclerView.setFocusable(false).
This worked for me.
I have an android ButtonView,
I set it to be unclickable:
mDoneBtn.setClickable(false);
later I set an onClickListener to it
mDoneBtn.setOnClickListener(new View.OnClickListener() {
does the latter make the button to be clickable again?
First you write:
mDoneBtn.setClickable(false);
Then you set onClick:
mDoneBtn.setOnClickListener(new View.OnClickListener() {...}
The button will trigger onClick event.
But if you set as:
mDoneBtn.setEnabled(false);
and then set your onClick, the onClick event will not be triggered.
Requested Edit:
setClickable automatically becomes true if onClickListenner is implemented. if you do setEnabled(false), until it is set to true, it will not be clickable even if you implement onClickListenner.
From View source:
public void setOnClickListener(OnClickListener l) {
if (!isClickable()) {
setClickable(true);
}
getListenerInfo().mOnClickListener = l;
}
I guess you can figure out the answer.
EDIT: valid for API level 19
The java.lang.RuntimeException is "Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead," but that is not correct. I am using setOnItemClickListener to do some stuff based on the new selection, but I also need to do some stuff before the user changes the selection. Specifically, I am collecting data for each selection that needs to be saved to a file before moving to another selection, since the other selection is associated with different set of data. Is there a way to use setOnClickListener with an Android Spinner?
spinner.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do some stuff before the user changes the selection
...
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
// Do some stuff based onItemSelected
...
You can replicate the an onclick event using ontouch events
this.spinner=(Spinner)findViewById(R.id.spinner);
this.spinner.setClickable(false);
this.spinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.v(TAG, "spinner touch");
//replicating a click
if(event.getAction() == MotionEvent.ACTION_UP){
v.playSoundEffect(android.view.SoundEffectConstants.CLICK);
}
return true;
}
});
You will have to set the Click listener on the underlying view (normally a TextView with id: android.R.id.text1) of the spinner. To do so:
Create a custom Spinner
In the constructor (with attributes) create the spinner by supplying the layout android.R.layout.simple_spinner_item
Do a findViewById(android.R.id.text1) to get the TextView
Now set the onClickListener to the TextView