如何使android listview可滚动?

xkftehaa  于 2021-06-29  发布在  Java
关注(0)|答案(9)|浏览(417)

我有两个列表视图,但它们不滚动。我该怎么纠正这个问题?
这是我的 layout.xml ```

  1. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="fill_parent"
  3. android:layout_height="wrap_content"
  4. android:fillViewport="true" >
  5. <RelativeLayout
  6. android:layout_width="fill_parent"
  7. android:layout_height="wrap_content"
  8. android:background="@drawable/backgrund" >
  9. <!-- Header Starts -->
  10. <LinearLayout
  11. android:id="@+id/header"
  12. android:layout_width="fill_parent"
  13. android:layout_height="40dip"
  14. android:layout_alignParentTop="true"
  15. android:background="@layout/header" >
  16. </LinearLayout>
  17. <!-- Header Ends -->
  18. <!-- Footer Start -->
  19. <TextView
  20. android:id="@+id/textAD"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_alignBottom="@+id/header"
  24. android:layout_alignParentRight="true"
  25. android:layout_marginBottom="14dp"
  26. android:layout_marginRight="26dp"
  27. android:text="Small Text"
  28. android:textAppearance="?android:attr/textAppearanceSmall"
  29. android:textColor="#FFFFFF" />
  30. <ImageView
  31. android:layout_width="fill_parent"
  32. android:layout_height="wrap_content"
  33. android:layout_below="@+id/header"
  34. android:layout_gravity="center_horizontal"
  35. android:focusable="false"
  36. android:paddingBottom="5px"
  37. android:paddingTop="10px"
  38. android:src="@android:drawable/divider_horizontal_bright" />
  39. <View
  40. android:layout_width="fill_parent"
  41. android:layout_height="1dip"
  42. android:background="#000000"
  43. android:focusable="false" />
  44. <LinearLayout
  45. xmlns:android="http://schemas.android.com/apk/res/android"
  46. android:id="@+id/linearLayout1"
  47. android:layout_width="match_parent"
  48. android:layout_height="match_parent"
  49. android:layout_below="@+id/header"
  50. android:orientation="vertical" >
  51. <TextView
  52. android:id="@+id/textm"
  53. android:layout_width="match_parent"
  54. android:layout_height="wrap_content"
  55. android:text="Malzemeler"
  56. android:textSize="20dp"
  57. android:textColor="#000000"/>
  58. <EditText
  59. android:id="@+id/editaramalzeme"
  60. android:layout_width="match_parent"
  61. android:layout_height="wrap_content"
  62. android:ems="10" />
  63. <Button
  64. android:id="@+id/btnmalzlist"
  65. android:layout_width="match_parent"
  66. android:layout_height="wrap_content"
  67. android:focusable="false"
  68. android:text="Ara" />
  69. <ListView
  70. android:id="@+id/mylist"
  71. android:layout_width="match_parent"
  72. android:layout_height="420dp"
  73. android:layout_weight="1"
  74. android:background="#FFFFFF" >
  75. </ListView>
  76. <ListView
  77. android:id="@+id/listsecili"
  78. android:layout_width="wrap_content"
  79. android:layout_height="210dp"
  80. android:layout_weight="1"
  81. android:background="#FFFFFF" >
  82. </ListView>
  83. <EditText
  84. android:id="@+id/txtNot"
  85. android:layout_width="match_parent"
  86. android:layout_height="88dp"
  87. android:ems="10"
  88. android:gravity="top"
  89. android:inputType="textMultiLine"
  90. android:lines="6"
  91. android:singleLine="false" >
  92. <requestFocus />
  93. </EditText>
  94. </LinearLayout>
  95. <Button
  96. android:id="@+id/btnkaydet"
  97. android:layout_width="wrap_content"
  98. android:layout_height="wrap_content"
  99. android:layout_alignParentLeft="true"
  100. android:layout_below="@+id/linearLayout1"
  101. android:text="malzeme ekle" />
  102. <Button
  103. android:id="@+id/btntoplugonder"
  104. style="?android:attr/buttonStyleSmall"
  105. android:layout_width="wrap_content"
  106. android:layout_height="wrap_content"
  107. android:layout_alignRight="@+id/textAD"
  108. android:layout_below="@+id/btnkaydet"
  109. android:text="toplu gonder" />
  110. </RelativeLayout>
  111. </ScrollView>**
6xfqseft

6xfqseft1#

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/listEmployeeDetails"
  4. android:layout_height="match_parent"
  5. android:layout_width="match_parent"
  6. android:layout_marginTop="5dp"
  7. android:layout_marginBottom="5dp"
  8. android:layout_gravity="center"
  9. android:background="#ffffff">
  10. <TextView android:id="@+id/tvEmpId"
  11. android:layout_height="wrap_content"
  12. android:textSize="12sp"
  13. android:padding="2dp"
  14. android:layout_width="0dp"
  15. android:layout_weight="0.3"/>
  16. <TextView android:id="@+id/tvNameEmp"
  17. android:layout_height="wrap_content"
  18. android:textSize="12sp"
  19. android:padding="2dp"
  20. android:layout_width="0dp"
  21. android:layout_weight="0.5"/>
  22. <TextView
  23. android:layout_height="wrap_content"
  24. android:id="@+id/tvStatusEmp"
  25. android:textSize="12sp"
  26. android:padding="2dp"
  27. android:layout_width="0dp"
  28. android:layout_weight="0.2"/>
  29. </LinearLayout>

详细信息.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:id="@+id/listEmployeeDetails"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="@color/page_bg"
  6. android:orientation="vertical" >
  7. <LinearLayout
  8. android:id="@+id/lLayoutGrid"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:background="@color/page_bg"
  12. android:orientation="vertical" >
  13. ................... others components here............................
  14. <ListView
  15. android:id="@+id/listView"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:alwaysDrawnWithCache="true"
  19. android:dividerHeight="1dp"
  20. android:horizontalSpacing="3dp"
  21. android:scrollingCache="true"
  22. android:smoothScrollbar="true"
  23. android:stretchMode="columnWidth"
  24. android:verticalSpacing="3dp"
  25. android:layout_marginBottom="30dp">
  26. </ListView>
  27. </LinearLayout>
  28. </RelativeLayout>

适配器类:

  1. import java.util.List;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.graphics.Color;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.TextView;
  10. public class ListViewAdapter extends BaseAdapter {
  11. private Context context;
  12. private List<EmployeeBean> employeeList;
  13. publicListViewAdapter(Context context, List<EmployeeBean> employeeList) {
  14. this.context = context;
  15. this.employeeList = employeeList;
  16. }
  17. public View getView(int position, View convertView, ViewGroup parent) {
  18. View row = convertView;
  19. EmployeeBeanHolder holder = null;
  20. LayoutInflater inflater = ((Activity) context).getLayoutInflater();
  21. row = inflater.inflate(R.layout.row, parent, false);
  22. holder = new EmployeeBeanHolder();
  23. holder.employeeBean = employeeList.get(position);
  24. holder.tvEmpId = (TextView) row.findViewById(R.id.tvEmpId);
  25. holder.tvName = (TextView) row.findViewById(R.id.tvNameEmp);
  26. holder.tvStatus = (TextView) row.findViewById(R.id.tvStatusEmp);
  27. row.setTag(holder);
  28. holder.tvEmpId.setText(holder.employeeBean.getEmpId());
  29. holder.tvName.setText(holder.employeeBean.getName());
  30. holder.tvStatus.setText(holder.employeeBean.getStatus());
  31. if (position % 2 == 0) {
  32. row.setBackgroundColor(Color.rgb(213, 229, 241));
  33. } else {
  34. row.setBackgroundColor(Color.rgb(255, 255, 255));
  35. }
  36. return row;
  37. }
  38. public static class EmployeeBeanHolder {
  39. EmployeeBean employeeBean;
  40. TextView tvEmpId;
  41. TextView tvName;
  42. TextView tvStatus;
  43. }
  44. @Override
  45. public int getCount() {
  46. return employeeList.size();
  47. }
  48. @Override
  49. public Object getItem(int position) {
  50. return null;
  51. }
  52. @Override
  53. public long getItemId(int position) {
  54. return 0;
  55. }
  56. }

employee bean类:

  1. public class EmployeeBean {
  2. private String empId;
  3. private String name;
  4. private String status;
  5. public EmployeeBean(){
  6. }
  7. public EmployeeBean(String empId, String name, String status) {
  8. this.empId= empId;
  9. this.name = name;
  10. this.status = status;
  11. }
  12. public String getEmpId() {
  13. return empId;
  14. }
  15. public void setEmpId(String empId) {
  16. this.empId= empId;
  17. }
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. public String getStatus() {
  25. return status;
  26. }
  27. public void setStatus(String status) {
  28. this.status =status;
  29. }
  30. }

在活动课上:
oncreate方法:

  1. public static List<EmployeeBean> EMPLOYEE_LIST = new ArrayList<EmployeeBean>();
  2. //create emplyee data
  3. for(int i=0;i<=10;i++) {
  4. EmployeeBean emplyee = new EmployeeBean("EmpId"+i,"Name "+i, "Active");
  5. EMPLOYEE_LIST .add(emplyee );
  6. }
  7. ListView listView;
  8. listView = (ListView) findViewById(R.id.listView);
  9. listView.setAdapter(new ListViewAdapter(this, EMPLOYEE_LIST));
展开查看全部
h4cxqtbf

h4cxqtbf2#

我知道这个问题已经有4-5年的历史了,不过,这可能还是有用的:
有时,如果只有几个元素“退出屏幕”,列表可能不会滚动。这是因为操作系统并不认为它实际上超出了屏幕。
我这么说是因为我今天遇到了这个问题-我只有2到3个元素超出了屏幕限制,而且我的列表不能滚动。这真是个谜。我一加上几个,它就开始滚动。
因此,你必须首先确保它不是一个设计问题,就像列表看起来超出了屏幕的边界,但实际上,“它没有”,并调整其尺寸和边距值,看看它是否开始“滚动”。是的,对我来说。

oewdyzsn

oewdyzsn3#

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

2q5ifsrm

2q5ifsrm4#

从不放 ListViewScrollView . ListView 它本身是可滚动的。

p4rjhz4m

p4rjhz4m5#

实际上这不好做。但是如果您想这样做,只需将listview的高度固定为 Package 内容。

  1. android:layout_height="wrap_content"
bprjcwpo

bprjcwpo6#

listview因此具有内置滚动功能。所以不能在scrollview中使用listview。将其封装在任何其他布局中,如linearlayout或relativelayout。

4jb9z9bj

4jb9z9bj7#

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

lnvxswe2

lnvxswe28#

ListView 内部 ScrollView 从来没有灵感。但是,如果您想要发布类似xml的行为,我有3个选择:
删除 ScrollView :删除您的 ScrollView ,您可以给 ListView 相对于总布局,有一些特定的大小(或者特定的 dp 或者 layout_weight ).
替换 ListView s与 LinearLayout s:您可以通过迭代项列表来添加列表项,并将每个项视图添加到相应的 LinearLayout 通过膨胀视图并设置相应的数据(字符串、图像等)
如果你真的需要 ListView 他在房间里 ScrollView ,你必须 ListView s不可滚动(这实际上与上面的解决方案2相同,但是 ListView 代码),否则布局将无法按预期运行。
制造 ListView 不可滚动,您可以阅读这篇so帖子,其中对我的精确解决方案如下:

  1. listView.setOnTouchListener(new OnTouchListener() {
  2. public boolean onTouch(View v, MotionEvent event) {
  3. return (event.getAction() == MotionEvent.ACTION_MOVE);
  4. }
  5. });
bsxbgnwa

bsxbgnwa9#

我找到了一个棘手的解决办法。。。它只在相对布局中起作用。我们只需要把一个视图放在一个listview的上面,并为视图设置可点击的'true',为listview设置可点击的'false'

  1. <ListView
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:id="@+id/listview
  5. android:clickable="false" />
  6. <View
  7. android:layout_width="match_parent"
  8. android:background="@drawable/gradient_white"
  9. android:layout_height="match_parent"
  10. android:clickable="true"
  11. android:layout_centerHorizontal="true"
  12. android:layout_alignTop="@+id/listview" />

相关问题