nestedscrollview 内的两个 Recyclerview

Posted

技术标签:

【中文标题】nestedscrollview 内的两个 Recyclerview【英文标题】:Two Recycler views inside nested scroll view 【发布时间】:2019-03-21 23:18:16 【问题描述】:

我有一个嵌套滚动视图,在其中,我有一个包含两个recycler views 的线性布局。现在的问题是第一个recycler view 并没有完全扩展。即第一个 recycler view 列表中有 5 个项目,但如果我放置包装内容,它只会显示第一个项目。由于包装内容,这应该最多扩展到 5 个项目,但它不起作用。如果我将高度固定为200DP300dp,那么它将开始显示更多达到该高度的项目。但是,我想显示第一个 recycler view 的所有项目,然后当我滚动它时,它应该在第一个 recycler view 项目结束后开始第二个 recycler view 项目。

我的布局是:

<?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"
    android:layout_
    android:layout_
    android:fillViewport="true">

    <LinearLayout
        android:layout_
        android:layout_
        android:background="@color/colorPrimary"
        android:layout_marginTop="60dp"
        android:orientation="vertical">

        <com.sooryen.customview.CustomTextView
            android:layout_
            android:layout_
            android:layout_margin="@dimen/spacing_normal"
            android:text="@string/my_booking_latest_booking_lable"
            app:font="avenirltstd_medium" />

        <include layout="@layout/layout_divider" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_ongoing_bookings"
            android:visibility="visible"
            android:layout_
            android:layout_
            app:layoutManager="android.support.v7.widget.LinearLayoutManager" />

        <com.sooryen.customview.CustomTextView
            android:layout_
            android:layout_
            android:visibility="visible"
            android:layout_margin="@dimen/spacing_normal"
            android:text="@string/my_booking_past_booking_lable"
            app:font="avenirltstd_medium" />

        <include layout="@layout/layout_divider" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv_past_bookings"
            android:visibility="visible"
            android:layout_
            android:layout_
            android:nestedScrollingEnabled="false"
            android:layout_marginBottom="@dimen/spacing_extra_small"
            android:scrollbars="none"
            app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
        <View
            android:layout_
            android:layout_ />
    </LinearLayout>
</android.support.v4.widget.NestedScrollView> 

请帮忙。我做错了什么。我从互联网上尝试了很多方法,但没有任何效果。这是此屏幕的屏幕截图

LABEL latest bookings 有 5 项,但它只显示一项,并且没有扩展到 5 项。但是,第二个 LABEL Past bookings 运行良好,并在我向上滚动到末尾时显示所有项目。

我的java文件是:

public class MyBookingActivity extends BaseToolBarActivity implements 
    SwipeRefreshLayout.OnRefreshListener 

    int cancelValue;
    private SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.rv_ongoing_bookings)
    RecyclerView mRVOngoingBookings;
    private List<MyBookingsApi.ApiResponse.Datum> mCourtDataList = new ArrayList<>();
    @BindView(R.id.rv_past_bookings)
    RecyclerView mRVPastBookings;
    private BaseRecyclerAdapter<MyBookingsApi.ApiResponse.Datum, MyBookingsViewHolder> mBaseRecyclerAdapterMyBookings = new BaseRecyclerAdapter<MyBookingsApi.ApiResponse.Datum, MyBookingsViewHolder>(MyBookingsViewHolder.class, R.layout.layout_list_item_my_bookings) 

        @Override
        protected void populateViewHolder(MyBookingsViewHolder itemViewHolder, final MyBookingsApi.ApiResponse.Datum model, int position) 
            String date = null;
            try 
                date = new SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH).format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).parse(model.getDate()));
             catch (ParseException e) 
                e.printStackTrace();
            
            String sessions = "N.A.";
            int CancelledValue = model.getCancelled();
            if (CancelledValue == 0)
            
                itemViewHolder.PaymentYesNo.setText(getString(R.string.booking_detail_no));
                itemViewHolder.PaymentYesNo.setBackgroundResource(R.drawable.round_corner_linear_green);
            
            else
            
                itemViewHolder.PaymentYesNo.setText(getString(R.string.booking_detail_yes));
                itemViewHolder.PaymentYesNo.setBackgroundResource(R.drawable.round_corner_linear_red);
            
            String paymentConfirmed  = model.getPayment_confirmed();
            String paymentType = model.getPaymentType();
            if (paymentConfirmed.equals("true"))
            
                itemViewHolder.PaymentStatus.setText("Confirmed");

            
            else
            
                itemViewHolder.PaymentStatus.setText("Pending");
            
            if (CancelledValue == 0 && paymentConfirmed.equals("false") && paymentType.equals("cash"))
            

                itemViewHolder.DepositThisAmount.setText("Deposit " + String.valueOf(model.getDepositAmount()) + " SAR in " +
                String.valueOf(model.getDepositTimeAllowed()) + " HRS");
            
            else
            
                itemViewHolder.DepositThisAmount.setVisibility(View.GONE);
            
            if (model.getSession() != null)
                sessions = model.getSession();
            itemViewHolder.mCustomTextViewCourtBookingTime.setText(date + " " + sessions);
            if (getResources().getConfiguration().locale.equals(new Locale("ar"))) 
                itemViewHolder.mCustomTextViewCourtPrice.setText(model.getPrice() + " " + "ريال");
                itemViewHolder.mCustomTextViewCourtCancellationCutoff.setText("يمكن الغاء الحجز قبل المباراة" + " : " + model.getCancelCutoffTime());
             else 
                itemViewHolder.mCustomTextViewCourtPrice.setText(getString(R.string.price, model.getPrice()));
                itemViewHolder.mCustomTextViewCourtCancellationCutoff.setText(getString(R.string.cancellation_cutoff, model.getCancelCutoffTime()));
            
            String cancelshow = model.getCancelshow();
            if (cancelshow.equals("true"))
            
                cancelValue = 1;
                itemViewHolder.mAppCompatImageViewMoreHoriz.setVisibility(View.VISIBLE);
            
            else
            
                cancelValue = 0;
                itemViewHolder.mAppCompatImageViewMoreHoriz.setVisibility(View.GONE);
            

            itemViewHolder.mCustomTextViewCourtName.setText(model.getCourtName());
            itemViewHolder.mAppCompatImageViewMoreHoriz.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    PopupMenu popup = new PopupMenu(MyBookingActivity.this, v);
                    popup.getMenu().add(0, 1, 0, getString(R.string.my_booking_cancel));


                    //adding click listener
                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() 
                        @Override
                        public boolean onMenuItemClick(MenuItem item) 
                            switch (item.getItemId()) 
                                case 1:
                                    cancelBooking(model.getId());
                                    break;
                                //case 1:
                                  //  deleteBooking(model.getId());
                            
                            return false;
                        
                    );
                    //displaying the popup
                    popup.show();
                
            );
            itemViewHolder.mReaRelativeLayoutMyBooking.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    //launchActivity(MyBookingDetailActivity.getActivityIntent(MyBookingActivity.this, model.getId(), "MyBooking", 1));
                
            );

            itemViewHolder.mAppCompatImageViewRightArrow.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    launchActivity(MyBookingDetailActivity.getActivityIntent(MyBookingActivity.this, model.getId(), "MyBooking", 1));
                
            );
        

    ;
    private BaseRecyclerAdapter<PastBookingsApi.ApiResponse.Datum, MyPastBookingsViewHolder> mBaseRecyclerAdapterMyPastBookings = new BaseRecyclerAdapter<PastBookingsApi.ApiResponse.Datum, MyPastBookingsViewHolder>(MyPastBookingsViewHolder.class, R.layout.layout_list_item_my_bookings) 

        @Override
        protected void populateViewHolder(MyPastBookingsViewHolder itemViewHolder, final PastBookingsApi.ApiResponse.Datum model, int position) 
            String date = null;
            try 
                date = new SimpleDateFormat("yyyy/MM/dd", Locale.ENGLISH).format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH).parse(model.getDate()));
             catch (ParseException e) 
                e.printStackTrace();
            
            String sessions = "N.A.";
            int CancelledValue = model.getCancelled();
            if (CancelledValue == 0)
            
                itemViewHolder.PaymentYesNo.setText(getString(R.string.booking_detail_no));
                itemViewHolder.PaymentYesNo.setBackgroundResource(R.drawable.round_corner_linear_green);
            
            else
            
                itemViewHolder.PaymentYesNo.setText(getString(R.string.booking_detail_yes));
                itemViewHolder.PaymentYesNo.setBackgroundResource(R.drawable.round_corner_linear_red);
            
            itemViewHolder.DepositThisAmount.setVisibility(View.GONE);
            int paymentConfirmed  = model.getPaymentConfirmed();
            if (paymentConfirmed == 1)
            
                itemViewHolder.PaymentStatus.setText("Confirmed");

            
            else
            
                itemViewHolder.PaymentStatus.setText("Pending");
            
            if (model.getSession() != null)
                sessions = model.getSession();
            itemViewHolder.mCustomTextViewCourtBookingTime.setText(date + " " + sessions);
            if (getResources().getConfiguration().locale.equals(new Locale("ar"))) 
                itemViewHolder.mCustomTextViewCourtPrice.setText(model.getPrice() + " " + "ريال");
             else 
                itemViewHolder.mCustomTextViewCourtPrice.setText(getString(R.string.price, model.getPrice()));
            
            itemViewHolder.mCustomTextViewCourtCancellationCutoff.setText(getString(R.string.cancellation_cutoff, "0"));
            itemViewHolder.mCustomTextViewCourtName.setText(model.getCourtName());
            itemViewHolder.mAppCompatImageViewMoreHoriz.setVisibility(View.VISIBLE);
            itemViewHolder.mAppCompatImageViewMoreHoriz.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    PopupMenu popup = new PopupMenu(MyBookingActivity.this, v);
                    popup.getMenu().add(0, 1, 0, getString(R.string.notification_delete));

                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() 
                        @Override
                        public boolean onMenuItemClick(MenuItem item) 
                            switch (item.getItemId()) 
                                case 1:
                                    deleteBooking(model.getId());
                                    break;
                            
                            return false;
                        
                    );
                    popup.show();
                
            );
            itemViewHolder.mAppCompatImageViewRightArrow.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    launchActivity(MyBookingDetailActivity.getActivityIntent(MyBookingActivity.this, model.getId(), "MyBooking", 1));
                
            );
            itemViewHolder.mReaRelativeLayoutMyBooking.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                   // launchActivity(MyBookingDetailActivity.getActivityIntent(MyBookingActivity.this, model.getId(), "MyBooking", 1));
                
            );
        
    ;

    public static Intent getActivityIntent(Context context) 
        return new Intent(context, MyBookingActivity.class);
    

    private void cancelBooking(final int id) 

    

    private void deleteBooking(final int id) 

    

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle(getString(R.string.title_activity_my_booking));
        setUpRecyclerView();
        setUpEmptyAdapter();
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
        swipeRefreshLayout.setOnRefreshListener(this);
        getMyBooking();
        getMyPastBooking();
        /*swipeRefreshLayout.post(new Runnable() 
                                    @Override
                                    public void run() 
                                        swipeRefreshLayout.setRefreshing(true);

                                        getMyBooking();
                                    
                                
        );*/
    

    private void setUpEmptyAdapter() 
        mRVPastBookings.setAdapter(new BaseEmptyRecyclerAdapter<EmptyViewHolderRefresh>(EmptyViewHolderRefresh.class, R.layout.layout_place_holderrefresh) 
            @Override
            protected void populateEmptyViewHolder(EmptyViewHolderRefresh viewHolder) 
                viewHolder.mAppCompatImageView.setImageResource(R.drawable.no_booking_place_holder);
                viewHolder.mCustomTextViewTitle.setText(R.string.no_bookings_available_title);
                viewHolder.mCustomTextViewDescription.setText(R.string.no_bookings_available_description);
                viewHolder.refreshText.setText(R.string.swipe);
                //viewHolder.mCustomButton.setText(R.string.try_again);
                viewHolder.mCustomButton.setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View v) 
                        //getMyPastBooking();
                    
                );
            
        );

        mRVOngoingBookings.setAdapter(new BaseEmptyRecyclerAdapter<EmptyViewHolderRefresh>(EmptyViewHolderRefresh.class, R.layout.layout_place_holderrefresh) 
            @Override
            protected void populateEmptyViewHolder(EmptyViewHolderRefresh viewHolder) 
                viewHolder.mAppCompatImageView.setImageResource(R.drawable.no_booking_place_holder);
                viewHolder.mCustomTextViewTitle.setText(R.string.no_bookings_available_title);
                viewHolder.mCustomTextViewDescription.setText(R.string.no_bookings_available_description);
                //viewHolder.mCustomButton.setText(R.string.try_again);
                viewHolder.refreshText.setText(R.string.swipe);
                viewHolder.mCustomButton.setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View v) 
                       // getMyBooking();
                    
                );
            
        );
    

    private void setUpRecyclerView() 
         mRVOngoingBookings.setAdapter(mBaseRecyclerAdapterMyBookings);
        DividerItemDecoration mDividerItemDecorationMyBookings = new DividerItemDecoration(this, LinearLayoutManager.VERTICAL);
        mDividerItemDecorationMyBookings.setDrawable(ContextCompat.getDrawable(this, R.drawable.divider));
        mRVOngoingBookings.addItemDecoration(mDividerItemDecorationMyBookings);

        mRVPastBookings.setAdapter(mBaseRecyclerAdapterMyPastBookings);
        DividerItemDecoration mDividerItemDecorationMyPastBookings = new DividerItemDecoration(this, LinearLayoutManager.VERTICAL);
        mDividerItemDecorationMyPastBookings.setDrawable(ContextCompat.getDrawable(this, R.drawable.divider));
        mRVPastBookings.addItemDecoration(mDividerItemDecorationMyPastBookings);

    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        if (item.getItemId() == android.R.id.home) 
            finishWithResultCancel();
        
        return super.onOptionsItemSelected(item);
    

    @Override
    protected int getMenuResource() 
        return RESOURCE_NO_MENU;
    

    @Override
    protected int getLayoutResource() 
        return R.layout.activity_my_booking;
    

    @Override
    public void onResume() 
        super.onResume();
        registerEventBus();
        //getMyBooking();
        //getMyPastBooking();
    

    @Override
    public void onPause() 
        super.onPause();
        unRegisterEventBus();
    

    private void getMyPastBooking() 
      /// swipeRefreshLayout.setRefreshing(true);
        final ProgressDialog pd = new ProgressDialog(MyBookingActivity.this);
        pd.setMessage("Please wait..");
        pd.show();
        Handler mHandler = new Handler();
        mHandler.postDelayed(new Runnable() 

            @Override
            public void run() 
                pd.dismiss();
                startApiService(PastBookingsApi.getServiceIntent(MyBookingActivity.this, getToken()));
            

        , 800);

    

    private void getMyBooking() 
       // swipeRefreshLayout.setRefreshing(true);
        final ProgressDialog pd = new ProgressDialog(MyBookingActivity.this);
        pd.setMessage("Please wait..");
        pd.show();
        Handler mHandler = new Handler();
        mHandler.postDelayed(new Runnable() 

            @Override
            public void run() 
                pd.dismiss();
                startApiService(MyBookingsApi.getServiceIntent(MyBookingActivity.this, getToken()));
            

        , 2000);

        //swipeRefreshLayout.setRefreshing(false);
    

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(BaseApi.EventBusMessage eventBusMessage) 
        switch (eventBusMessage.event) 
            case BaseApi.EventBusMessage.EVENT_SHOW_PROGRESS_DIALOG:
                showProgressDialog();
                break;
            case BaseApi.EventBusMessage.EVENT_HIDE_PROGRESS_DIALOG:
                hideProgressDialog();
                break;
            default:
                if (eventBusMessage.object instanceof MyBookingsApi.ApiResponse)
                    parseMyBookingsSuccessResponse((MyBookingsApi.ApiResponse) eventBusMessage.object);
                else if (eventBusMessage.object instanceof PastBookingsApi.ApiResponse) 
                    parsePastBookingsSuccessResponse((PastBookingsApi.ApiResponse) eventBusMessage.object);
                 else if (eventBusMessage.object instanceof BaseApi.DefaultErrorResponse) 
                    parseErrorResponse((BaseApi.DefaultErrorResponse) eventBusMessage.object);
                 else if (eventBusMessage.object instanceof BaseApi.Error500Response) 
                    //parseError500Response((BaseApi.Error500Response) eventBusMessage.object);
                 else if (eventBusMessage.object instanceof MyBookingCancelApi.ApiResponse) 
                    parseCancelBookingSuccessResponse((MyBookingCancelApi.ApiResponse) eventBusMessage.object);
                 else if (eventBusMessage.object instanceof MyBookingDeleteApi.ApiResponse) 
                    parseDeleteBookingSuccessResponse((MyBookingDeleteApi.ApiResponse) eventBusMessage.object);
                
                break;
        
    

    private void parseDeleteBookingSuccessResponse(MyBookingDeleteApi.ApiResponse apiResponse) 
        showSnackBar(apiResponse.getMessage());
        getMyPastBooking();
    

    private void parseCancelBookingSuccessResponse(final MyBookingCancelApi.ApiResponse apiResponse) 
        showSnackBar(apiResponse.getMessage());
        getMyBooking();
        /*final ProgressDialog pd = new ProgressDialog(MyBookingActivity.this);
        pd.setMessage("Please wait..");
        pd.show();
        Handler mHandler = new Handler();
        mHandler.postDelayed(new Runnable() 

            @Override
            public void run() 
                pd.dismiss();
                startApiService(MyBookingDeleteApi.getServiceIntent(MyBookingActivity.this, getToken(), apiResponse.getData().getId()));
            

        , 800);*/

    

    private void parsePastBookingsSuccessResponse(PastBookingsApi.ApiResponse apiResponse) 
        List<PastBookingsApi.ApiResponse.Datum> datumList = apiResponse.getData();
        if (datumList.size() > 0) 
            Collections.sort(datumList, new Comparator<PastBookingsApi.ApiResponse.Datum>()
            
                @Override
                public int compare(PastBookingsApi.ApiResponse.Datum o1, PastBookingsApi.ApiResponse.Datum o2) 
                    return o1.getId() > o2.getId() ? -1 : (o1.getId() < o2.getId() ) ? 1 : 0;
                
            );
            mRVPastBookings.setAdapter(mBaseRecyclerAdapterMyPastBookings);
            mBaseRecyclerAdapterMyPastBookings.setItems(datumList);
         else 
            mRVPastBookings.setAdapter(new BaseEmptyRecyclerAdapter<EmptyViewHolderRefresh>(EmptyViewHolderRefresh.class, R.layout.layout_place_holderrefresh) 
                @Override
                protected void populateEmptyViewHolder(EmptyViewHolderRefresh viewHolder) 
                    viewHolder.mAppCompatImageView.setImageResource(R.drawable.no_booking_place_holder);
                    viewHolder.mCustomTextViewTitle.setText(R.string.no_bookings_available_title);
                    viewHolder.mCustomTextViewDescription.setText(R.string.no_bookings_available_description);
                   // viewHolder.mCustomButton.setText(R.string.try_again);
                    viewHolder.refreshText.setText(R.string.swipe);
                    viewHolder.mCustomButton.setOnClickListener(new View.OnClickListener() 
                        @Override
                        public void onClick(View v) 
                           // getMyPastBooking();
                        
                    );
                
            );
        
    

    private void parseMyBookingsSuccessResponse(MyBookingsApi.ApiResponse apiResponse) 
       // mCourtDataList = apiResponse.getData();
        List<MyBookingsApi.ApiResponse.Datum> datumList = apiResponse.getData();
        if (datumList.size() > 0) 
            Collections.sort(datumList, new Comparator<MyBookingsApi.ApiResponse.Datum>()
            
                @Override
                public int compare(MyBookingsApi.ApiResponse.Datum o1, MyBookingsApi.ApiResponse.Datum o2) 
                    return o1.getId() > o2.getId() ? -1 : (o1.getId() < o2.getId() ) ? 1 : 0;
                
            );

            mRVOngoingBookings.setAdapter(mBaseRecyclerAdapterMyBookings);
            mBaseRecyclerAdapterMyBookings.setItems(datumList);
         else 
            mRVOngoingBookings.setAdapter(new BaseEmptyRecyclerAdapter<EmptyViewHolderRefresh>(EmptyViewHolderRefresh.class, R.layout.layout_place_holderrefresh) 
                @Override
                protected void populateEmptyViewHolder(EmptyViewHolderRefresh viewHolder) 
                    viewHolder.mAppCompatImageView.setImageResource(R.drawable.no_booking_place_holder);
                    viewHolder.mCustomTextViewTitle.setText(R.string.no_bookings_available_title);
                    viewHolder.mCustomTextViewDescription.setText(R.string.no_bookings_available_description);
                    //viewHolder.mCustomButton.setText(R.string.try_again);
                    viewHolder.refreshText.setText(R.string.swipe);
                    viewHolder.mCustomButton.setOnClickListener(new View.OnClickListener() 
                        @Override
                        public void onClick(View v) 
                            //getMyBooking();
                        
                    );
                
            );
        
    

    private void parseError500Response(BaseApi.Error500Response error500Response) 
        showAlertDialog(R.string.session_timeout_title, R.string.session_timeout_message, new DialogInterface.OnClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int which) 
                logout();
            
        );
    

    private void parseErrorResponse(BaseApi.DefaultErrorResponse defaultErrorResponse) 
        showSnackBar(defaultErrorResponse.getMessage());
    

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void noInternet(String message) 
        mRVOngoingBookings.setAdapter(new BaseEmptyRecyclerAdapter<EmptyViewHolderRefresh>(EmptyViewHolderRefresh.class, R.layout.layout_place_holderrefresh) 
            @Override
            protected void populateEmptyViewHolder(EmptyViewHolderRefresh viewHolder) 
                viewHolder.mAppCompatImageView.setImageResource(R.drawable.no_internet_placeholder);
                viewHolder.mCustomTextViewTitle.setText(R.string.connection_problem_title);
                viewHolder.mCustomTextViewDescription.setText(R.string.connection_problem_description);
                viewHolder.refreshText.setText(R.string.swipe);
                //viewHolder.mCustomButton.setText(R.string.try_again);
                viewHolder.mCustomButton.setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View v) 
                       // getMyBooking();
                    
                );
            
        );
        mRVPastBookings.setAdapter(new BaseEmptyRecyclerAdapter<EmptyViewHolderRefresh>(EmptyViewHolderRefresh.class, R.layout.layout_place_holderrefresh) 
            @Override
            protected void populateEmptyViewHolder(EmptyViewHolderRefresh viewHolder) 
                viewHolder.mAppCompatImageView.setImageResource(R.drawable.no_internet_placeholder);
                viewHolder.mCustomTextViewTitle.setText(R.string.connection_problem_title);
                viewHolder.mCustomTextViewDescription.setText(R.string.connection_problem_description);
                viewHolder.refreshText.setText(R.string.swipe);
                //viewHolder.mCustomButton.setText(R.string.try_again);
                viewHolder.mCustomButton.setOnClickListener(new View.OnClickListener() 
                    @Override
                    public void onClick(View v) 
                       // getMyPastBooking();
                    
                );
            
        );
    

activity_my_booking 布局是:

 <?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe_refresh_layout"
    android:layout_
    android:layout_>

    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/coordinatorLayout"
        android:layout_
        android:layout_>

        <include layout="@layout/layout_toolbar" />

        <include layout="@layout/content_my_booking" />
    </android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.SwipeRefreshLayout>

【问题讨论】:

你的实现太错误了!在这种情况下,您不应该使用两个 RecyclerView。您可以在 RecyclerView 的 Adapter 中使用不同的视图类型来实现相同的效果,并且它会以更好的方式工作。 @hardartcore 你能解释一下吗 请注意,千万不要在 NestedScrollView 中使用 recyclerview,否则会有 UI 性能问题。 问题是否解决了? 【参考方案1】:

将第一个RecyclerView的高度设置为wrap_content并将setNestedScrollingEnabled设置为false

RecyclerView rv_ongoing_bookings= (RecyclerView) findViewById(R.id.rv_ongoing_bookings);
rv_ongoing_bookings.setNestedScrollingEnabled(false);

【讨论】:

不,它不工作。回收站视图仍然只显示第一个项目并且没有扩展到它的全高。使用这一行“app:layoutManager="android.support.v7.widget.LinearLayoutManager"的目的是什么。这是问题的原因吗? 用于设置recyclerview的布局管理器,如果你也在java中设置布局管理器,这里就不用说了。 如果你愿意,你可以删除该行和recyclerView.setLayoutManager(new LinearLayoutManager(this)); 我在 java 代码中使用这三行 DividerItemDecoration mDividerItemDecorationMyBookings = new DividerItemDecoration(this, LinearLayoutManager.VERTICAL); mDividerItemDecorationMyBookings.setDrawable(ContextCompat.getDrawable(this, R.drawable.divider)); mRVOngoingBookings.addItemDecoration(mDividerItemDecorationMyBookings); 那是在项目之间添加分隔线【参考方案2】:

要实现它,我的方法是自定义我的 recyclerView 适配器以充当部分标题回收器视图,如下所示: https://android-arsenal.com/details/1/4495

【讨论】:

【参考方案3】:

以下代码将使回收站视图可扩展...

public class ExpandableRecyclerView extends RecyclerView

private boolean expanded = false;

public ExpandableRecyclerView(Context context, AttributeSet attrs, int defStyle)

    super(context, attrs, defStyle);


public ExpandableRecyclerView(Context context)

    super(context);


public ExpandableRecyclerView(Context context, AttributeSet attrs)

    super(context, attrs);


public boolean isExpanded()

    return expanded;


@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

    if (isExpanded())
    
        int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    
    else
    
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    


public void setExpanded(boolean expanded)

    this.expanded = expanded;


用法...以您使用回收站视图的方式使用它... 只需设置recyclerView.setExpanded(true);

你可以直接在scrollview里面使用...

【讨论】:

我必须将此代码放在新类中,然后像 mRvOngoingBookings.setExpanded(true); ? 如果可能的话,为两个回收站视图替换并使用这个类。然后将两个回收站视图的 setExpandable 设置为 true ......它与回收站视图相同......它可能会帮助你...... . 好吧,让我试试这个【参考方案4】:

因此,这并不是您问题的真正答案,它更像是使用RecyclerView 的建议,因为它应该取决于您在问题中包含的屏幕截图。 你真的不需要创建两个RecyclerView 来实现你需要的布局。据我了解,您想显示一些项目并根据某些内容放置带有文本的分隔符(我可以看到预订日期)。

RecyclerView.Adapter 中有getItemViewType,您可以在其中根据某些变量返回一个int 值。例如,您的getItemViewType 应该如下所示:

@Override
public int getItemViewType(int position) 
    MyObject myObject = mItems.get(position);
    // myObject.isSeparator() is a sample function which helps you identify it you should show construct a booking view or separator
    if(myObject.isSeparator())
        // this variable is declared inside your Adapter ->
        // private static final int VIEW_TYPE_SEPARATOR = 1;
        return VIEW_TYPE_SEPARATOR;
    
    // private static final int VIEW_TYPE_BOOKING = 0;
    return VIEW_TYPE_BOOKING;

根据这些值,您需要创建两个ViewHolder

@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
    if (VIEW_TYPE_SEPARATOR == viewType) 
       new SeparatorViewHolder(/* Some params which you need here */);
    
    return new BookingViewHolder(/* Some other params which you need here */);

还有->

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) 
    if (VIEW_TYPE_SEPARATOR == getItemViewType(position)) 
        // Populate SeparatorViewHolder
     else 
        // Populate BookingViewHolder
    

最后,您应该使用您需要确定该项目是预订项目还是分隔符的对象填充您的List

我希望你明白了主要的想法。

【讨论】:

以上是关于nestedscrollview 内的两个 Recyclerview的主要内容,如果未能解决你的问题,请参考以下文章

SwipeRefreshLayout 内的 NestedScrollView 内的 RecyclerView 不能平滑滚动

NestedScrollView 内的 RecyclerView ScrollListener

无法滚动到 NestedScrollView 内的 RecyclerView 中的项目

NestedScrollView 内的回收器视图导致滚动从中间开始

NestedScrollView内部的RecyclerView ViewHolder创建

NestedScrollView 内的 RecyclerView :使水平滚动更容易