0

I'm making an app that uses a listview that has section headers and content. For each header I want to use an image but the view is setting it's height to the image's and not the value that I set the height at. This is in my adapter:

@Override public View getView(int position, View convertView, ViewGroup parent) {
        TextView view = (TextView) super.getView(position, convertView, parent);
        view.setTextColor(Color.DKGRAY);
        Item item = getItem(position);
        if (item.type == Item.SECTION) {
            if (position == 0) {
                view.setBackgroundResource(R.drawable.myImage);
                view.setHeight(400);
                view.setMaxHeight(400);
            }
            else {
                //deal with other views
            }
        }
        return view;
    }

I'm testing this by just setting the first item in the list as a photo, but as I said, the view isn't using 400px as the height. If I set the height the same way in the else block and just set the background as a color it works fine. I should note that I want to avoid just scaling the picture manually to fit because eventually I want the view to just show a frame of the full image and add a scrolling effect.

Ryan Sayles
  • 3,389
  • 11
  • 56
  • 79

1 Answers1

0

I'm not quite sure what you are asking. Do you want it only to only show 400px if there is an image and smaller if there is not? With the code you have does the image show?

I immediately want to rewrite your code like this:

@Override 
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = new TextView(mContext); // where mContext = context; appears in the adapter constructor
    convertView.setTextColor(Color.DKGRAY);
    Item item = getItem(position);
    if (item.type == Item.SECTION) {
        if (position == 0) {
            convertView.setBackgroundResource(R.drawable.myImage);
            convertView.setHeight(400);
            convertView.setMaxHeight(400);
            //need something like convertView.setText((String) item); to get text to show
        }
        else {
            //deal with other views
        }
    }
    return convertView;
}

I'd recommend using xml to define the layout for the row. Makes it far easier to customise the look of the row.

TTransmit
  • 3,270
  • 2
  • 28
  • 43
  • Yeah so what happens is even when I set the height to say 400px, the view actually uses the images height which is like 1000px – Ryan Sayles Jan 22 '15 at 18:05
  • And you want to cut off and use the top 400px rather than scale it down to 400px? – TTransmit Jan 22 '15 at 18:07
  • Right, either the top or the bottom – Ryan Sayles Jan 22 '15 at 18:09
  • You can either define the background image in xml like this: http://stackoverflow.com/a/8501398/2832027 OR you can use a layout for your row with RelativeLayout with an ImageView first: http://stackoverflow.com/a/8242775/2832027 – TTransmit Jan 22 '15 at 18:41
  • So I used the xml file referenced in the first link and all that did was make it so the image wasn't stretched, it still kept it above 400px. Also it appears that I can't use a convertView and cast it with a (TextView). I'm getting an error that some of the methods, like setTextColor do not exist. – Ryan Sayles Jan 22 '15 at 20:25
  • Sorry, I thought that would be a quick fix. It seems that this stretching to the background image is a feature of android views. I guess you need to use an ImageView in a RelativeLayout. – TTransmit Jan 22 '15 at 20:48