如何使android listview可滚动?

时间:2012-12-03 08:34:44

标签: android android-listview

我有两个列表视图,但它们不滚动。我该如何纠正?

这是我的layout.xml

 <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/backgrund" >

            <!-- Header  Starts -->

            <LinearLayout
                android:id="@+id/header"
                android:layout_width="fill_parent"
                android:layout_height="40dip"
                android:layout_alignParentTop="true"
                android:background="@layout/header" >
            </LinearLayout>

            <!-- Header Ends -->
            <!-- Footer Start -->

            <TextView
                android:id="@+id/textAD"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/header"
                android:layout_alignParentRight="true"
                android:layout_marginBottom="14dp"
                android:layout_marginRight="26dp"
                android:text="Small Text"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="#FFFFFF" />

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/header"
                android:layout_gravity="center_horizontal"
                android:focusable="false"
                android:paddingBottom="5px"
                android:paddingTop="10px"
                android:src="@android:drawable/divider_horizontal_bright" />

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dip"
                android:background="#000000"
                android:focusable="false" />

            <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/linearLayout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/header"
                android:orientation="vertical" >

                <TextView
                    android:id="@+id/textm"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Malzemeler" 
                    android:textSize="20dp"
                    android:textColor="#000000"/>

                <EditText
                    android:id="@+id/editaramalzeme"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10" />

                <Button
                    android:id="@+id/btnmalzlist"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:focusable="false"
                    android:text="Ara" />

                <ListView
                    android:id="@+id/mylist"
                    android:layout_width="match_parent"
                    android:layout_height="420dp"
                    android:layout_weight="1"
                    android:background="#FFFFFF" >
                </ListView>

                <ListView
                    android:id="@+id/listsecili"
                    android:layout_width="wrap_content"
                    android:layout_height="210dp"
                    android:layout_weight="1"
                    android:background="#FFFFFF"  >
                </ListView>

                <EditText
                    android:id="@+id/txtNot"
                    android:layout_width="match_parent"
                    android:layout_height="88dp"
                    android:ems="10"
                    android:gravity="top"
                    android:inputType="textMultiLine"
                    android:lines="6"
                    android:singleLine="false" >

                    <requestFocus />
                </EditText>
            </LinearLayout>

            <Button
                android:id="@+id/btnkaydet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/linearLayout1"
                android:text="malzeme ekle" />

            <Button
                android:id="@+id/btntoplugonder"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignRight="@+id/textAD"
                android:layout_below="@+id/btnkaydet"
                android:text="toplu gonder" />
        </RelativeLayout>

    </ScrollView>**

10 个答案:

答案 0 :(得分:68)

永远不要将ListView放入ScrollViewListView本身是可滚动的。

答案 1 :(得分:11)

默认情况下,ListView是可滚动的。不要将ScrollView放到ListView

答案 2 :(得分:6)

实际上它不好做。但是如果你想这样做,只需将listview的高度固定为wrap_content。

android:layout_height="wrap_content"

答案 3 :(得分:4)

你不应该把一个ListView放在ScrollView中,因为ListView类实现了自己的滚动,它只是不接收手势,因为它们都由父ScrollView处理

答案 4 :(得分:3)

Listview也有内置滚动功能。所以你不能在scrollview中使用listview。将其封装在任何其他布局中,如LinearLayout或RelativeLayout。

答案 5 :(得分:3)

这是我的工作代码。你可以试试这个。

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/listEmployeeDetails"
        android:layout_height="match_parent" 
        android:layout_width="match_parent"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_gravity="center"
        android:background="#ffffff">

        <TextView android:id="@+id/tvEmpId"
                      android:layout_height="wrap_content"
                      android:textSize="12sp"
                      android:padding="2dp"
                      android:layout_width="0dp"
                      android:layout_weight="0.3"/>
            <TextView android:id="@+id/tvNameEmp"
                      android:layout_height="wrap_content"
                      android:textSize="12sp"                     
                      android:padding="2dp"
                      android:layout_width="0dp"
                      android:layout_weight="0.5"/>
             <TextView
                    android:layout_height="wrap_content"
                    android:id="@+id/tvStatusEmp"
                    android:textSize="12sp"
                    android:padding="2dp"
                    android:layout_width="0dp"
                    android:layout_weight="0.2"/>               
</LinearLayout> 

details.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listEmployeeDetails"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/page_bg"
    android:orientation="vertical" >
    <LinearLayout
        android:id="@+id/lLayoutGrid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/page_bg"
        android:orientation="vertical" >

        ................... others components here............................

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:alwaysDrawnWithCache="true"
            android:dividerHeight="1dp"
            android:horizontalSpacing="3dp"
            android:scrollingCache="true"
            android:smoothScrollbar="true"
            android:stretchMode="columnWidth"
            android:verticalSpacing="3dp" 
            android:layout_marginBottom="30dp">
        </ListView>
    </LinearLayout>
</RelativeLayout>

适配器类:

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {
    private Context context;
    private List<EmployeeBean> employeeList; 

    publicListViewAdapter(Context context, List<EmployeeBean> employeeList) {
            this.context = context;
            this.employeeList = employeeList;
        }

    public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            EmployeeBeanHolder holder = null;
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(R.layout.row, parent, false);

            holder = new EmployeeBeanHolder();
            holder.employeeBean = employeeList.get(position);
            holder.tvEmpId = (TextView) row.findViewById(R.id.tvEmpId);
            holder.tvName = (TextView) row.findViewById(R.id.tvNameEmp);
            holder.tvStatus = (TextView) row.findViewById(R.id.tvStatusEmp);

            row.setTag(holder);
            holder.tvEmpId.setText(holder.employeeBean.getEmpId());
            holder.tvName.setText(holder.employeeBean.getName());
            holder.tvStatus.setText(holder.employeeBean.getStatus());

             if (position % 2 == 0) {
                    row.setBackgroundColor(Color.rgb(213, 229, 241));
                } else {                    
                    row.setBackgroundColor(Color.rgb(255, 255, 255));
                }        

            return row;
        }

   public static class EmployeeBeanHolder {
        EmployeeBean employeeBean;
        TextView tvEmpId;
        TextView tvName;
        TextView tvStatus;
    }

    @Override
    public int getCount() {
            return employeeList.size();
        }

    @Override
    public Object getItem(int position) {
            return null;
        }

    @Override
    public long getItemId(int position) {
            return 0;
    }
}

员工bean类:

public class EmployeeBean {
    private String empId;
    private String name;
    private String status;

    public EmployeeBean(){      
    }

    public EmployeeBean(String empId, String name, String status) {
        this.empId= empId;
        this.name = name;
        this.status = status;
    }

    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId= empId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status =status;
    }
}

在Activity类中:

onCreate方法:

public static List<EmployeeBean> EMPLOYEE_LIST = new ArrayList<EmployeeBean>();

//create emplyee data
for(int i=0;i<=10;i++) {
  EmployeeBean emplyee = new EmployeeBean("EmpId"+i,"Name "+i, "Active");
  EMPLOYEE_LIST .add(emplyee );
}

ListView listView;
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(new ListViewAdapter(this, EMPLOYEE_LIST));

答案 6 :(得分:2)

ListView置于ScrollView内并不会受到启发。 但如果您想要发布类似XML的行为,那么我有三个选择:

  1. 移除ScrollView:移除ScrollView,您可以为ListView提供与总布局相关的特定尺寸(具体dp或{ {1}})。

  2. layout_weight替换为ListView s:您可以通过遍历项目列表添加列表项目,并将每个项目视图添加到相应的LinearLayout膨胀的观点&amp;设置相应的数据(字符串,图像等)

  3. 如果您确实需要将LinearLayout放在ListView内,则必须使ScrollView不可滚动(实际上与解决方案2相同)以上,但使用ListView代码),否则布局将无法按预期运行。
    要使ListView不可滚动,您可以阅读this SO post,其中我的精确解决方案如下所示:

  4. ListView

答案 7 :(得分:1)

我知道这个问题是4-5岁,但是,这可能有用:

有时,如果您只有几个“退出屏幕”的元素,则列表可能无法滚动。那是因为操作系统不会将其视为实际超出屏幕。

我这样说是因为我今天遇到了这个问题 - 我只有2或3个超出屏幕限制的元素,而且我的列表不可滚动。这真是个谜。一旦我添加了一些,它就开始滚动。

所以你必须首先确定它不是一个设计问题,比如显示超出屏幕边界的列表,但实际上,“它没有”,并调整其尺寸和边距值,看看是否它开始“变得可滚动”。它确实对我而言。

答案 8 :(得分:0)

我发现了一个棘手的解决方案......它仅适用于RelativeLayout。 我们只需要在ListView上方放置一个View并设置clickable&#39; true&#39;在View上,对于ListView

是false
 <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listview
    android:clickable="false" />

<View
    android:layout_width="match_parent"
    android:background="@drawable/gradient_white"
    android:layout_height="match_parent"
    android:clickable="true"
    android:layout_centerHorizontal="true"
    android:layout_alignTop="@+id/listview" />

答案 9 :(得分:0)

我遇到了这个问题,我找到了解决方案。在我的例子中,listView 比屏幕大,所以列表不够大,需要滚动,但我看不到它,因为一半的列表超出了屏幕。

我解决了添加足够的 marginBotton 使 listView 更短并且适合屏幕的问题。

<ListView
        android:id="@+id/lvDatos"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="300dp"/>
相关问题