android自定义适配器动态行高

时间:2014-10-15 09:56:14

标签: android listview custom-adapter row-height

我刚刚创建了我的第一个自定义适配器,效果很好。 然后,我为适配器创建了一个rowlayout.xml来填充。

但我有一个问题,该行不是动态的,所以如果我尝试放入比行更长的文本,行不会扩展,而是关闭文本。 (见图:http://tinyurl.com/paxdt3a

这是我的rowLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >

    <!--The second line is used for the date of the post-->
    <TextView
        android:id="@+id/secondLine"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="Time and date"
        android:textSize="12sp" />

    <!--The first line is used to the post-->
    <TextView
        android:id="@+id/firstLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/secondLine"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:gravity="center_vertical"
        android:text="Post"
        android:textSize="16sp" />

</RelativeLayout> 

这是我的适配器:

public class MyAdapter extends ArrayAdapter<UserPost> {
    private final Context context;
    private final List<UserPost> posts;

    public MyAdapter(Context context, List<UserPost> posts) {
        super(context, R.layout.rowlayout, posts);
        this.context = context;
        this.posts = posts;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.rowlayout, parent, false);

        TextView firstText = (TextView) rowView.findViewById(R.id.firstLine);
        TextView secondText = (TextView) rowView.findViewById(R.id.secondLine);

        //Insert the post
        firstText.setText(posts.get(position).getMessage());
        //Insert the time
        secondText.setText(posts.get(position).getDate());

        return rowView;
    }
}

有没有人知道如何解决这个问题?

谢谢!

更新(解决方案):

好的,所以我在rowlayout.xml中做了一些更改,最后我想出了这个,这有效!

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="6dip" >

    <!--The first line is used to the post-->
    <TextView
        android:id="@+id/firstLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:gravity="center_vertical"
        android:text="Post"
        android:textSize="16sp" />

    <!--The second line is used for the date of the post-->
    <TextView
        android:id="@+id/secondLine"
        android:layout_below="@+id/firstLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:ellipsize="marquee"
        android:singleLine="false"
        android:text="Time and date"
        android:textSize="12sp" />

</RelativeLayout> 

2 个答案:

答案 0 :(得分:4)

在您的item layoutandroid:layout_height="?android:attr/listPreferredItemHeight"表示您强制所有行的特定行高更改为android:layout_height="wrap_content"

答案 1 :(得分:0)

你可以做的就是删除singleLine属性

 android:singleLine="false"

或者你可以这样做

public class MyAdapter extends ArrayAdapter<UserPost> {
private final Context context;
private final List<UserPost> posts;

public MyAdapter(Context context, List<UserPost> posts) {
    super(context, R.layout.rowlayout, posts);
    this.context = context;
    this.posts = posts;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
 LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(text.length>MAX_LENGTH){
View rowView = inflater.inflate(R.layout.rowlayout1, parent, false);
//....
}else{


    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);

    TextView firstText = (TextView) rowView.findViewById(R.id.firstLine);
    TextView secondText = (TextView) rowView.findViewById(R.id.secondLine);

    //Insert the post
    firstText.setText(posts.get(position).getMessage());
    //Insert the time
    secondText.setText(posts.get(position).getDate());
   }
    return rowView;
  }
}