android自定义适配器动态行高

android自定义适配器动态行高,android,listview,custom-adapter,row-height,Android,Listview,Custom Adapter,Row Height,我刚刚创建了我的第一个自定义适配器,它工作得非常好。 然后,我创建了一个rowlayout.xml供适配器填充 但是我有一个问题,行不是动态的,所以如果我尝试放入一个比行长的文本,行不会展开,而是将文本切掉。(见下图:) 以下是我的行布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:

我刚刚创建了我的第一个自定义适配器,它工作得非常好。 然后,我创建了一个rowlayout.xml供适配器填充

但是我有一个问题,行不是动态的,所以如果我尝试放入一个比行长的文本,行不会展开,而是将文本切掉。(见下图:)

以下是我的行布局:

<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;
    }
}
公共类MyAdapter扩展了ArrayAdapter{
私人最终语境;
私人最终名单员额;
公共MyAdapter(上下文、列表帖子){
super(上下文、R.layout.rowlayout、posts);
this.context=上下文;
这个.posts=posts;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
视图行视图=充气机。充气(R.layout.rowlayout,父级,false);
TextView firstText=(TextView)rowView.findViewById(R.id.firstLine);
TextView secondText=(TextView)rowView.findViewById(R.id.secondLine);
//插入柱子
setText(posts.get(position.getMessage());
//插入时间
secondText.setText(posts.get(position.getDate());
返回行视图;
}
}
有人知道如何解决这个问题吗

谢谢

更新(解决方案):

好的,所以我在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> 

您只需删除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;
  }
}
公共类MyAdapter扩展了ArrayAdapter{
私人最终语境;
私人最终名单员额;
公共MyAdapter(上下文、列表帖子){
super(上下文、R.layout.rowlayout、posts);
this.context=上下文;
这个.posts=posts;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
如果(文本长度>最大长度){
视图行视图=充气机。充气(R.layout.rowlayout1,父级,false);
//....
}否则{
视图行视图=充气机。充气(R.layout.rowlayout,父级,false);
TextView firstText=(TextView)rowView.findViewById(R.id.firstLine);
TextView secondText=(TextView)rowView.findViewById(R.id.secondLine);
//插入柱子
setText(posts.get(position.getMessage());
//插入时间
secondText.setText(posts.get(position.getDate());
}
返回行视图;
}
}

您只需删除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;
  }
}
公共类MyAdapter扩展了ArrayAdapter{
私人最终语境;
私人最终名单员额;
公共MyAdapter(上下文、列表帖子){
super(上下文、R.layout.rowlayout、posts);
this.context=上下文;
这个.posts=posts;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
如果(文本长度>最大长度){
视图行视图=充气机。充气(R.layout.rowlayout1,父级,false);
//....
}否则{
视图行视图=充气机。充气(R.layout.rowlayout,父级,false);
TextView firstText=(TextView)rowView.findViewById(R.id.firstLine);
TextView secondText=(TextView)rowView.findViewById(R.id.secondLine);
//插入柱子
setText(posts.get(position.getMessage());
//插入时间
secondText.setText(posts.get(position.getDate());
}
返回行视图;
}
}

在您的
项目布局上
您有
android:layout\u height=“?android:attr/listpresprereditemheight”
意味着您将强制所有行的特定行高更改为
android:layou height=“wrap\u content”
项目布局上
您有
android:layout\u height=“?android:attr/listPreferredItemHeight”
表示您正在强制将所有行的特定行高更改为
android:layout\u height=“wrap\u content”