How to scroll layout which have 3 list view

后端 未结 5 772
情歌与酒
情歌与酒 2021-02-08 02:13

I have one layout. This layout contain 3 list view with the height of wrap_content data in the Listview are not fix. When Listview have a liitel huge data at that time the data

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-08 02:23

    Use linear layout instead of listview in your xml file. This is your xml file.

    
    
        
            
            
            
    
            
            
            
        
    
    

    and put another xml which will be inflated by list.

    this is your main activity class.

    package com.list.add;
    
    import java.util.ArrayList;
    import java.util.List;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.LinearLayout;
    import android.widget.ScrollView;
    import android.widget.TextView;
    
    public class NewlistActivity extends Activity {
        /** Called when the activity is first created. */
        LinearLayout l1,l2,l3;
        ScrollView scrollView;
        ViewGroup contentView;
        List list = new ArrayList();
        List list1 = new ArrayList();
        List list2 = new ArrayList();
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            l1 = (LinearLayout) findViewById(R.id.l1);
            l2 = (LinearLayout) findViewById(R.id.l2);
            l3 = (LinearLayout) findViewById(R.id.l3);
            scrollView = (ScrollView) findViewById(R.id.scr);
            contentView = (ViewGroup) findViewById(R.id.r2);
            scrollView.setOnTouchListener(new ScrollPager(scrollView, contentView));
            scrollView.post(new Runnable() {
                public void run() {
                    scrollView.scrollTo(0, contentView.getPaddingTop());
                }
            });
            list.add("Parth");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Parth");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Shah");
            list.add("Parth");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Parth");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Chirag");
            list.add("Shah");
    
            list1.add("Parth");
            list1.add("Parth");
            list1.add("Parth");
            list1.add("Parth");
            list1.add("Parth");
            list1.add("Parth");
    
            list2.add("Kalpesh");
            list2.add("Kalpesh");
            list2.add("Kalpesh");
            list2.add("Kalpesh");
            list2.add("Kalpesh");
            list2.add("Kalpesh");
            list2.add("Kalpesh");
            System.out.println(list);
            System.out.println(list1);
            System.out.println(list2);
            for (int i=0; i

    and make one scroller class like this:

    public class ScrollPager implements OnTouchListener
    public ScrollPager(ScrollView aScrollView, ViewGroup aContentView)
        {
            mScrollView = aScrollView;
            mContentView = aContentView;
            scroller = new Scroller(mScrollView.getContext(), new OvershootInterpolator());
            task = new Runnable()
            {
                public void run()
                {
                    scroller.computeScrollOffset();
                    mScrollView.scrollTo(0, scroller.getCurrY());
    
                    if (!scroller.isFinished())
                    {
                        mScrollView.post(this);
                    }
                }
            };
        }
    public boolean onTouch(View v, MotionEvent event)
        {
            // Stop scrolling calculation.
            scroller.forceFinished(true);
            // Stop scrolling animation.
            mScrollView.removeCallbacks(task);
    
            if (event.getAction() == MotionEvent.ACTION_UP)
            {
    

提交回复
热议问题