在android

时间:2016-06-18 07:56:02

标签: android

我在嵌套的scrollview中创建了水平列表视图。由于垂直滚动操作适用于背景,水平列表视图无法顺畅滚动。我试图使用listview ontouchlistner停止垂直滚动。它的工作正常。但是,如果我触摸水平列表视图,垂直滚动停止。所以我不想在触摸期间停止垂直滚动停止。我想在水平列表视图滚动时停止垂直滚动视图任何人都可以帮我解决这个问题。

try {
        JSONObject jsonRootObject = new JSONObject(data);
        JSONArray jsonArray = jsonRootObject.optJSONArray("new_hotel");

        //Iterate the jsonArray and print the info of JSONObjects
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            //   int id = Integer.parseInt(jsonObject.optString("hotel_id").toString());
            String name = jsonObject.optString("hotel_name").toString();
            final String image = jsonObject.optString("logo").toString();
            Log.d("response", "response -----" + image);

            Movie movie = new Movie();


            movie.sethorizontal1_title(jsonObject.optString("hotel_name").toString());
            movie.sethorizontal1_image(jsonObject.optString("logo").toString());
            movie.sethorizontal1_hotelid(jsonObject.optString("hotel_id").toString());
            movie.sethorizontal1_city(jsonObject.optString("city").toString());
            movieList.add(movie);
            listview1.setAdapter(adapter1);
            listview1.setOnTouchListener(new HorizontalListView.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 HorizontalScrollView touch events.
                    v.onTouchEvent(event);
                    return true;
                }
            });



            adapter1.notifyDataSetChanged();

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

` 这是我的xml代码:

 <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".Home">

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

        <include
            android:id="@+id/toolbar"
            layout="@layout/tool_bar" />
        <android.support.design.widget.CoordinatorLayout

            android:layout_width="match_parent"
            android:layout_height="match_parent"

            android:background="?android:attr/colorBackground">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/materialup_appbar"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@null"


                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                >
                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapse_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"


                    app:expandedTitleTextAppearance="@android:color/transparent"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed"
                    >​

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="300dp"
                        android:src="@drawable/sample"
                        android:scaleType="fitXY"
                        android:id="@+id/homeimage"
                        android:adjustViewBounds="true"
                        app:layout_collapseMode="parallax"
                        app:layout_collapseParallaxMultiplier="1.0"/>
                </android.support.design.widget.CollapsingToolbarLayout>
            </android.support.design.widget.AppBarLayout>

            <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:id="@+id/nest_scrollview"
                android:fillViewport="true"
                android:scrollbarStyle="insideOverlay"
                android:layout_gravity="fill_vertical">

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Hi UserName"
                        android:textSize="30dp"
                        android:gravity="left"
                        android:id="@+id/home_username"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:textSize="30dp"
                        android:gravity="left"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        />
                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="10dp"
                        android:background="@drawable/horizontalline"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="New Hotels"
                        android:textSize="15dp"
                        android:gravity="left"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        />



                    <com.airstar.abservetech.adapter.HorizontalListView
                        android:id="@+id/listview1"
                        android:layout_width="fill_parent"
                        android:layout_height="270dp"
                        android:background="#ddd"
                        android:scrollbarStyle="outsideOverlay"
                        />
                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="10dp"
                        android:background="@drawable/horizontalline"
                        android:layout_marginTop="10dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"/>



                </LinearLayout>

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

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/header"
        app:menu="@menu/drawer" />
</android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:1)

您可以在水平视图中实现RecyclerView,而不是使用第三方库或水平列表视图,如下所示

 <android.support.v7.widget.RecyclerView
                    android:id="@+id/gallery_more"
                    android:layout_width="fill_parent"
                    android:layout_height="250dip"
                    android:layout_below="@+id/Store2"
                    android:layout_marginBottom="25dp">
                </android.support.v7.widget.RecyclerView>

要使其水平,您必须在片段或活动中编写以下内容

linearLayoutManagerHorizontal =
            new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);

    morerecyclerView.setLayoutManager(linearLayoutManagerHorizontal);
    morerecyclerView.setHasFixedSize(true);

您还可以点击以下链接:

blog.nkdroidsolutions.com/android-horizontal-vertical-recyclerview-example/

它可以正常工作而没有任何复杂性