【问题标题】:android custom adapter dynamic row heightandroid自定义适配器动态行高
【发布时间】:2014-10-15 09:56:14
【问题描述】:

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

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

这是我的行布局:

<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> 

【问题讨论】:

    标签: android listview custom-adapter row-height


    【解决方案1】:

    在您的item layout上,您有android:layout_height="?android:attr/listPreferredItemHeight",这意味着您强制将所有行的特定行高更改为android:layout_height="wrap_content"

    【讨论】:

      【解决方案2】:

      你可以做的就是删除 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;
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-15
        • 1970-01-01
        • 1970-01-01
        • 2012-07-27
        • 2016-01-01
        相关资源
        最近更新 更多