Android Listview show only one item

后端 未结 5 1377
暗喜
暗喜 2020-12-09 01:40

I have a ListView, where i changed appearence of row, but listview have size of one row, instead of fullscreen.

list_item.xml:



        
5条回答
  •  臣服心动
    2020-12-09 02:20

    If you use ListView inside ScrollView, you have this issue. And my solution is following as:

    1. Create a custom listview which is non scrollable

    public class NonScrollListView extends ListView {
    
    public NonScrollListView(Context context) {
        super(context);
    }
    public NonScrollListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();    
    }
    

    }

    2. Use above custom class for xml file

      
        
    

    It worked well on all OS-version for me. Hope best for you.

提交回复
热议问题