简体   繁体   English

如何将Recyclerview添加到Scrollview?

[英]How To Add Recyclerview To Scrollview?

I have an activity where there are a few Components and a recyclerview. 我有一个活动,那里有一些组件和一个recyclerview。 I want the UI to be such that if I swipe up, I want the whole screen to scroll down; 我希望UI能够使我向上滑动,使整个屏幕向下滚动; not just the recyclerview. 不只是recyclerview。 How do I make this possible? 我如何做到这一点? I have tried by encapsulating all my layouts except the coordinator layout with a scrollview but it doesn't seem to be working. 我已经尝试通过用scrollview封装除协调器布局之外的所有布局,但它似乎无法正常工作。

Java code: Java代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_art_call);

        Fund = (Button) findViewById(R.id.button2);
        /* -------- Obtain the Event ID of the item that the user has selected on the RecyclerView ------*/
        Intent intent = getIntent();
        Artcall_id  = intent.getStringExtra("Artcall_id");
        /* ----------------------------------------------------------------------------------------------*/

        mEventFundRef = FirebaseDatabase.getInstance().getReference().child("Fund");
        mRewardsRef = FirebaseDatabase.getInstance().getReference().child("Rewards").child(Artcall_id);
        rewards_list = (RecyclerView) findViewById(R.id.reward_list);


        rewards_list.setHasFixedSize(true);
        LinearLayoutManager layoutManager =  new LinearLayoutManager(this);
        rewards_list.setLayoutManager(layoutManager);
}

XML code: XML代码:

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 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"
        tools:context=".FundingFlow.SingleArtCall">

   <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/relLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <include
                android:id="@+id/genpage_toolbar"
                layout="@layout/app_bar_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </RelativeLayout>

        <TextView
            android:id="@+id/band_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="24dp"
            android:layout_marginTop="186dp"
            android:text="Sean Roldan and Friends"
            android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
            android:textSize="24sp" />


        <!--Bottom Button -->
        <include layout="@layout/fund_button_layout" />

        <TextView
            android:id="@+id/band_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/band_name"
            android:layout_below="@+id/band_name"
            android:text="Performing in Hyderabad, India." />

        <TextView
            android:id="@+id/started_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignStart="@+id/band_name"
            android:layout_marginTop="245dp"
            android:text="Started by Vishwesh Kirthi"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/genre_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="259dp"
            android:text="Indian Folk and Blues"
            android:textAppearance="@style/TextAppearance.AppCompat.Button" />

        <ImageView
            android:id="@+id/videoView"
            android:layout_width="match_parent"
            android:layout_height="126dp"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/relLayout1"
            android:src="@color/white" />

        <ProgressBar
            android:id="@+id/progressBar2"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="338dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="233dp"
            android:max="100"
            android:progress="60" />

        <TextView
            android:id="@+id/amt_fund_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignStart="@+id/band_name"
            android:layout_marginBottom="191dp"
            android:text="Rs. 7,80,000"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/target_fund_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignStart="@+id/band_name"
            android:layout_marginBottom="162dp"
            android:text="pledged of Rs. 13,00,000" />

        <TextView
            android:id="@+id/total_backers_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/textView35"
            android:layout_alignTop="@+id/amt_fund_txt"
            android:text="120"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/textView35"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignTop="@+id/target_fund_txt"
            android:layout_marginEnd="124dp"
            android:text="backers" />

        <TextView
            android:id="@+id/time_left_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/amt_fund_txt"
            android:layout_toEndOf="@+id/band_name"
            android:text="25"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/time_qt_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/target_fund_txt"
            android:layout_toEndOf="@+id/band_name"
            android:text="days to go" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/time_qt_left"
            android:id="@+id/relLayout2">


            <android.support.v7.widget.RecyclerView
                android:id="@+id/reward_list"
                android:layout_width="354dp"
                android:layout_height="match_parent"
                android:layout_centerInParent="true"
                android:paddingTop="10dp"
                ></android.support.v7.widget.RecyclerView>

        </RelativeLayout>



    </RelativeLayout>


</ScrollView>

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

You should avoid putting a recyclerview inside of a scrollview. 您应该避免将recyclerview放在scrollview中。 When you do the recyclerview will draw all of the elements instead of just what is visible on the screen. 完成后,recyclerview将绘制所有元素,而不是屏幕上可见的所有元素。 I ran into a very similar issue and was able to work around it by using ModelAdapter from FastAdapter . 我遇到了一个非常类似的问题,并且可以通过使用FastAdapter中的ModelAdapter来解决它。 The idea is you create 1 adapter type that contains the content for your textviews and another type to hold your reward_list items. 想法是您创建1个适配器类型,其中包含您的textviews的内容,另一种类型用于保存您的reward_list项目。

var modelAdapter: ModelAdapter<RewardModel, GenericRewardItem>

modelAdapter = ModelAdapter({
    when (it) {
        is RewardModel.BodyContent -> BodyContentItem(it)
        is RewardModel.Reward -> RewardItem(it)
    }
})

val fastAdapter: FastAdapter<GenericJobDetailItem> = 
FastAdapter.with(listOf(modelAdapter))
recycler_view.apply {
    layoutManager = LinearLayoutManager(view.context)
    hasFixedSize()
    adapter = fastAdapter
}

val items = mutableListOf<RewardModel>()
items.add(RewardModel.BodyContent(/* All your body content */))
rewards.forEach {
    items.add(RewardModel.Reward(/* reward item */))
}

modelAdapter.setNewList(items)

FastAdapter ia a really nice library, I use it for all my adapter views now. FastAdapter是一个非常不错的库,我现在将其用于所有适配器视图。

Change your RecyclerViews 更改您的RecyclerViews

layout_height:wrap_content

Then in your code 然后在你的代码中

yourRecyclerview.setNestedScrollingEnabled(false);

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

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