Android listview not scrolling

前端 未结 7 1386
离开以前
离开以前 2020-12-21 15:21

i have a layout whose starting tag(parent tag is ) is



        
相关标签:
7条回答
  • 2020-12-21 15:59

    You cannot add a ListView in a scroll View, as list view also scrolls and there would be a synchonization problem between listview scroll and scroll view scroll. You can make a CustomList View and add this method into it.

    @Override public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        /*
         * Prevent parent controls from stealing our events once we've gotten a touch down
         */
        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {
            ViewParent p = getParent();
            if (p != null) {
                p.requestDisallowInterceptTouchEvent(true);
            }
        }
        return false;
    }
    
    0 讨论(0)
  • 2020-12-21 16:03

    Try below class, it may help you to work with ListView inside ScrollView

    Utility.java

    import android.view.View;
    import android.view.ViewGroup;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    
    public class Utility {
            public static void setListViewHeightBasedOnChildren(ListView listView) {
                  ListAdapter listAdapter = listView.getAdapter();
                if (listAdapter == null) {
                // pre-condition
                      return;
                }
    
                int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
                for (int i = 0; i < listAdapter.getCount(); i++) {
                     View listItem = listAdapter.getView(i, null, listView);
                     if (listItem instanceof ViewGroup) {
                        listItem.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                     }
                     listItem.measure(0, 0);
                     totalHeight += listItem.getMeasuredHeight();
                }
    
                LayoutParams params = listView.getLayoutParams();
                params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
                          listView.setLayoutParams(params);
            }
         }
    

    Finally use this line just after setting adapter to your listview - Utility.setListViewHeightBasedOnChildren(your_listview);

    0 讨论(0)
  • 2020-12-21 16:04

    just add this, then it will work.

    listView.setOnTouchListener(new View.OnTouchListener() {
            // Setting on Touch Listener for handling the touch inside ScrollView
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // Disallow the touch request for parent scroll on touch of child view
                v.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });
    
    0 讨论(0)
  • 2020-12-21 16:17

    You dont able to do ScrollViw and ListView in one layout. If you have put ScrollView then ListView will not work and if you dont put ScrollView then ListView will work to scroll.

    UPDATE

    Even if the listview inside scrollview or scrollview inside listview, both will work. You need to handled its touch accordingly.

    0 讨论(0)
  • 2020-12-21 16:18

    Simply, remove the ScrollView from your layout. And, make the LinearLayout as parent. And, try to run your app. It will scroll the list.

    Because the ListView class implements its own scrolling and it just doesn't receive gestures because they all are handled by the parent ScrollView I strongly recommend you to simplify your layout somehow. For example you can add views you want to be scrolled to the ListView as headers or footers.

    Have a look at this

    Update

    Take one parent layout with two child. And, in one layout it should contain ScrollView with your contents. And, on another layout contain your ListView Try like this He has done like that one.

    0 讨论(0)
  • 2020-12-21 16:21

    I found one more solution to get rid of this scrolling problem. Actually using listview in scrollview sometimes might create problems.

    We could remove this scrollview and just take one listview. Now create three layout first main layout with listview (which we want to show above list), second for header and third for footer (which we are to show below list) and finally add header and footer in list. This will add header and footer as a list item.

    Note: This solution is for small list like 2 or 3 items in list.

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