简体   繁体   English

如何滚动有3个列表视图的布局

[英]How to scroll layout which have 3 list view

I have one layout. 我有一个布局。 This layout contain 3 list view with the height of wrap_content data in the Listview are not fix. 此布局包含3个列表视图,Listview中的wrap_content数据的高度未修复。 When Listview have a liitel huge data at that time the data goes to underneath and the data can not able to see,so i want to scroll the view with all three Listview how it is possible. 当Listview当时有大量数据时,数据会进入下面并且数据无法看到,所以我想用所有三个Listview滚动视图如何实现。

Any one have an idea about this ? 有谁对此有所了解?

This is my view which contain 3 Listview, now it's with the less data but when the data will make huge at that time the last Listview have a problem to view. 这是我的视图,其中包含3个Listview,现在它的数据较少,但当数据在那时变得很大时,最后一个Listview有问题需要查看。 I want to scroll the grey colored view... 我想滚动灰色视图...

在此输入图像描述

Use linear layout instead of listview in your xml file. 在xml文件中使用线性布局而不是listview。 This is your xml file. 这是你的xml文件。

<?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. 并放入另一个将由列表膨胀的xml。

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)
        {

Use this method and your listview become scrollable inside scrollview:- 使用此方法,您的列表视图可在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));

} }

You should use weight attribute android:layout_weight="1" in each ListView in XML layout. 您应该在XML布局中的每个ListView中使用权重属性android:layout_weight="1" So that it will divide the screen with equal space for each listview and your scrolling will work for each ListView. 因此,它将为每个列表视图划分具有相等空间的屏幕,并且您的滚动将适用于每个ListView。

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. 如果要在垂直或水平堆栈中显示内容,则应使用LinearLayout ,然后使用layout_weight属性控制容器中各个行/列的比例。

If you want screen-sized, set the layout_width and layout_height to fill_parent otherwise you will not get all available screen dimension. 如果您想要屏幕大小,请将layout_widthlayout_height设置为fill_parent否则您将无法获得所有可用的屏幕尺寸。 If you try to use wrap_content for height, everything will collapse, unless you resort to additional layout constraints, eg minHeight . 如果你尝试使用wrap_content作为高度,一切都会崩溃,除非你采用额外的布局约束,例如minHeight

We use this everywhere and it is quite reliable. 我们在任何地方都使用它,它非常可靠。 For three items, you can use 1/1/1 or 3/3/3 . 对于三个项目,您可以使用1/1/13/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). 权重只是整个跨度(宽度/高度)的相对比率。 Eg if you want the middle element twice the size, use 1/2/1 ; 例如,如果您想要中间元素的两倍大小,请使用1/2/1 ; if you want it 40% use 30/40/30 or 3/4/3 . 如果你想要40%使用30/40/303/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. 一个好的“技巧”是在一行/列上使用layout_weight = 1(其他行默认为零),它将“填充”任何剩余空间。 This is a common layout scenario. 这是一种常见的布局方案。

If you need the stack to be scrollable, you can put it into a ScrollView . 如果需要堆栈可滚动,可以将其放入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). 在这种情况下,您必须将LinearLayoutlayout_height设置为wrap_content ,并且您将根据布局系统的奇思妙想进行折叠(这意味着您需要使用最小/最大约束)。

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? 你有没有想过使用ExpandableListView进行同样的实现? 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 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/ 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. 我确信这个想法并不好,但如果你实施了这个,那么你肯定会发现在你的情况下是有益的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用父级布局滚动可扩展列表视图 - How to scroll expandable list view with parent layout 如何在片段中存在的相对布局中使用滚动视图? - How to use scroll view in relative layout which exist in fragment? 如何动态添加imageView到具有线性布局的滚动视图? - How to add imageView dynamically to scroll view which has linear layout? 滚动视图滚动列表视图而不是布局 - Scroll view scrolls the list view not the layout 当滚动视图子级具有权重时,Android如何在父滚动视图中使用列表视图子级 - Android how to use list view child in parent scroll view when scroll view child have weight 滚动和显示时的android list view布局动画 - android list view layout animation on scroll and on show 如何布置对话框=&gt;滚动视图=&gt;可扩展列表视图? - How can I layout my dialog => scroll view => expandable list view? 如何删除由于android中布局底部的Scroll View导致的多余空间? - How to remove the extra space which is occurred because of Scroll View at the bottom of layout in android? 如何设置滚动视图内并包含两个 TextView 的线性布局的可见性消失? - How to set visibility GONE of a Linear Layout which is inside a scroll view and contains two TextViews inside? (Android)如果我有水平滚动,如何将TextView放在屏幕的中心(不在布局或父视图中)? - (Android) How to put a TextView int the Center of the screen (Not in the Layout or Parent View) if I have a Horizontal Scroll?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM