How can i have a ListView inside a NestedScrollView

后端 未结 4 1644
盖世英雄少女心
盖世英雄少女心 2021-01-05 07:52

I have created an app with a page that need to load content dynamically from web service. I want to have listview that can scroll together with a linear layout inside Nested

相关标签:
4条回答
  • 2021-01-05 07:58

    Well, I would suggest you 2 ways to solve that problem:

    1) Try to make LinearLayout a header of your ListView. Note that header should be inflated as it is written here.

    2) You mentioned that you use NestedScrollView, so maybe you should also try to replace ListView inside NestedScrollView with LinearLayout, as wise people suggested here, adding row views in loop similar to how your adapter works.

    Good luck!

    0 讨论(0)
  • 2021-01-05 08:02

    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)
  • 2021-01-05 08:18

    Instead of add a ListView below a Linear Layout and inside a ScrollView, I would suggest to put everything inside the ListView.

    Yes you can.

    Implement (override) following method on your adapter:

    public class MyAdapter extends BaseAdapter {
        // One view to Header
        // One view to filter options ("most helpful first" and "Options")
        // One view to comments
        private final static int VIEW_HEADER = 0;
        private final static int VIEW_OPTIONS = 1;
        private final static int VIEW_COMMENTS = 2;
        private final static int VIEW_TYPE_MAX = 3;
    
        @Override
        public int getViewTypeCount () {
            // It will return 3 since I have 3 different types of VIEW
            return VIEW_TYPE_MAX;
        }
    
        @Override
        public int getItemViewType(int position) {
            if (position == 0)
                return VIEW_HEADER;
            else if (position == 1)
                return VIEW_OPTIONS;
            else
                return VIEW_COMMENTS;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                if(getItemViewType(position) == VIEW_HEADER)
                    // Inflate HEADER Layout
                else if (getItemViewType(position) == VIEW_OPTIONS)
                    // Inflate Options Layout
                else
                    // Inflate comments Layout
            }
    
            // Fill the view contents according to its type
            ....
            return convertView;
        }
    }
    

    Android will re-use the views. However Android will re-use views of the same type always.

    0 讨论(0)
  • 2021-01-05 08:21

    Just use this code, if you want to make listview expand in a nestedscrollview. Pass the listview reference to the function at the end of creating listview.

    private static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null)
            return;
    
        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
        int totalHeight = 0;
        View view = null;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            view = listAdapter.getView(i, view, listView);
            if (i == 0)
                view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, ViewGroup.LayoutParams.WRAP_CONTENT));
    
            view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
            totalHeight += view.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
    
    0 讨论(0)
提交回复
热议问题