滚动活动中的ListView仅显示一个项目

时间:2015-11-18 16:45:34

标签: android android-listview

我正在使用Android Studio 1.4附带的滚动活动 在这里我有一个ImageView和一个ListView。此布局的xml是:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.viv.droidchef.EachRec"
    tools:showIn="@layout/each_rec">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:id="@+id/recPixx"/>
        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/ingredients_list"></ListView>

    </LinearLayout>



</android.support.v4.widget.NestedScrollView>

我面临的问题是,在我的ListView 中,只显示第一项。这是我的ListView代码

String ingredients = new String[]{
                    " Boneless Chicken                    - 300 g",
                    "Ginger garlic paste                   - 2 tsp",
                    " Kashmiri red chilly pwd              - ¾ tsp",
                    "Lemon juice                           - 1 tsp",
                    "Salt to taste                                ",
                    "Cooking oil                     - 3 to 4 tbsp",
                    "Gram / besan flour (kadalai maavu    - 2 tbsp",
                    "Yoghurt                               - 1 cup",
                    "Kashmiri red chilly pwd               - 1 tsp",
                    " Ginger garlic paste                  - 1 tsp",
                    "Turmeric pwd                          - ¼ tsp",
                    "Garam masala pwd                      - ½ tsp",
                    "Salt as required                             "
            };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, ingredients);
        ListView lv= (ListView)findViewById(R.id.ingredients_list);
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        lv.setAdapter(adapter);
lv.setOnTouchListener(new ListView.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        // Disallow ScrollView to intercept touch events.
                        v.getParent().requestDisallowInterceptTouchEvent(true);
                        break;

                    case MotionEvent.ACTION_UP:
                        // Allow ScrollView to intercept touch events.
                        v.getParent().requestDisallowInterceptTouchEvent(false);
                        break;
                }

                // Handle ListView touch events.
                v.onTouchEvent(event);
                return true;
            }
        });

我使用了多种选择模式,因为我想要一份清单。 我使用setOnTouchListener,因为我的布局中已经有了NestedScrollView。我在另一篇SO帖子上发现了这个。

有没有办法解决我面临的问题并获取列表中的所有项目?

0 个答案:

没有答案
相关问题