Gridview image issue (Edge image not using CustomImageView) - android

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!

Related

How do I fit an exact number of ListView items on screen?

I would like the rows in a ListView to be sized so that exactly six of them can fit on the screen. For that, I would need to know how much vertical space is available to the ListView (not the whole screen). However, no measuring can be done in onCreate() since no views have been rendered yet.
If I make measurements after rendering, the ListView might be drawn and then resized, which may be distracting. What is the smartest way to establish the necessary row height before rendering the ListView?
in onCreate you can get the height of your screen and divide by 6.
Now in your getView you get the reference of the top layout for each item, suppost you have named it's id to root and i.e it's a LinearLayout.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){ some inflate }
LinearLayout root = (LinearLayout) view.findViewById(R.id.root);
LayoutParams lp = root.getLayoutParams();
lp.height = screenHeight/6;
root.setLayoutParams(lp);
....
return view;
}
Yes, this assumes the ListView is in fullscreen.
If you have other layouts, you will have to get those height into account.
Then your height will be: int heightForEachItem = (screenHeight - otherlayoutsHeightTogether) / 6;
Turns out that the earliest you can measure a ListView is in the onGlobalLayout() callback.
Here is my example.
params = new AbsListView.LayoutParams(-1,-1);
listview.getViewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){
#Override
public void onGlobalLayout(){ //this is called just before rendering
params.height = listview.getHeight()/6; // this is what I was looking for
listview.getViewTreeObserver.removeOnGlobalLayoutListener(this); // this is called very often
}
adapter = new ArrayAdapter<...>(int position, ...){
#Override
public View getView(...){
LinearLayout item = new LinearLayout(context);
item.setLayoutParams(params);
// add text, images etc with getItem(position) and item.addView(View)
return item;
}
}
listview.setAdapter(adapter);

Get the ListView's width in the Array Adapter

I have a ListView which will be either full screen width (phone) or roughly 1/3 of the width if it's a landscape tablet. In the List it will display an imaged on a remote server.
Obviously the image can take some time to download the image, so, in the JSON data, I have an average colour of the image as hex, and the ratio of the image. What I want is, in the adapter, to fill the image with the average colour at the right size. I've got the colour part down, using this code
final ColorDrawable cd = new ColorDrawable(Color.parseColor(blog.getAverageColor()));
Picasso.with(getContext()).load(blog.getFullPath()).placeholder(cd).into(img);
However, I can't get the size to work. My original idea was to call getWidth() on the view, and times it by blog.getRatio(), then set the images height to that, and it's width to the width of the view.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = getLayoutInflater(null).inflate(r, parent, false);
}
if (view == null) {
return null;
}
int width = view.getWidth();
However, width is always 0. How can I get the width of the view? Or is this just not possible to do like this?
You can get width of listView by call parent.getWidth(). parent is a view that your view will be attached to. So your view will have same sizes.
view.getWidth() returns 0 because at this point your view isn't really added to ListView.

Setting ImageView width and height before drawing in a ListView adapter

I have an adapter to a ListView is a list of ImageViews. I am using a stretch to make the image fil the imageview so I can take smaller images and make them larger on the screen, however the ImageView normally just uses wrap_content and this is an issue because the images just show up as their normal width and height. Is there any way I can set the height and width of a view before drawing it because as in this case I do not have control over the view after it has been drawn. Here is my aapter method:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String currentImage = getItem(position);
ScaleType scaleType = ScaleType.FIT_CENTER;
float screenWidth = parent.getResources().getDisplayMetrics().widthPixels;
if (convertView == null) {
convertView = new ImageView(parent.getContext());
}
// WHAT I WOULD LIKE TO BE ABLE TO DO, but this returns null pointer exception
// convertView.getLayoutParams().width = (int) screenWidth;
// convertView.getLayoutParams().height = (int) ((float)(screenWidth/currentImage.getWidth())*currentImage.getHeight());
((ImageView) convertView).setScaleType(scaleType);
((ImageView) convertView).setImageBitmap(MainActivity.cache.getBitmap(currentImage));
return convertView;
}
How about something like someView.setHeight() and someView.setWidth()? Or someView.setLayoutParams()? You could add either of these to the overridden getView() callback and it should take care of your problem.
You could also Create a Custom View and override something like getMeasuredWidthAndState(). (I think that's one of the right methods, but I'm not one hundred percent sure.) You could create a width class variable and a height class variable that all instances of your custom ImageView would use. However, that might be a bit much if you just want to set the layout width and height though.

ViewPager Gallery replacement

I have done enough searching and finally I am asking this question.
I am converting gallery of imageviews into Viewpager backed up by PagerAdapter. I was able to achieve this:-
In onpageselected I am getting the position which I am using to get the red border.
Problem:-
Only the left most imageview is returned in onpageselected(). The rightmost imageview can never come to left and thus cannot be selected. Further on touching an imageview onpageselected() does not get called. It only gets called when you swipe it.
Questions:-
How to centre the selected imageview?
How to get the imageview position on touching it?
Thanks to NathanZ he gave me some direction. Finally I had to set a OnClickListener() on the ImageView not the ViewPager.
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
MyImageView grpView = new MyImageView(ctxt);
final int temp=position;
grpView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
pos=temp;//This is the value what I am interested in
}
});
grpView.setLayoutParams(new LayoutParams((int)(109*density), (int)(109*density)));
myViewPager.addView(grpView);
return grpView;
}

listview scrolling top can't set index 0 of

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

Categories

Resources