Android - OnItemClickListener listview - android

Here's what I want to do:
I have this list of tasks. When I click on one of the items I want this to appear:
Assuming that menubar can be found with
findViewById(R.id.menubar);
Can someone please tell what code I have to put onItemClickListener
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
NOTE: The menubar visibility its set as GONE.
Thanks!

Without looking at the code, a precise answer is not possible. However, when I first wanted to implement a "Quick Action Bar" similar to the one you have shown, I had followed this example here:
http://code.google.com/p/simple-quickactions/
And this with some styling put in: https://github.com/lorensiuswlt/NewQuickAction3D
Hope this helps....

It is a bit difficult without seeing any code, but I can give you some logic ideas.
Based on your description, I'm assuming that those quick bar controls are hidden on EACH listview item so, it goes:
Fotos (visible)
Action Controls (hidden)
[End of Item]
PROJECTOS (visible)
Action Controls (hidden)
...
I think the OnItemClick method gives you the view (item: Fotos, Projectos, etc) that was clicked as the 2nd argument. If you cast that back to whatever layout you used to create the listview items, you should be able to use findViewByID to get access to the hidden controls that are on each listview and make them visible.
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//Assuming each item is a linear layout
LinearLayout itemAsLL = (LinearLayout)view;
//find the action bar controls
LinearLayout actionControlsLayout = itemAsLL.findViewById(R.name.of.actioncontrols.id);
//Make it visible
actionControlsLayout.setVisibility(VIEW.Visible);
}
You would need to find a way to hide those controls when another item is clicked? Maybe save the view (or position in the adapterView ) as a class variable and when another item is clicked, go to that view/find that view and hide the controls.

I finally found out the answer. Apparently, when you fill a view with lots of tasks, the adapter does not fill everything when you load the layout, so everytime you scroll down, hes putting more items on the listview.
What you have to do is make sure you put a field "loaded" and, in case is false, it does not show the menubar below that item, otherwise, when you click in one of the items of that list, when you scroll down, since they will have the same relative position (if you click on the first item, the position = 0, when you scroll down, there will be another position = 0) the menubar will appear on both items.
Hope it helps ;)

Related

How do I change a list view item dynamically?

I basically want something similar to the Twitter or Pocket app where you long click on an item in the list View and it gives you a set of options to perform on that item by changing the layout of that item, to reveal a set of buttons.
ListView lv= getListView();
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> av, View v,
int pos, long id) {
// TODO Auto-generated method stub
Context ctx = getApplicationContext();
mDbHelper.deleteNotes(id);
fillData();
Toast.makeText(ctx, "Long Clicked at" + id, Toast.LENGTH_SHORT).show();
return false;
}
});
My long click code is here. Right know I just directly delete the item that is long clicked, but I want the user to see 3-4 buttons when an item is long clicked in place of the said item.
Anyone knows how to do it?
EDIT
Here is the twitter image:
What you could do, is when the user long presses, simply within your adapter, hide or show a, for example, LinearLayout containing all the buttons and so on within it.
Make sure this element you are showing fits itself below or above all other usual elements.
Do you get what I'm trying to say? Holla if you need more info.
Update 1
Okay so in reply to your comment, no you won't need to create a separate xml file etc. See this for example,
Your custom row xml with two layouts:
RelativeLayout mainView
RelativeLayout extraView
In your row xml have an extra layout (my preference is relative, it's upto you anyway), and set this layout to be below the main stuff (make sure they both fit on the row). So in this scenario, mainView is the layout with the stuff you always want shown and extraView is the stuff you want only when long pressed.
Make sure the default visibility setting for extraView is set as GONE
Then, simply when long pressed, that specific rows extraView.setVisibility(View.VISIBLE);
And obviously vice versa when no longer long pressed.
Hit me up if you're confused, and apologies for the non formatted reply as I'm currently commuting and replying via my mobile, will format the answer when at a pc.

How to animate item in ListView when being clicked?

I want to have one ListView that when I click on the item the view slide out to the left.
So I have:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
arg1.startAnimation(openAnimation);
}
});
However, animation applied to the different item in the list instead of the one being clicked on. The behavior seems to be random as sometime it happened to more than one item at the same time.
As I suspected this is because of the way Adapter reuse it's view to create item. I went to modify the getView method in my Adapter to inflate new view every time it's being called. Then the animation doesn't happen any more.
Is there a way to resolve this? I tried to move the animation to inside my Adapter but then I can not associate it with other action on the Listview.
Ultimately, I want the item to be clickable but when swipe left/right reveal delete button (iOS delete behavior). Am I on the wrong track here? This should be possible though as Android can implement swipe to remove in the Notification bar.
I recommend that you check this thread also.
I don't think, that this is possible without having to modify your adapter to suit this type of behavior. For what I understand, you don't have any problems with implementing the code recognizing the swipe gestures on different ListView-rows, only with the animation the should follow this gesture on according row(s).
I'd rewrite the adapter to suit at least 2 row types: normal rows, and rows to be deleted. In the "getView()" method of your adapter, you should only reuse the convertView of normal Views. Rows that are to be deleted should not reuse them, so that animating one would not modify the others.
Upon clicking a normal row, you should first tell the adapter that the row on the clicked position is now of type to-be-deleted, call .notifyDatasetChanged(), and then start the animation on that row.

Change list background problem

I have a ListView and on clicking a list item, it starts another activity.
So my problem is that, when I click on list item, it should change its background and move to another activity and on pressing back on this activity the list item should retain it changed background color. And again clicking on another list, it should remove the earlier background color and again do the same.
Thanks in advance.
You'll want the setItemChecked() method in the ListView. This should make the item you click "permanently on", if you place it in the onItemClick() method in a listener. Note that when you navigate to the next activity, you will have to use onSaveInstanceState() and onRestoreInstanceState() to store which item in the ListView has been clicked; I don't believe Android will retain which one has been clicked when you navigate away from the Activity with the ListView.
There is quite a good tutorial of this in the Fragments section of the Developers website, here.
Hope this helps.
Keep track of the selected item. Use a custom adapter and override the getView method. In this method check if the position of the view is the selected item and change the background of the view accordingly.
Override the item click listener and do the following to change the color of the clicked view:
listView1.setOnItemClickListener(new ListView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3)
{
v.setBackgroundColor(Color.BLACK);
}
});
I solve it by setItemChecked() methood and use android:choiceMode="singleChoice" inside ListView in my layout xml file. I used MyClass.this.getListView().setItemChecked(position,true); on clicking the Item in the list and the on getView() i am doing this...
if(position == MyClass.this.getListView().getCheckedItemPosition()){
holder._newsLinear.setBackgroundColor(Color.CYAN);
}
else{
holder._newsLinear.setBackgroundColor(Color.BLACK);
}

Listview changes image on number of lines when i'm trying to update one line

I have a list of items, which i'd like to "check" with filled star when user clicks on one item in the list.
I have a ListView with text & image, represented in XML layout, and using simple StringAdapter.
I've implemented the above by doing this:
this.listViewSub.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(final AdapterView parent, final View view, final int position,
final long id) {
ImageView img = (ImageView) view.findViewById(R.id.unchkImg);
img.setImageResource(R.drawable.starchk);
I guess its not the right way to do it, but i don't have any idea how else to code it.
This code for some reason changes few random items in the list, and after one click i have about 4-5 items with icon changed.
Any idea how to solve it correctly?
Thanks
It's hard to tell without code, but my first guess is that as the views get recycled, if you don't reset the ImageView to use the default "unchecked" image in the getView method, each time you star an item the view will remain starred, even it is used for a different item. So your adapter should just reset the ImageView's image resource ; though in that case, you'll have to remember which items you starred in order to reset the ImageView correctly : set it to do the default drawable or to the starred one.
At last, after long search - i've found the answer in an article:
http://www.codegod.biz/WebAppCodeGod/Android-ListView-with-dynamic-Images-AID588.aspx
That was a tough one.

Minimum scroll amount when programmatically selecting ListItem in android ListView

refering to this question: how does scrolling in android listview work?
on change from state 2 to 3 the clicked listitem is increased in its size to show more information. if the clicked item was the last one on screen, the extended listitem is not fully visible because the new content flows out at the bottom of the screen.
my current solution is to call "setSelection(index)", if the last visible listitem was clicked. this results in a state were the selection is brought to the top of the screen. this is annoying because the listitem moves from bottom to top.
is there a way to avoid this? this means, is there something to let the listview know, that is should scroll only the minimum amount of pixels, so that the clickeditem is fully visible?
or have i to programm this functionality on my own?
Add a custom OnItemClickListener to the ListView. Implement OnItemClickListener.onItemClick like this:
public void onItemClick(final AdapterView<?> parent, final View view,
int position, long id) {
View hiddenContent = view.findViewById(R.id.hiddenContent);
hiddenContent.setVisibility(View.VISIBLE);
// At this point the layout hasn't be redone and you don't have reliable
// measurements on the view. It would be nice to do something after the view
// has gone through another layout
view.post(new Runnable() {
public void run() {
Rect r = new Rect();
view.getDrawingRect(r);
parent.requestChildRectangleOnScreen(view, r, false);
}
});
}
Whenever you click on a list item to show the hidden content the ListView control will go ahead and do whatever scrolling is needed (or no scrolling if none is needed) to completely show the row.

Categories

Resources