简体   繁体   English

Android Listview不滚动仍然无法正常工作

[英]Android Listview is not scrolling still not working

Good day! 美好的一天!

I have tried a lot of other posts on stackoverflow but it didn't work for me, maybe its because I'm new to Android Development. 我在stackoverflow上尝试了很多其他帖子,但它对我不起作用,也许是因为我是Android开发的新手。

My problem is as follow: 我的问题如下:

在此输入图像描述

XML Layout: 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>

Preview in Android Studio : 在Android Studio中预览

在此输入图像描述

inside the onCreateView: 在onCreateView中:

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

Binding the listview: 绑定列表视图:

 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;

 }

Another point is i can't click a list item wich is strange ! 另一点是我无法点击奇怪的列表项!

Any ideas or tips? 任何想法或提示? what do i wrong? 我错了什么?

Kind Regards, Stefan 亲切的问候,斯特凡

Try something like the following. 尝试以下内容。

<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>
<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>
<LinearLayout>    
    <ScrollView>
         <ListView ...>

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

You need to add a LinearLayout around the scrollview, that way the scrollview knows inside what it should scroll. 你需要在scrollview周围添加一个LinearLayout ,这样滚动视图就会知道它应该滚动的内容。 If this doesn't work, replace the ListView with another LinearLayout and add your items to the LinearLayout. 如果这不起作用,请将ListView替换为另一个LinearLayout,并将项目添加到LinearLayout。 I'm not sure if a View is capable of scrolling at all. 我不确定View是否能够滚动。 Try first option first, otherwise try the latter. 首先尝试第一个选项,否则尝试后者。 I know the latter will work for sure since I used the exact same design myself in an app I'm developing. 我知道后者肯定会起作用,因为我在我正在开发的应用程序中使用了完全相同的设计。

As already mentioned in the comment, you have to add a ScrollView around your ListView, eg: 正如评论中已经提到的,您必须在ListView周围添加ScrollView,例如:

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

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

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