Android list view inside a scroll view

后端 未结 30 2045
一向
一向 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:06

    If for some reason you don't want to use addHeaderView and addFooterView, e.g. when you have several lists, a good idea would be to reuse ListAdapter to populate a simple LinearLayout so there's no scrolling functionality.

    If you already have a whole fragment derived from ListFragment and want to convert it to a similar fragment with simple LinearLayout without scrolling instead (e.g. to put it in ScrollView), you can implement an adapter fragment like this:

    // converts listFragment to linearLayout (no scrolling)
    // please call init() after fragment is inflated to set listFragment to convert
    public class ListAsArrayFragment extends Fragment {
        public ListAsArrayFragment() {}
    
        private ListFragment mListFragment;
        private LinearLayout mRootView;
    
    
        // please call me!
        public void init(Activity activity, ListFragment listFragment){
            mListFragment = listFragment;
            mListFragment.onAttach(activity);
            mListFragment.getListAdapter().registerDataSetObserver(new DataSetObserver() {
                @Override
                public void onChanged() {
                    super.onChanged();
                    refreshView();
                }
            });
        }
    
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // create an empty vertical LinearLayout as the root view of this fragment
            mRootView = new LinearLayout(getActivity());
            mRootView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            mRootView.setOrientation(LinearLayout.VERTICAL);
            return mRootView;
        }
    
        // reusing views for performance
        // todo: support for more than one view type
        ArrayList mViewsToReuse = new ArrayList<>();
        ArrayList mCurrentViews = new ArrayList<>();
    
        // re-add views to linearLayout
        void refreshView(){
    
            // remove old views from linearLayout and move them to mViewsToReuse
            mRootView.removeAllViews();
            mViewsToReuse.addAll(mCurrentViews);
            mCurrentViews.clear();
    
            // create new views
            for(int i=0; i

    You may also want to forward onCreate, onPause, onResume, etc. to the original fragment depending on your needs or try inheritance instead of composition (but override certain methods so original fragment is not actually attached to layout hierarchy); but I wanted to isolate original fragment as much as possible, because we only need to extract its ListAdapter. If you call original fragment's setListAdapter in onAttach, that's probably enough.

    Here's how to use ListAsArrayFragment to include OriginalListFragment without scrolling. In parent activity's onCreate:

    ListAsArrayFragment fragment = (ListAsArrayFragment) getFragmentManager().findFragmentById(R.id.someFragmentId);
    OriginalListFragment originalFragment = new OriginalListFragment();
    fragment.init(this, originalFragment);
    
    // now access originalFragment.getListAdapter() to modify list entries
    // and remember to call notifyDatasetChanged()
    

提交回复
热议问题