如何在滚动Receierview Android的同时滚动父布局

人气:674 发布:2022-10-16 标签: android android-recyclerview scrollview

问题描述

请帮助我解决以下情况。

我正在开发一个应用程序,在其中一个页面中,我有一个带有背景图像的LinearLayout和带有名称列表的RecillerView。我需要的是,当我向上滚动RecclerView时,我需要上面的LinearLayout也向上移动,以便回收视图中的列表不会位于上面的LinearLayout下面,而当我向下滚动ReccyerView时,我需要向下滚动上面的LinearLayout,以便我们可以完整地看到图像。

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

下面是布局:

<?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>

以下是我在相应的Java类中使用的代码:

    @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,因为循环视图本身就有它。我在这里写的是一步一步的指南,将标题(不是粘性的)放在您的Recloop View的顶部,以便它们可以一起滚动:

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>

我们将其命名为Header_Layout

2-您需要实现另一个名为getItemViewType的RecrecerAdapter方法,它的输出将作为第二个参数放在CreateViewHolder(ViewGroup父级,int viewType)中。这里,您可以为所需的每种视图扩展布局(对我来说,这两种视图如下所示):

@Override
public int getItemViewType(int position){
    if(position == 0){
        return 0;
    } else {
        return 1;
    }
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView;
    if(viewType==1){
        itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.report_row_layout, parent, false);
    } else {
        itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.report_header_layout, parent, false);
    }

    return new MyViewHolder(itemView);
}

请注意,我的第一个职位(表头)不同,其他职位相同,您可以为多个职位提供多个视图。

3-如果需要,您应该更改您的onBindViewHolder,在我的情况下,我需要让它对我的第一个职位没有任何作用

4-移除ScrollView并按位置实现布局,您的主布局应该如下所示:

<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在做什么,但如果您认为不需要,请将其删除。如果需要,您可以将页眉设置为LinearLayout或其他任何形式。

希望这能有所帮助。

386