Android list view inside a scroll view

后端 未结 30 2058
一向
一向 2020-11-21 13:43

I have an android layout which has a scrollView with a number of elements with in it. At the bottom of the scrollView I have a listView

30条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 14:00

    found a solution for scrollview -> viewpager -> FragmentPagerAdapter -> fragment -> dynamic listview, but im not the author. there is some bugs, but at least it works

    public class CustomPager extends ViewPager {
    
        private View mCurrentView;
    
        public CustomPager(Context context) {
            super(context);
        }
    
        public CustomPager(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            if (mCurrentView == null) {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                return;
            }
            int height = 0;
            mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = mCurrentView.getMeasuredHeight();
            if (h > height) height = h;
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
    
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    
        public void measureCurrentView(View currentView) {
            mCurrentView = currentView;
            this.post(new Runnable() {
                @Override
                public void run() {
                    requestLayout();
                }
            });
        }
    
        public int measureFragment(View view) {
            if (view == null)
                return 0;
    
            view.measure(0, 0);
            return view.getMeasuredHeight();
        }
    }
    
    
    public class MyPagerAdapter extends FragmentPagerAdapter {
    
        private List fragments;
        private int mCurrentPosition = -1;
    
    
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);//or u can set them separately, but dont forget to call notifyDataSetChanged()
            this.fragments = new ArrayList();
            fragments.add(new FirstFragment());
            fragments.add(new SecondFragment());
            fragments.add(new ThirdFragment());
            fragments.add(new FourthFragment());
        }
    
        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            super.setPrimaryItem(container, position, object);
            if (position != mCurrentPosition) {
                Fragment fragment = (Fragment) object;
                CustomPager pager = (CustomPager) container;
                if (fragment != null && fragment.getView() != null) {
                    mCurrentPosition = position;
                    pager.measureCurrentView(fragment.getView());
                }
            }
        }
    
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }
    
        @Override
        public int getCount() {
            return fragments.size();
        }
    }
    

    fragments layout can be anything

    
    
    
        
    
    

    then somewhere just

    lv = (ListView) view.findViewById(R.id.lv1);
            lv.setAdapter(arrayAdapter);
            setListViewHeightBasedOnChildren(lv);
        }
    
        public 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,
                            LinearLayout.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);
            listView.requestLayout();
        }
    

提交回复
热议问题