How to scroll layout which have 3 list view

后端 未结 5 779
情歌与酒
情歌与酒 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.

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView android:id="@+id/scr" android:layout_height="fill_parent"
        android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
        <LinearLayout android:layout_width="fill_parent"
            android:id="@+id/r2" android:orientation="vertical"
            android:layout_height="fill_parent" android:paddingTop="100dip"
            android:paddingBottom="100dip">
            <LinearLayout android:layout_width="fill_parent"
                android:id="@+id/l1" android:orientation="vertical"
                android:layout_height="wrap_content" android:layout_marginTop="10dip"
                android:background="#00B2EE">
            </LinearLayout>
            <LinearLayout android:layout_width="fill_parent"
                android:id="@+id/l2" android:orientation="vertical"
                android:layout_height="wrap_content" android:layout_marginTop="10dip"
                android:background="#00EE76">
    
            </LinearLayout>
            <LinearLayout android:layout_width="fill_parent"
                android:id="@+id/l3" android:orientation="vertical"
                android:layout_height="wrap_content" android:layout_marginTop="10dip"
                android:background="#7171C6">
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
    

    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<String> list = new ArrayList<String>();
        List<String> list1 = new ArrayList<String>();
        List<String> list2 = new ArrayList<String>();
    
        @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<list.size(); i++) 
            {
            LayoutInflater inflater = getLayoutInflater();
            View vi = inflater.inflate(R.layout.raw, null);
            TextView tv = (TextView) vi.findViewById(R.id.textView1);
            tv.setText(list.get(i));
            l1.addView(vi);
            }
            for (int i=0; i<list1.size(); i++) 
            {
            LayoutInflater inflater = getLayoutInflater();
            View vi = inflater.inflate(R.layout.raw, null);
            TextView tv = (TextView) vi.findViewById(R.id.textView1);
            tv.setText(list1.get(i));
            l2.addView(vi);
            }
            for (int i=0; i<list2.size(); i++) 
            {
            LayoutInflater inflater = getLayoutInflater();
            View vi = inflater.inflate(R.layout.raw, null);
            TextView tv = (TextView) vi.findViewById(R.id.textView1);
            tv.setText(list2.get(i));
            l3.addView(vi);
            }
        }
    
    }
    

    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)
            {
    
    0 讨论(0)
  • 2021-02-08 02:24

    Use this method and your listview become scrollable inside scrollview:-

    ListView lstNewsOffer.setAdapter(new ViewOfferAdapter(
                            ViewNewsDetail.this, viewOfferList));
                    getListViewSize(lstNewsOffer);
    
     void getListViewSize(ListView myListView) {
    ListAdapter myListAdapter = myListView.getAdapter();
    if (myListAdapter == null) {
        // do nothing return null
        return;
    }
    // set listAdapter in loop for getting final size
    int totalHeight = 0;
    for (int size = 0; size < myListAdapter.getCount(); size++) {
        View listItem = myListAdapter.getView(size, null, myListView);
        listItem.measure(0, 0);
        totalHeight += listItem.getMeasuredHeight();
    }
    // setting listview item in adapter
    ViewGroup.LayoutParams params = myListView.getLayoutParams();
    params.height = totalHeight
            + (myListView.getDividerHeight() * (myListAdapter.getCount() - 1));
    myListView.setLayoutParams(params);
    // print height of adapter on log
    Log.i("height of listItem:", String.valueOf(totalHeight));
    

    }

    0 讨论(0)
  • 2021-02-08 02:36

    You should use weight attribute android:layout_weight="1" in each ListView in XML layout. So that it will divide the screen with equal space for each listview and your scrolling will work for each ListView.

    0 讨论(0)
  • 2021-02-08 02:36

    If you are presenting stuff in a vertical or horizontal stack, you should be using LinearLayout and then use the layout_weight attribute to control the proportions of the individual rows/columns within the container.

    If you want screen-sized, set the layout_width and layout_height to fill_parent otherwise you will not get all available screen dimension. If you try to use wrap_content for height, everything will collapse, unless you resort to additional layout constraints, e.g. minHeight.

    We use this everywhere and it is quite reliable. For three items, you can use 1/1/1 or 3/3/3.

    The weights also do not have to be equal! You can divide up the proportions any way you wish; the weights are just relative ratios of the entire span (width/height). E.g. if you want the middle element twice the size, use 1/2/1; if you want it 40% use 30/40/30 or 3/4/3.

    A good "trick" is to use layout_weight=1 on exactly one row/column (others default to zero), and it will "fill-in" any remaining space. This is a common layout scenario.

    If you need the stack to be scrollable, you can put it into a ScrollView. In this case, you must set the layout_height of the LinearLayout to wrap_content, and you will be subject collapsing depending on the whim of the layout system (meaning you need to then use min/max constraints).

    0 讨论(0)
  • 2021-02-08 02:44

    Instead, let me give my best idea for the same kind of implementation.

    Have you ever thought to use ExpandableListView for the same kind of implementation? Just show the 1st group item as expanded and make other 2 groups as collapsed, just expand it when user click on the particular group.

    Here are some examples for the same:

    1. http://techdroid.kbeanie.com/2010/09/expandablelistview-on-android.html
    2. http://coderzheaven.com/2011/04/expandable-listview-in-android-using-simpleexpandablelistadapter-a-simple-example/

    I am sure this idea is not good but if you implement this then you will sure found beneficial in your case.

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