简体   繁体   English

如何使android listview可滚动?

[英]How to make android listview scrollable?

I have two listviews, but they don't scroll.我有两个列表视图,但它们不滚动。 How do I correct this?我该如何纠正?

Here is my layout.xml这是我的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>**

Never put ListView in ScrollView . 切勿将ListView放在ScrollView ListView itself is scrollable. ListView本身是可滚动的。

By default ListView is scrollable. 默认情况下,ListView是可滚动的。 Do not put ScrollView to the ListView 不要将ScrollView放到ListView中

Practically its not good to do. 实际上它不好做。 But if you want to do like this, just make listview's height fixed to wrap_content. 但是如果你想这样做,只需将listview的高度固定为wrap_content即可。

android:layout_height="wrap_content"

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

Listview so have inbuild scrolling capabilities. Listview所以有内置滚动功能。 So you can not use listview inside scrollview. 所以你不能在scrollview中使用listview。 Encapsulate it in any other layout like LinearLayout or RelativeLayout. 将其封装在任何其他布局中,如LinearLayout或RelativeLayout。

This is my working code. 这是我的工作代码。 you may try with this. 你可以试试这个。

row.xml 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 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>

Adapter class : 适配器类:

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;
    }
}

employee bean class: 员工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;
    }
}

in Activity class: 在Activity类中:

onCreate method: 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));

Putting ListView inside a ScrollView is never inspired. ListView放在ScrollView中从未受到启发。 But if you want your posted XML-like behavior, there're 3 options to me: 但如果您想要发布类似XML的行为,那么我有3个选项:

  1. Remove ScrollView : Removing your ScrollView , you may give the ListView s some specific size with respect to the total layout (either specific dp or layout_weight ). 删除ScrollView :删除ScrollView ,您可以为ListView提供与总布局(特定dplayout_weight )相关的特定大小。

  2. Replace ListView s with LinearLayout s: You may add the list-items by iterating through the item-list and add each item-view to the respective LinearLayout by inflating the view & setting the respective data (string, image etc.) 使用LinearLayout替换ListView :您可以通过迭代项列表来添加列表项,并通过膨胀视图和设置相应的数据(字符串,图像等)将每个项目视图添加到相应的LinearLayout

  3. If you really need to put your ListView s inside the ScrollView , you must make your ListView s non-scrollable (Which is practically the same as the solution 2 above, but with ListView codes), otherwise the layout won't function as you expect. 如果您确实需要将ListView放在ScrollView ,则必须使ListView不可滚动(这与上面的解决方案2实际上相同,但使用ListView代码),否则布局将无法按预期运行。
    To make a ListView non-scrollable, you may read this SO post , where the precise solution to me is like the one below: 要使ListView不可滚动,您可以阅读此SO帖子 ,其中我的精确解决方案如下所示:

 listView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { return (event.getAction() == MotionEvent.ACTION_MOVE); } }); 

I know this question is 4-5 years old, but still, this might be useful: 我知道这个问题是4-5岁,但是,这可能是有用的:

Sometimes, if you have only a few elements that "exit the screen", the list might not scroll. 有时,如果您只有几个元素“退出屏幕”,则列表可能无法滚动。 That's because the operating system doesn't view it as actually exceeding the screen. 那是因为操作系统不会将其视为实际超出屏幕。

I'm saying this because I ran into this problem today - I only had 2 or 3 elements that were exceeding the screen limits, and my list wasn't scrollable. 我这样说是因为我今天遇到了这个问题 - 我只有2或3个超出屏幕限制的元素,我的列表不可滚动。 And it was a real mystery. 这真是个谜。 As soon as I added a few more, it started to scroll. 一旦我添加了一些,它就开始滚动了。

So you have to make sure it's not a design problem at first, like the list appearing to go beyond the borders of the screen but in reality, "it doesn't", and adjust its dimensions and margin values and see if it's starting to "become scrollable". 所以你必须首先确定它不是一个设计问题,比如看起来超出屏幕边界的列表,但实际上,“它没有”,并调整其尺寸和边距值,看它是否开始“变得可滚动”。 It did, for me. 它确实对我而言。

I found a tricky solution... which works only in a RelativeLayout. 我发现了一个棘手的解决方案......它仅适用于RelativeLayout。 We only need to put a View above a ListView and set clickable 'true' on View and false for the ListView 我们只需要在ListView上面放置一个View,并在View上设置可点击的'true',为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" />

I had this problem and I found the solution.我遇到了这个问题,我找到了解决方案。 In my case the listView was bigger than the screen, so the list was not big enough to need scroll, but I coudn´t see it because half list was out of screen.在我的例子中,listView 比屏幕大,所以列表不够大,需要滚动,但我看不到它,因为一半列表在屏幕外。

I solved it adding enough marginBotton to make the listView shorter and it suits to the screen.我解决了它添加足够的 marginBotton 使 listView 更短并且适合屏幕的问题。

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM