【问题标题】:Android Listview is not scrolling still not workingAndroid Listview 不滚动仍然无法正常工作
【发布时间】:2015-08-02 10:51:11
【问题描述】:

美好的一天!

我在 stackoverflow 上尝试了很多其他帖子,但对我没有用, 可能是因为我是 Android 开发新手。

我的问题如下:

XML 布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_detail_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".ItemDetailActivity"
tools:ignore="MergeRootFrame" >

<TextView
    android:layout_width="match_parent"
    android:layout_height="89dp"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/txtDetails"
    android:layout_gravity="center_horizontal|top"
    android:padding="5dp" />

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="369dp"
    android:layout_gravity="center_horizontal|bottom" />

</FrameLayout>

在 Android Studio 中预览

onCreateView 内部:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) { }

绑定列表视图:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
 final View rootView = inflater.inflate(R.layout.fragment_item_detail,     container, false);


  List<Events> list = Events.getEvents();
  int max = list.size();
  String[] Values = new String[max];
  for (int i = 0; i < max; i++) {

      Events e = list.get(i);
      Values[i] = e.getNaam();
  }

  ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
                                    rootView.getContext(),
                                    android.R.layout.simple_list_item_1, Values);

 listView.setAdapter(arrayAdapter);

 return rootview;

 }

另一点是我不能点击一个奇怪的列表项!

有什么想法或提示吗? 我怎么了?

亲切的问候, 斯蒂芬

【问题讨论】:

  • 尝试将滚动视图添加到您的列表视图
  • 相同结果不可滚动

标签: java android listview


【解决方案1】:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_detail_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".ItemDetailActivity"
tools:ignore="MergeRootFrame" >

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/txtDetails"
    android:layout_gravity="center_horizontal|top"
    android:padding="5dp" />

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|bottom" />

</FrameLayout>

【讨论】:

  • 请设置绑定listview项的代码,因为我试过了,它滚动成功这证明xml没问题,问题可能出在java代码中
  • 尝试绑定类似此代码的项目并反馈我是否有效
  • 物品绑定在描述中
  • 请使用此代码 ListView lv=(ListView) findViewById(R.id.listView1); ArrayAdapter 适配器 = new ArrayAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
  • 移除 root.getcontext 并将其替换为 getapplicaitoncontext 或 Activityname.this
【解决方案2】:

试试下面的方法。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_detail_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    tools:context=".ItemDetailActivity"
    tools:ignore="MergeRootFrame" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/linearLayout">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/txtDetails"
            android:layout_gravity="center_horizontal|top"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingTop="10dp"
            android:paddingBottom="10dp" />

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>

</FrameLayout>

【讨论】:

  • 您的 ListView 是否足够大以启用滚动功能?
  • 是的!我什至不得不缩小它,因为抛出内存异常
  • 你能展示一下你是如何填充你的 ListView 的吗? java部分的代码。
  • 更新了答案。试试看。
【解决方案3】:
<LinearLayout>    
    <ScrollView>
         <ListView ...>

         </listView>
    </ScrollView>
</LinearLayout>

你需要在滚动视图周围添加一个LinearLayout,这样滚动视图就知道它应该在里面滚动什么。如果这不起作用,请将 ListView 替换为另一个 LinearLayout 并将您的项目添加到 LinearLayout。我不确定View 是否能够滚动。首先尝试第一个选项,否则尝试后者。我知道后者肯定会起作用,因为我自己在我正在开发的应用程序中使用了完全相同的设计。

【讨论】:

    【解决方案4】:

    正如评论中已经提到的,你必须在你的 ListView 周围添加一个 ScrollView,例如:

    <ScrollView>
         <ListView ....>
         </ListView>
    </ScrollView>
    

    【讨论】:

    • 更新了我的帖子,我尝试了相同的结果而不是滚动 Ty!
    • 列表视图不在滚动视图内。切换到 XML 代码并确保 ListView 在 ScrollView 中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多