listview doesn't scroll up until i touch it - android

i have a listView and a button ...
when i scroll down on the list .. the button which i named 'scroll_up' appears, so when i click it the list should scroll to the top.. but it didn't until i touch it.. i mean after clicking the button nothing happens ..but if i touch the list it scoll to the top ..
here is my button onClick
public void scrollup(View v){
lv.smoothScrollToPosition(0);
}
there is no error in the logcat...
Note:I use a header in the list .. maybe this what cause the problem ..
so is there a way to refresh the layout or somthing similar ?

If you want to move to first index of the list view, use below code:
listView.setSelection();

If you want to smooth scroll you may add line in listview in xml
android:smoothScrollbar="true"
or in code:
lv.setSmoothScrollbarEnabled(true);
And after your code
lv.smoothScrollToPosition(0);

Related

RecyclerView Item specific attribute OnClick

I'm having hard time making this work.
I have the following scenario - when click on CardView the most inner Image should get visible. On second click should disappear. This works fine.
However I want when I select the second CardView the most inner Image to appear the the Image on the first CardView to disappear.
I have this:
1. CardView generated in XML - the cardview have 2 ImageView inside
2. RecyclerView with CustomAdapter and ViewHolder.
3. When I implement OnClick inside ViewHolder it works for each Item - on click the Image appears and on second it disappears.
However I don't know how to check on which Item the image is visible so I can hide it if another Item is clicked. In other word if I select the second I want the first to be deselected. I don't know how to handle this per position.
Any ideas?
Seems like I was missing the point. I solved the problem simply with:
notifyItemChanged(position);
This redraws the item on this position. Since I have simple show/hide behavior with "hide" on initialization I was just using the hide state in OnBindViewHolder and the redraw did the trick.

How to use the OnClick attribute in XML for a button inside a ListView item?

I have a listView that uses a customAdapter and a has a custom layout. the listView Item contains an Image, Text and Button. I added an onClick attribute in the Xml android:onClick="onClicked". Then I made the method to respond to the Button
public void onClicked(View view){
......
}
but I don't know how to make it make a different code depending on the button position in the listView, if you know how to solve this problem please tell me, thanks
You can't do that, in your case you have to programatically set the onClickListener on your adapter code
You can create a custom adapter for your listview. You can program your button for click within that custom adapter ..

How to do Button click and list view item click in customize list view in android

I have 4 items in my list view in these 3 are text view and one is button in my case i have to open 2 new activities from a single list
1st from on button click
2nd from on list view item click
but when i add the button in list view list click is not working, also i am not able to handle the click of button.
I am using BaseAdapter class to set the data in list view.
Please help me to solve this.
Thanks.
Or you can just set to you button
android:focusable="false"
In this case you ListView will fire onItemClick action to listener, and the Button will also work when it clicked.
The previous answers to this didn't worked for me.
Add to your row's root layot android:descendantFocusability="blocksDescendants", this really does the trick, the buttons keep working and the list keeps firing the event.

Android: scroll to the bottom of the view

I have a ListView containing some custom Views, and at the end of my list, a FooterView containing a Button.
When I click the button, it adds a view at the end of my ListView. But the cursor of the scrollbar stays at its position.
Now, what I want is to auto-scroll to the end of the list (so the user can see the new item).
Do you have any idea on how I can do that?
i think you will find you happyness here
ListView has a method just for this: smoothScrollToPosition
Once you've added the new item, just calculate the length of the list and pass that (remembering zero-indexing) to the above method
Try this one.. it will solve your problem, i tried it and it works great.
listView.post(new Runnable(){
public void run() {
listView.setSelection(listView.getCount() - 1);
}});

Clicking List-view row and child Button as well

My ListView's row has one Button. Now i want to set click event to both row and button. But as i know ListView loose its onItemClick property if we set it's child click event. So please guide me a way to doing both at once
If you are using Buttons inside each list item, then set the click listener for the buttons on not on the list item.
Button.setOnClickListener(View.OnClickListener)
The list item clicks should go ignored and the buttons click listeners should do what you want.
You have to use ListView.setOnItemClickListener(OnItemClickListener). See the tutorial.
In OnItemClickListener.onItemClick() you're provided with the position of the item.
I don't understand why you don't want to use ListView.setOnItemClickListener(OnItemClickListener) ?
Because it react instantly to a touch event. Isn't what you want?

Categories

Resources