Set ListView item height

后端 未结 4 749
闹比i
闹比i 2020-12-13 14:53

this should be a very easy thing to do, but it looks like it\'s very tricky. In my code I have this ListView:



        
相关标签:
4条回答
  • 2020-12-13 15:32
    convertView = inflater.inflate(R.layout.custom_row, parent, false);
    
    if (CONDITION) {
        holder.wholeLayout.getLayoutParams().height = 1; // visibility Gone not working && 0 height crash app.
    } else {
        holder.wholeLayout.getLayoutParams().height = RelativeLayout.LayoutParams.WRAP_CONTENT;
    }
    
    0 讨论(0)
  • 2020-12-13 15:39
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:minHeight="60dp"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="center"
        android:weightSum="1"
        android:background="@color/main_color">
    
        <ImageView
            android:id="@+id/img_left_menu"
            android:layout_weight="0.4"
            android:focusable="false"
            android:maxHeight="50dp"
            android:maxWidth="50dp"
            android:minHeight="50dp"
            android:minWidth="50dp"
            android:layout_width="0dp"
            android:scaleType="centerInside"
            android:adjustViewBounds="true"
            android:layout_height="wrap_content"/>
    
        <TextView
            android:id="@+id/tx_left_menu"
            android:layout_width="0dp"
            android:textSize="18dp"
            android:layout_gravity="center_vertical"
            android:layout_weight="0.6"
            android:singleLine="true"
            android:layout_height="wrap_content"
            android:focusable="false"
            />
    </LinearLayout>
    

    in the adapter set the parent as described above

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      ...
      View v = inflater.inflate(R.layout.filtered_trip_row, parent,false);
      …
    
      return v;
    }
    
    0 讨论(0)
  • 2020-12-13 15:40
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      ...
      View v = inflater.inflate(R.layout.filtered_trip_row, parent,false);
      …
    
      return v;
    }
    

    just pass boolean value to the method so that it add the yourrowlayout to the listview and it will not give error futher like error java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

    and add yourLayout which u wants to populate through Adapter

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:cdpb="http://schemas.android.com/apk/res/ale.android.brainfitness"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="55dp">
    ...
    </LinearLayout>
    

    it works for me and i think it may help you

    0 讨论(0)
  • 2020-12-13 15:43

    How are you inflating the view?

    If you are setting 'parent' parameter to null like below, then layout parameters are ignored.

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      ...
      View v = inflater.inflate(R.layout.filtered_trip_row, null);
      …
    
      return v;
    }
    

    Passing 'parent' to inflate should work as you expect.

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      ...
      View v = inflater.inflate(R.layout.filtered_trip_row, parent);
      …
    
      return v;
    }
    

    This explains it in detail: Why does LayoutInflater ignore the layout_width and layout_height layout parameters I've specified?

    To avoid getting UnsupportedOperationException, you can add false as an input parameter to the inflator. Example:

    inflater.inflate(R.layout.filtered_trip_row, parent, false)
    
    0 讨论(0)
提交回复
热议问题