How can I put a ListView into a ScrollView without it collapsing?

后端 未结 27 3223
轮回少年
轮回少年 2020-11-21 05:24

I\'ve searched around for solutions to this problem, and the only answer I can find seems to be \"don\'t put a ListView into a ScrollView\". I have yet to see any real expl

相关标签:
27条回答
  • 2020-11-21 05:48

    Although the suggested setListViewHeightBasedOnChildren() methods work in most of the cases, in some cases, specially with a lot of items, I noticed that the last elements are not displayed. So I decided to mimic a simple version of the ListView behavior in order to reuse any Adapter code, here it's the ListView alternative:

    import android.content.Context;
    import android.database.DataSetObserver;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.View;
    import android.widget.LinearLayout;
    import android.widget.ListAdapter;
    
    public class StretchedListView extends LinearLayout {
    
    private final DataSetObserver dataSetObserver;
    private ListAdapter adapter;
    private OnItemClickListener onItemClickListener;
    
    public StretchedListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(LinearLayout.VERTICAL);
        this.dataSetObserver = new DataSetObserver() {
            @Override
            public void onChanged() {
                syncDataFromAdapter();
                super.onChanged();
            }
    
            @Override
            public void onInvalidated() {
                syncDataFromAdapter();
                super.onInvalidated();
            }
        };
    }
    
    public void setAdapter(ListAdapter adapter) {
        ensureDataSetObserverIsUnregistered();
    
        this.adapter = adapter;
        if (this.adapter != null) {
            this.adapter.registerDataSetObserver(dataSetObserver);
        }
        syncDataFromAdapter();
    }
    
    protected void ensureDataSetObserverIsUnregistered() {
        if (this.adapter != null) {
            this.adapter.unregisterDataSetObserver(dataSetObserver);
        }
    }
    
    public Object getItemAtPosition(int position) {
        return adapter != null ? adapter.getItem(position) : null;
    }
    
    public void setSelection(int i) {
        getChildAt(i).setSelected(true);
    }
    
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    
    public ListAdapter getAdapter() {
        return adapter;
    }
    
    public int getCount() {
        return adapter != null ? adapter.getCount() : 0;
    }
    
    private void syncDataFromAdapter() {
        removeAllViews();
        if (adapter != null) {
            int count = adapter.getCount();
            for (int i = 0; i < count; i++) {
                View view = adapter.getView(i, null, this);
                boolean enabled = adapter.isEnabled(i);
                if (enabled) {
                    final int position = i;
                    final long id = adapter.getItemId(position);
                    view.setOnClickListener(new View.OnClickListener() {
    
                        @Override
                        public void onClick(View v) {
                            if (onItemClickListener != null) {
                                onItemClickListener.onItemClick(null, v, position, id);
                            }
                        }
                    });
                }
                addView(view);
    
            }
        }
    }
    }
    
    0 讨论(0)
  • 2020-11-21 05:49

    This is the only thing that worked for me:

    on Lollipop onwards you can use

    yourtListView.setNestedScrollingEnabled(true);
    

    This enable or disable nested scrolling for this view if you need backwards compatibility with older version of the OS you'll have to use the RecyclerView.

    0 讨论(0)
  • 2020-11-21 05:52

    You should not put a ListView in a ScrollView because a ListView already is a ScrollView. So that would be like putting a ScrollView in a ScrollView.

    What are you trying to accomplish?

    0 讨论(0)
  • 2020-11-21 05:54

    This whole problem would just go away if LinearLayout had a setAdapter method, because then when you told someone to use it instead the alternative would be trivial.

    If you actually want a scrolling ListView inside another scrolling view this won't help, but otherwise this will at least give you an idea.

    You need to create a custom adapter to combine all the content you want to scroll over and set the ListView's adapter to that.

    I don't have sample code handy, but if you want something like.

    <ListView/>
    
    (other content)
    
    <ListView/>
    

    Then you need to create an adapter that represents all of that content. The ListView/Adapters are smart enough to handle different types as well, but you need to write the adapter yourself.

    The android UI API just isn't as mature as pretty much everything else out there, so it doesn't have the same niceties as other platforms. Also, when doing something on android you need to be in an android (unix) mindset where you expect that to do anything you're probably going to have to assemble functionality of smaller parts and write a bunch of your own code to get it to work.

    0 讨论(0)
  • 2020-11-21 05:54

    Instead of putting the listview inside Scrollview, put the rest of the content between listview and the opening of the Scrollview as a separate view and set that view as the header of the listview. So you will finally end up only list view taking charge of Scroll.

    0 讨论(0)
  • 2020-11-21 05:59

    This will definitely work............
    You have to just replace your <ScrollView ></ScrollView> in layout XML file with this Custom ScrollView like <com.tmd.utils.VerticalScrollview > </com.tmd.utils.VerticalScrollview >

    package com.tmd.utils;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.widget.ScrollView;
    
    public class VerticalScrollview extends ScrollView{
    
        public VerticalScrollview(Context context) {
            super(context);
        }
    
         public VerticalScrollview(Context context, AttributeSet attrs) {
                super(context, attrs);
            }
    
            public VerticalScrollview(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
            }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            final int action = ev.getAction();
            switch (action)
            {
                case MotionEvent.ACTION_DOWN:
                        Log.i("VerticalScrollview", "onInterceptTouchEvent: DOWN super false" );
                        super.onTouchEvent(ev);
                        break;
    
                case MotionEvent.ACTION_MOVE:
                        return false; // redirect MotionEvents to ourself
    
                case MotionEvent.ACTION_CANCEL:
                        Log.i("VerticalScrollview", "onInterceptTouchEvent: CANCEL super false" );
                        super.onTouchEvent(ev);
                        break;
    
                case MotionEvent.ACTION_UP:
                        Log.i("VerticalScrollview", "onInterceptTouchEvent: UP super false" );
                        return false;
    
                default: Log.i("VerticalScrollview", "onInterceptTouchEvent: " + action ); break;
            }
    
            return false;
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            super.onTouchEvent(ev);
            Log.i("VerticalScrollview", "onTouchEvent. action: " + ev.getAction() );
             return true;
        }
    }
    
    0 讨论(0)
提交回复
热议问题