根据ListAdapter中的对象调整ListView的大小

时间:2015-03-14 18:23:01

标签: android listview android-listview

我有一个listview,其中包含不同帖子的内容。我目前每页加载的帖子限制为25.每个帖子的大小不同。我希望ListView大小达到这25个对象,即它应该容纳来自listadapter的所有对象。 我目前将列表视图大小设置如下。

<ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="5000sp" >
</ListView>

我的ListAdapter设置为:

ListAdapter adapter = new SimpleAdapter(NewsFeed.this, contactList,
                R.layout.list_item, new String[] { TAG_MESSAGE,
                        TAG_CREATETIME }, new int[] { R.id.message,
                        R.id.time });
        setListAdapter(adapter);

list_item.xml

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/time"
        android:gravity="right"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="#b1b1b1"
        android:background="#abc0e3" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/cover" />

    <TextView
        android:id="@+id/message"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:text="TextView"
        android:linksClickable="true"
android:autoLink="web" />

    </LinearLayout>

2 个答案:

答案 0 :(得分:1)

看看这是否适合您,在哪里调整宽度,然后您可以根据文本长度决定使用的宽度,

/* create an inline class that defines each item in the list
  private class Post 
  {
    String time;
    String message;
    Bitmap bmp;
    public String getTime(){return time;}
    public String getMessage(){return message;}
    public Bitmap getBitmap(){return bmp;}
  } 

  //** populate your array with each item in list 
  ArrayList<Post> array = new ArrayList<Post>();
  Post p = new Post();
  //* ..............

  ListAdapter  adapter = new ListAdapter (this, array);
  listView.setAdapter(adapter);

  private class ListAdapter  extends BaseAdapter 
  {
    Context context;
    ArrayList<Post> contactList;
    public ListAdapter(Context context, ArrayList<Post> contactList) 
    {
      this.context = context;
      this.contactList = contactList; 
    }

    public View getView(int position, View convertView, ViewGroup parent) 
    {
      try
      {
          if(convertView==null)
          {
            LayoutInflater inflater = getLayoutInflater();
            convertView = inflater.inflate(R.layout.list_item, parent, false);
          }  
          ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
          TextView timeview = (TextView) convertView.findViewById(R.id.time);
          TextView msgview = (TextView) convertView.findViewById(R.id.message);

          Post  post = getItem(position);
          imageView.setImageBitmap(post.getBitmap());
          timeView.setText(post.getTime());
          messageView.setText(post.getMessage());

          int width = convertView.getWidth();
          int height = convertView.getHeight();

          int messageWidth = post.getMessage().length();

          //* adjust the width based on your message width
          //* width = ........ do something here
          //* set the desired width here
          convertView.setLayoutParams(new LayoutParams(width, height));

        return convertView;
      }
      catch(Exception e)
      {
      }
      return null;
    }
    @Override
    public int getCount() 
    {
      return contactList.size();
    }

    @Override
    public Post getItem(int position) 
    {
        return contactList.get(position);
    }

    @Override
    public long getItemId(int position) 
    {
       return 0;
    }
  }

答案 1 :(得分:0)

更改list_item.xml

中的代码

android:layout_width="wrap_content"fill_parentmatch_parent