Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我们如何在滚动recyclerview android时滚动父布局_Android_Scrollview_Android Recyclerview - Fatal编程技术网

我们如何在滚动recyclerview android时滚动父布局

我们如何在滚动recyclerview android时滚动父布局,android,scrollview,android-recyclerview,Android,Scrollview,Android Recyclerview,请帮我解决以下情况 我正在开发一个应用程序,其中在一个页面中,我有一个带有背景图像的Linearlayout和带有姓名列表的RecyclerView。我需要的是,当我向上滚动RecyclerView时,我需要上面的Linearlayout也向上移动,以便RecyclerView中的列表不在上面的Linearlayout之下,当我向下滚动RecyclerView时,我需要需要上面的线性布局向下滚动,以便我们可以完全看到图像 我已经做的是使用了recyclerview的setOnScrollList

请帮我解决以下情况

我正在开发一个应用程序,其中在一个页面中,我有一个带有背景图像的Linearlayout和带有姓名列表的RecyclerView。我需要的是,当我向上滚动RecyclerView时,我需要上面的Linearlayout也向上移动,以便RecyclerView中的列表不在上面的Linearlayout之下,当我向下滚动RecyclerView时,我需要需要上面的线性布局向下滚动,以便我们可以完全看到图像

我已经做的是使用了recyclerview的setOnScrollListener,在onScroll()函数中,我得到了向下滚动和向上滚动事件。但是现在我被困在了如何继续下去的问题上

下面是布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <include layout="@layout/toolbar"
        android:id="@+id/toolbar" />

    <android.support.v7.widget.CardView
        android:id="@+id/cardview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:layout_margin="8dp"
        card_view:cardBackgroundColor="@android:color/white"
        card_view:cardCornerRadius="8dp">

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/scrollview"
                android:fillViewport="true">

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

                        <LinearLayout
                            android:id="@+id/map"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:orientation="vertical"
                            android:background="@drawable/hydeparkmap"
                            android:layout_weight="3" ></LinearLayout>


                        <android.support.v7.widget.RecyclerView
                            android:id="@+id/recycler_view"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_weight="2"
                            android:scrollbars="vertical" />

                    </LinearLayout>

            </ScrollView>

    </android.support.v7.widget.CardView>

</RelativeLayout>
    @InjectView(R.id.scrollview)
    ScrollView scrollview;

    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shoplist);
        ButterKnife.inject(this);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setTitle("ShopList");

        scrollview.setVerticalScrollBarEnabled(true);
        lm=new LinearLayoutManager(ShopListActivity.this);
        recyclerView.setLayoutManager(lm);
        firstVisibleInListview = lm.findFirstVisibleItemPosition();
        adapter = new RecyclerViewAdapter(ShopListActivity.this, getData());
        recyclerView.setAdapter(adapter);

        recyclerView.addOnItemTouchListener(
                new RecyclerItemClickListener(ShopListActivity.this, new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {

                        Intent intent1 = new Intent(ShopListActivity.this, ShopProductListActivity.class);
                        intent1.putExtra("position", String.valueOf(position));
                        intent1.putExtra("shopname", it.get(position).getTitle());
                        intent1.putExtra("shopimage", String.valueOf(it.get(position).getImgIcon()));
                        intent1.putExtra("subcategory", subcategory);
                        startActivity(intent1);

                    }
                })
        );
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int currentFirstVisible = lm.findFirstVisibleItemPosition();

                if(currentFirstVisible > firstVisibleInListview){
                   Toast.makeText(getBaseContext(),"Scrolled up ",Toast.LENGTH_SHORT).show();
                    scrollview.fullScroll(ScrollView.FOCUS_UP);
               }
                else
                    Toast.makeText(getBaseContext(),"Scrolled down ",Toast.LENGTH_SHORT).show();

                firstVisibleInListview = currentFirstVisible;
            }
        });

    }

所以,如果我完全理解你的问题,你可以看到一些应该滚动的视图 RcyclerView。 如果是这样,在这种情况下,您应该删除您的ScrollView,因为RecyclerView本身就有它。我在这里写的是一步一步的指南,在你的RecyclerView顶部放一个标题(不是粘性的),这样它们就可以一起滚动:

1-首先,你需要创建一个包含你的标题的布局,在我的例子中,我有一个标题的图像,所以它看起来像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@mipmap/winter"/>

</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<include layout="@layout/toolbar"
    android:id="@+id/toolbar" />

<android.support.v7.widget.CardView
    android:id="@+id/cardview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar"
    android:layout_margin="8dp"
    card_view:cardBackgroundColor="@android:color/white"
    card_view:cardCornerRadius="8dp">

    <android.support.v7.widget.RecyclerView
         android:id="@+id/recycler_view"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />

</android.support.v7.widget.CardView>

</RelativeLayout>
请注意,我的第一个位置(标题)不同,其他位置相同。您可以对多个位置使用多个视图

3-如果需要的话,你应该更换你的onBindViewHolder在我的情况下,我需要让它对我的第一个职位不起任何作用

4-删除ScrollView并在位置中实现布局,主布局应如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@mipmap/winter"/>

</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<include layout="@layout/toolbar"
    android:id="@+id/toolbar" />

<android.support.v7.widget.CardView
    android:id="@+id/cardview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar"
    android:layout_margin="8dp"
    card_view:cardBackgroundColor="@android:color/white"
    card_view:cardCornerRadius="8dp">

    <android.support.v7.widget.RecyclerView
         android:id="@+id/recycler_view"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />

</android.support.v7.widget.CardView>

</RelativeLayout>

我不知道您的CardView正在做什么,但如果您认为不需要,请将其删除。如果需要,您可以将标题设置为线性布局或其他任何形式


希望这有帮助

在此视图中使用视图类型。提出两种观点,即:。一个带有标题,另一个带有要显示的内容。其他的东西都会放在另一个角度。现在我正在检查其他东西,所以不写代码。但是给出了链接。结账