android - 如何滚动具有 3 个 ListView 的布局

标签 android android-layout

我有一种布局。此布局包含 3 个 ListView , ListView 中 wrap_content 数据的高度不固定。当 Listview 有一个 liitel 巨大的数据时,数据会在下面并且数据无法看到,所以我想用所有三个 Listview ScrollView 怎么可能。

有人对此有想法吗?

这是我的 View ,其中包含 3 个 Listview,现在它的数据较少,但是当数据变大时,最后一个 Listview 无法查看。我想滚动灰色 View ...

enter image description here

最佳答案

在您的 xml 文件中使用线性布局而不是 ListView 。 这是您的 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>

并放入另一个将由列表膨胀的 xml。

这是您的主要 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);
        }
    }

}

然后像这样制作一个滚动条类:

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

关于android - 如何滚动具有 3 个 ListView 的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7657961/

相关文章:

android - 5dp 的 'padding' 处不允许使用整数类型

android - 使用 chrisjenx CalligraphyLayoutInflater 时出现 InflateException,android.view.InflateException

java - 发送邮件后返回应用

AndroidPlot - XYplot 未显示在 xml 中的图形布局上

android - admob 横幅未显示在弹出窗口中

android - 多行编辑文本和编辑文本的首字母大写

android - 在 android 中,当方向改变时,背景图像不会改变

android - 如何让我的 AppWidget 自动填充主屏幕的宽度?

android - Android上如何计算网卡何时打开连接

android - ActionBar 中 MenuItem 的默认全息蓝色背景是什么?