I have set up an Recyclerview with textviews and hidden buttons per row.
if the user clicks the item, the buttons are shown.
If the user clicks another item, the buttons are hidden.
The recycleview sends the position to the activity per interface which changes its property.
As long as the recyclerview is not scrolled, everything works perfectly.
Layoutmanager.getchildat(position)
Now, when items exceed the view, and the user scrolls the recyclerview, the position gets wrong. It adds up 1 per item scrolled down that the user has clicked. For Example the third Item is the first to be seen, the user clicks the fifth, the buttons are shown in the eight row.
How to fix that? Can i access a gettarget property? I tried smoothcontroller.gettargetposition and subtracted it from the position but its not the right solution.
Edit:
Just added a onViewClick(View v) to the interface and saved the viee to activity when buttons set to visible. When another view is clicked, the saved view is used to hide the buttons. (View oldView = view)
Just added a onViewClick(View v) to the interface and saved the viee to activity when buttons set to visible. When another view is clicked, the saved view is used to hide the buttons. (View oldView = view)
Related
I have searched a lot in SO, but didn't get similar type thread. In my application, i need to add or remove item in activity's layout when user clicks on listview's item. Listview displays data using a custom array adapter. If user clicks on an item then it will be added to the activity's layout and further clicks on the same item, it will be removed. I don't know how to accomplish this task. Any help will be appreciated in this context.
Here is an image:
I do not know what UI effect do you want.
1. If you want the top panel section in your image fixed(do not scroll) when scroll list, then you can:
Use a LinearLayout as root element
Add a LinearLayout as top panel
Add ListView below top panel
Create a selectedView.xml that you what add to top panel when click a item of ListView
When user clicks item of ListView, you can try code below:
ItemModel itemData = dataList.get(position)
// inflate selectItemView and add to top panel
View selectItemView= LayoutInflater.from(this).inflate(R.layout.selectedView, topPanelView, true);
View text1 = selectItemView.findViewById(xxx);
text1.setText(itemData.getxxx);
...
//inflate other field using itemData
...
If you want top panel scroll out of top when scroll ListView.
Make top panel as HeaderView of ListView and follow the part step of 1 and code.
How do i get a specific view inside of a RecyclerView item? For example I have a floating action button inside of a recyclerview item. The recyclerview card can swipe to trigger an event (new activity) and when the user swipes i am making the fab invisible. I want so that when the user returns back to the activity with the recyclerview, the fab is visible again.
I've tried to implement this a few ways, but for some reason it's taking the fab from the NEXT card/item and placing it on the original card/item that was swiped. This is a problem because the next card might have a different colored fab or no fab at all. What is happening is that it's taking the most recent viewholder item, even if the previous one is the one I want to deal with.
So i need a way to reference the fab in the current item. I'm currently setting it to currentFab = holder.mFab (but again, it's taking the most recent holder item even though it's not the one I pressed on). I need a way to reference the fab in a specific item.
I've tried something similar in the past, I've added the Swipe function in "onBindViewHolder ", Using a Swipe Library.
What I have made is, A CardView, Inside it there are 2 Strings, And a Button, this Button will be invisible according to specific conditions, So because it's inside onBindVewHolder, it was easy to make some edits on the button.
Here's a sample :
holder.Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.Button.setVisibility(View.GONE);
}
});
Ofcourse this isn't a "fully working code" it's just an example, Plus you didn't put any code that you have tried.
By following Handle Button click inside a row in RecyclerView
and Issue with CardView and OnClickListener in RecyclerView my code is working for both view i.e complete row and imageview inside row.
If imageview inside row is clicked then onclick of row item should not fired, but it happen following approach in given links
Like for ListView item if you give focusable="true" of view inside row then onItemClick is not fired, if view inside row is clicked (only onClick(View) is fired.
How to make it possible with Recycler view?
You may have better success using listeners so that you can "bubble up" those events to your Activity/Fragment. Then you should be able to keep things separate.
Here is an example: https://guides.codepath.com/android/Using-the-RecyclerView#attaching-click-handlers-using-listeners
I followed the tutorial to create one for click on the row, and then created another (on my own) to handle clicking on the button within the row.
I have a listview in which my list contain textview,means I have a list of text where user can select the text, read it and set margin note at the end of that text and a button is created over there. So when next time the user read that text then the button/buttons is there and when click on that button a popup is open where he can see his margin note.
Now the problem is that he can create more than one margin note at the end. In this case I have to create one or more button dynamically at the end of that selected textview. So please help me I am unable to create more than one button dynamically.
There are a couple of ways to do it.
1) Create the buttons (max the user will require) already in the xml file of the list item element and set their visibility to invisible or gone. When the user selects it you can set the visibility to visible. The visibility can be set dynamically.
2) The other way is to add buttons programatically. List views do reuse views for performance purposes. this could explain as to why you are having trouble with the button moving positions. in this case you have identify the id of the list item and add and remove buttons every time the view is created in your getView method of your list adapter.
Remember to chk the condition on your getView method and set visibility or add/remove button.
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
System.out.println("I clicked row item");
button1= (Button)v.findViewById(R.id.button1);
button1.setVisibility(button1.isShown() ? View.GONE : View.VISIBLE);}
The problem:
I have a ListView item with a relatively complex layout. One of the children in the list item view is a TextView with links. The TextView can handle the clicks on links found in it(by using Linkify), and has a OnClickListener that handles click on parts of the text that are not links. The listener will just get the position of the view in the ListView and perform a click:
#Override
public void onClick(View v) {
int position = mListView.getPositionForView(v);
mListView.performItemClick(v, position, mAdapter.getItemId(position));
}
Everything works fine, except the click doesn't trigger the ListView to update the list item's drawable state. It does trigger that when you click on other TextViews in the item that are not clickable.
Thanks!
Got it working.
Set the list item view clickable and set its background as a state-list drawable, also set addStatesFromChildren to true. This will make sure the state of the list view item is changed when any of its children is clicked(assume the child is clickable).
However, this will cause the ListView itself not receiving focus, so you need to handle the click by adding View.OnClickListener to the list item itself instead of adding a OnItemClickedListener to the ListView.