How to make android listview scrollable?

后端 未结 9 1345
悲&欢浪女
悲&欢浪女 2021-01-03 19:12

I have two listviews, but they don\'t scroll. How do I correct this?

Here is my layout.xml

 

        
相关标签:
9条回答
  • 2021-01-03 19:51

    You shouldn't put a ListView inside a ScrollView because the ListView class implements its own scrolling and it just doesn't receive gestures because they all are handled by the parent ScrollView

    0 讨论(0)
  • 2021-01-03 19:51

    Putting ListView inside a ScrollView is never inspired. But if you want your posted XML-like behavior, there're 3 options to me:

    1. Remove ScrollView: Removing your ScrollView, you may give the ListViews some specific size with respect to the total layout (either specific dp or layout_weight).

    2. Replace ListViews with LinearLayouts: You may add the list-items by iterating through the item-list and add each item-view to the respective LinearLayout by inflating the view & setting the respective data (string, image etc.)

    3. If you really need to put your ListViews inside the ScrollView, you must make your ListViews non-scrollable (Which is practically the same as the solution 2 above, but with ListView codes), otherwise the layout won't function as you expect.
      To make a ListView non-scrollable, you may read this SO post, where the precise solution to me is like the one below:

    listView.setOnTouchListener(new OnTouchListener() {
      public boolean onTouch(View v, MotionEvent event) {
        return (event.getAction() == MotionEvent.ACTION_MOVE);
      }
    });

    0 讨论(0)
  • 2021-01-03 19:57

    By default ListView is scrollable. Do not put ScrollView to the ListView

    0 讨论(0)
  • 2021-01-03 19:57

    Practically its not good to do. But if you want to do like this, just make listview's height fixed to wrap_content.

    android:layout_height="wrap_content"
    
    0 讨论(0)
  • 2021-01-03 19:57

    Listview so have inbuild scrolling capabilities. So you can not use listview inside scrollview. Encapsulate it in any other layout like LinearLayout or RelativeLayout.

    0 讨论(0)
  • 2021-01-03 19:59

    Never put ListView in ScrollView. ListView itself is scrollable.

    0 讨论(0)
提交回复
热议问题