when scroll to and I click item list to set the fist index it is not set .what ?
public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) {
// TODO Auto-generated method stub
//ImageView imgNew = (ImageView) arg1.findViewById(R.id.imgIcon);
//imgNew.setImageResource(R.drawable.push);
ImageView imgOld = (ImageView) listView1.getChildAt(0).findViewById(R.id.imgIcon);
imgOld.setImageResource(R.drawable.push);
}
If I'm not mistaken, you're trying to change an image once it's been clicked. Try doing something like this:
ImageView imgOld = (ImageView) listView1.getChildAt(index).findViewById(R.id.imgIcon);
If you have free time and want to save your time (in the future), watch Google I/O 2010 - The world of ListView (about 1 hour).
When I started to code in Android, I spent lots of time on this stackoverflow with ListView. Then someone pointed me to that video, and it was be easier :-)
I don't understand the issue but just in case
getChildAt(0)
0 index means first visible item on the screen
Related
I have an odd issue. Basically I use a custom image view which I overwrite the draw method. The draw method just writes the tag of the image object.
I have a grid of all these custom image views. The issue stems from the edge image that it renders. Basically the image does not display any text (just the image) but when I scroll out and back again the text reappears for the image. I am just asking whether there is any way to actually render the text of the edge image and not have to scroll off screen and back.
Here is the getView code for my image adaptor:
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
CustomImageView tmp;
if (arg1 == null) {
tmp = new CustomImageView("", cx, true);
} else {
tmp = (CustomImageView) arg1;
}
int Dimens = (int) getResources().getDimension(R.dimen.gridParam);
tmp.setPadding(3, 3, 3, 3);
tmp.setLayoutParams(new GridView.LayoutParams(Dimens, Dimens));
tmp.setTag("TESTIMG" + arg0);
tmp.setImageResource(mThumbIds[arg0]);
return tmp;
}
}
And here is the code for my gridview, its just a standard creation.
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdaptor(this));
Hopefully you can help me out. Just curious as to why this issue is occuring, maybe I left out something.
Any help would be appreciated. Thanks!
I seen SimpleExpandableListAdapter example when i clicked expanded group item at moving top of the screen. i created NewAdapter which extends
BaseExpandableListAdapter. I want to do same thing but dont know how to do. i searched lot of things which is not worked for me. Please let me know how to do.
Thank you in Advance.
This one is working for me
expandList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if (!parent.isGroupExpanded(groupPosition)) {
parent.expandGroup(groupPosition);
} else {
parent.collapseGroup(groupPosition);
}
parent.setSelectedGroup(groupPosition);
return true;
}
});
As the main working part for scroll is
parent.setSelectedGroup(groupPosition);
may this solve your problem .
I think using duration gives a better user experience. So you can use this, with duration added. Will scroll the item in position smoothly to the top of the listview.
int duration = 500; //miliseconds
int offset = 0; //fromListTop
listview.smoothScrollToPositionFromTop(position,offset,duration);
descrease duration to make scrolling faster
What you are looking for is,
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)
{
myListview.setSelectionFromTop(position, 5);
}
This will position your selected list item as the first visible item on screen. However, it does so without any smooth scroll animation, the moment you tap the item, it becomes the first item visible.
If you want the scroll animation, you could use,
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3)
{
int offset = position - myListview.getFirstVisiblePosition();
if(myListview.getFirstVisiblePosition() > 0)
offset -= 1;
myListview.smoothScrollByOffset(offset);
}
Note that smoothScrollByOffset is available from API-Level 11 onward.
However, both these methods will not work if you select an item close to the bottom of the list, as the list will not scroll further upwards if the last list item is visible. To overcome this, you could convert your listview into a circular listview, as described here.
I've come with a problem. I need to highlight the selected item within a Gallery. I've tried changing the appearance of the selected view through this method:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
The second parameter of that view is the current selected view, and I'm trying in this case, increase the size of the text. However this doesn't work, not even if I call invalidate in the selected item or in the entire Gallery.
This is the code I use to change the textview text size
TextView textview = (TextView)view;
textview.setTextSize(50, TypedValue.COMPLEX_UNIT_SP);
textview.invalidate();
Do you know how to do this? Thanks
Your implementation works, but it does not return the text size to normal once the item becomes unselected - the text stays larger size.
This should fix it:
private View lastview;
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if (lastview != null) lastview.setBackgroundColor(0xFFFFFFFF);
lastview = arg1;
arg1.setBackgroundColor(0xFFFF0000);
}
You can set text size instead of color, or do whatever you want to the style of it.
Try StateListDrawable - looks apt for your situation.
UPDATE: You have reversed the parameters in setTextSize. It should be:
textview.setTextSize(TypedValue.COMPLEX_UNIT_SP, 50);
I'd appreciate it if someone could help me with my problem.
I am trying to change the image in an ImageView when someone clicks on it. I've put my images in an array and I am using a while loop to cycle once through all of them.
My problem is that while the first image (image8, not in the array) shows in the view all the other (after creating the OnClickListener) do not. Actually nothing happens and I am not sure where the mistake is. Thanks in advance.
This is the problematic code:
final int array[]=new int[5];
array[0]= R.drawable.image6;
array[1]= R.drawable.image4;
array[2]= R.drawable.image9;
array[3]= R.drawable.image4;
array[4]= R.drawable.image5;
ImageView touchView = (ImageView)findViewById(R.id.imageview);
touchView.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View touchView, MotionEvent ev) {
//get coordinates of touch event
int x = (int)ev.getRawX();
int y = (int)ev.getRawY();
---Code missing---
((ImageView)touchView).setImageResource(R.drawable.image8);
touchView.setOnClickListener(new View.OnClickListener() {
int counter = 0;
#Override
//Image change on every click
public void onClick(View touchView) {
while(counter<5){
((ImageView) touchView).setImageResource(array[counter]);
counter++;
});
You can't change images in a sequence like that and expect anything to show on the screen. You should use the click to start a separate thread that will do the image animation. See the description of the android.view.animation package. It sounds like the AnimationDrawable class will give you exactly what you want.
There is a special view for your case. ImageSwitcher is what you need. There is an example from android developers on how to use it. It should be trivial to adapt the example to your needs.
When I use the Gallery widget, how do I get the images to say scale up & glow on being selected and scaled down & un-glow on being unselected?
All tutorials I've seen have this effect but I'm not able to see it...
Is there some kind of an animation that I have to attach to the Gallery?
Hope this is helpful. I manage to "simulate" the shrink/grow solution with the Gallery widget. Since they removed the getScale(), things get a little bit complicated. I think that this it's not the best solution at all, but at least I can live with it.
What I have found is that Gallery manages the focus EXTREMELY BAD. So, the first approach was to add a focus change listener on the ImageView displayed, but no luck there. Focus is a MESS there... in terms that, the selected image it's not the currently focused view. I have sent a mail to android-developers mailing list about some error on API doc (regarding the focusSearch()method and some focus contants).
Here's my solution to this problem:
Build an animation resource to 'grow' the image:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0"
android:toXScale="1.10"
android:fromYScale="1.0"
android:toYScale="1.10"
android:duration="300"
android:pivotX="50%"
android:pivotY="50%"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fillAfter="false"/>
If you don't get what that means then you should proceed to read this
That will be our 'grow' effect, and you will need to save it in: res/anim/grow.xml or whatever name it suites you (but always in res/anim dir).
You can follow the resource guide from here to create a Gallery view. The ImageAdapter builds an ImageView every time that the Gallery object calls getView(). One workaround you could implement is adding a line to the getView() method that identifies a View with a position, this way:
...
i.setId(position);
...
With that line added to the getView() method of the ImageAdpater object, you can then unequivocally identify that view within a listener, for instance:
g.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected (AdapterView<?> parent, View v, int position, long id) {
Animation grow = AnimationUtils.loadAnimation(YourActivity.this, R.anim.grow);
View sideView = parent.findViewById(position - 1);
if (sideView != null)
((ImageView)sideView).setLayoutParams(new Gallery.LayoutParams(150, 100));
sideView = parent.findViewById(position + 1);
if (sideView != null)
((ImageView)sideView).setLayoutParams(new Gallery.LayoutParams(150, 100));
v.startAnimation(grow);
v.setLayoutParams(new Gallery.LayoutParams(170, 150));
}
public void onNothingSelected (AdapterView<?> parent) {
System.out.println("NOTHING SELECTED");
}
});
NOTE: You may notice that all the values from animation and from layout parameters has been choosen by me at hand. This is because i'm not going to clean code for you. And, this is just a workaround the BAD focus issue with this widget or the android view system. If focus were OK, then, all you need to do is set a focus change listener that makes the gros/shrink when it got focus/unfocused.
I hope this may help you to find a way around for your problem,
Regards,
New EDIT: This is the listener I have set, I also added the line i.clearAnimation() in the getView() method:
private class SelectListener implements AdapterView.OnItemSelectedListener {
private Animation grow;
private int last;
public SelectListener() {
grow = AnimationUtils.loadAnimation(RouteGallery.this, R.anim.grow);
last = 0;
}
public void onItemSelected (AdapterView<?> parent, View v, int position, long id) {
View sideView = parent.findViewById(last);
if (sideView != null && last != position)
sideView.clearAnimation();
v.startAnimation(grow);
last = position;
}
public void onNothingSelected (AdapterView<?> parent) {
}
}
You need to use an ImageSwitcher. The ImageSwitcher has methods for setting the in and out animations (when image is selected and deselected, or selected and replaced).
The following link has a good tutorial on how to use it in conjunction with the Gallery.
I implemented a similar animation like this:
final Animation shrink = AnimationUtils.loadAnimation(activity, R.anim.shrink);
shrink.setFillAfter(true);
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// This iterates through the views which are currently displayed on screen.
for (int k=0; k<gallery.getChildCount(); k++){
// Do whatever else you want, here.
// This is how you get a particular element of a view
ImageView background = (ImageView) gallery.getChildAt(k).findViewById(R.id.menu_item_background);
//clear animation
gallery.getChildAt(k).clearAnimation();
}
// Scale the selected one
view.startAnimation(shrink);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {}
});