当前位置: 首页 > 知识库问答 >
问题:

Appbarlayout和recyclerview的平滑滚动

白浩荡
2023-03-14

我想实现像Google Play App一样的平滑滚动行为。我尝试过这里提到的解决方案:

  1. 解决方案1
  2. 解决方案2
  3. 解决方案3

但所有上述解决方案都没有像在Google Play应用程序中那样给出预期的结果。以下是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:id="@+id/layoutContent"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:background="@android:color/transparent"
    app:elevation="0dp">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

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

            <View
                android:id="@+id/view1"
                android:layout_width="match_parent"
                android:layout_height="125dp"
                android:background="@android:color/transparent" />

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:paddingLeft="24dp"
                android:paddingRight="12dp" />
        </RelativeLayout>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_margin="@dimen/fab_margin"
    app:backgroundTint="@android:color/white"
    app:layout_anchor="@id/recycler_view"
    app:layout_anchorGravity="bottom|right|end"
    app:srcCompat="@drawable/ic_add_card" />
</android.support.design.widget.CoordinatorLayout>

如何实现平滑滚动?

编辑:以下是recyclerview适配器

class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewViewHolder> {
private static final int DAYS_LEFT_TO_EXPIRE = 3;
private CardCallback mCallback;
private List<Ticket> mTickets;

@Inject
RecyclerViewAdapter() {
    mTickets = new ArrayList<>();
}

@Override
public RecyclerViewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_card, parent, false);
    return new RecyclerViewViewHolder(view);
}

@Override
public void onBindViewHolder(final RecyclerViewViewHolder holder, int position) {
    Ticket ticket = mTickets.get(position);
    if (ticket != null) {
        holder.setTicket(ticket);
        if (ticket.getHeadCount() != -1)
            holder.mTxtHeadCount.setText(String.valueOf(ticket.getHeadCount()));
        if (ticket.getType() != null && !ticket.getType().trim().isEmpty())
            setCardTitle(holder.mTxtCardTitle, ticket);
        if (ticket.getBlockCount() != -1)
            holder.mTxtBlockCount.setText(String.valueOf(ticket.getBlockCount()));
        if (ticket.getSeasonalPass() != null && !ticket.getSeasonalPass().trim().isEmpty())
            holder.mCardSeasonalPass.setText(ticket.getSeasonalPass());
        if (ticket.getLastUsedDate() != null)
            holder.mTxtCardLastUsedDate.setText(ticketDateFormatter.format(ticket.getLastUsedDate()));
        if (ticket.getExpiryDate() != null)
            holder.mTxtCardExpiryDate.setText(ticketDateFormatter.format(ticket.getExpiryDate()));
        if (ticket.getSeatingClass() != null && !ticket.getSeatingClass().trim().isEmpty())
            holder.mTxtSeatingClass.setText(ticket.getSeatingClass());
        if (ticket.getTheaterGroup() != null && !ticket.getTheaterGroup().trim().isEmpty())
            holder.mTxtTheaterGroup.setText(ticket.getTheaterGroup());
        if (ticket.getSellerName() != null && !ticket.getSellerName().trim().isEmpty())
            holder.mTxtSellerName.setText(ticket.getSellerName());
        if (ticket.getDescription() != null && !ticket.getDescription().trim().isEmpty())
            holder.mTxtCardDescription.setText(ticket.getDescription());
        if (ticket.getStatus() != null && !ticket.getStatus().trim().isEmpty())
            holder.mTxtCardStatus.setText(ticket.getStatus());
        if (DateUtil.noOfDaysLeft(ticket.getExpiryDate()) <= DAYS_LEFT_TO_EXPIRE)
            holder.mLblWarningMessage.setVisibility(View.VISIBLE);
        else holder.mLblWarningMessage.setVisibility(View.GONE);
        setCardProgress(holder.mCardProgress, ticket);
    }
}

private void setCardTitle(TextView txtCardTitle, Ticket ticket) {
    switch (ticket.getType()) {
        case Constants.CARD_TYPE_PACK:
            txtCardTitle.setText(ticket.getPackCount() + " " + ticket.getType());
            break;
        case Constants.CARD_TYPE_SEASONAL:
            txtCardTitle.setText(ticket.getType().concat(" pass"));
            break;
        default:
            txtCardTitle.setText(ticket.getType());
            break;
    }
}

private void setCardProgress(ProgressBar progressBar, Ticket ticket) {
    int maxLimit = 0, progress = 0;
    switch (ticket.getType()) {
        case Constants.CARD_TYPE_SEASONAL:
            maxLimit = (int) TimeUnit.MILLISECONDS.toDays(ticket.getExpiryDate().getTime() - ticket.getLastUsedDate().getTime());
            progress = maxLimit - DateUtil.noOfDaysLeft(ticket.getExpiryDate());
            break;
        case Constants.CARD_TYPE_PACK:
            maxLimit = ticket.getPackCount();
            progress = maxLimit - (ticket.getPackCount() - ticket.getPackUsed());
            break;
        case Constants.CARD_TYPE_TICKET:
            maxLimit = (int) TimeUnit.MILLISECONDS.toDays(ticket.getExpiryDate().getTime() - ticket.getTicketBoughtDate().getTime());
            progress = maxLimit - DateUtil.noOfDaysLeft(ticket.getExpiryDate());
            break;
    }
    progressBar.setMax(maxLimit);
    if (progress < 0) {
        progressBar.setProgress(maxLimit);
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
            progressBar.setProgress(progress, true);
        else progressBar.setProgress(progress);
    }
}

@Override
public int getItemCount() {
    return mTickets.size();
}

void setTickets(List<Ticket> tickets) {
    if (tickets != null && !tickets.isEmpty()) {
        mTickets.addAll(tickets);
        notifyDataSetChanged();
    }
}

void setCallback(CardCallback callback) {
    this.mCallback = callback;
}

void clearTicketList() {
    mTickets.clear();
    notifyItemRangeChanged(0, mTickets.size());
}

interface CardCallback {
    void onBtnMoreCardInfoClicked(Ticket ticket, LinearLayout layout, ImageButton imageButton);
}

class RecyclerViewViewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.txt_head_count)
    TextView mTxtHeadCount;
    @BindView(R.id.txt_card_type)
    TextView mTxtCardTitle;
    @BindView(R.id.txt_block_count)
    TextView mTxtBlockCount;
    @BindView(R.id.txt_seasonal_pass)
    TextView mCardSeasonalPass;
    @BindView(R.id.txt_card_last_used_date)
    TextView mTxtCardLastUsedDate;
    @BindView(R.id.txt_card_expiry_date)
    TextView mTxtCardExpiryDate;
    @BindView(R.id.card_progress)
    ProgressBar mCardProgress;
    @BindView(R.id.txt_seating_class)
    TextView mTxtSeatingClass;
    @BindView(R.id.txt_theater_group)
    TextView mTxtTheaterGroup;
    @BindView(R.id.txt_seller_name)
    TextView mTxtSellerName;
    @BindView(R.id.btn_more_card_info)
    ImageButton mBtnMoreCardInfo;
    @BindView(R.id.layout_card_description)
    LinearLayout mLayoutCardDescription;
    @BindView(R.id.txt_card_description)
    TextView mTxtCardDescription;
    @BindView(R.id.txt_card_status)
    TextView mTxtCardStatus;
    @BindView(R.id.lbl_warning_message)
    TextView mLblWarningMessage;

    private Ticket mTicket;

    NFCCardRecyclerViewViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);

        mBtnMoreCardInfo.setOnClickListener(v -> {
            if (mCallback != null)
                mCallback.onBtnMoreCardInfoClicked(mTicket, mLayoutCardDescription, mBtnMoreCardInfo);
        });
    }

    void setTicket(Ticket mTicket) {
        this.mTicket = mTicket;
    }
}

}

共有1个答案

富辰阳
2023-03-14

按照此链接上的步骤操作:平滑的应用栏布局 这个库帮助我解决了我的问题。

 类似资料:
  • 我正在开发一个应用程序,其中我使用了AppBarLayout和CollapsingToolbarLayout以及NestedScrollView。我已经成功地实现了这一点,并且运行良好。 现在我想做的是,在嵌套滚动视图上滑动(快速向上滑动)时,它应该完全滚动到顶部。类似地,在向屏幕底部滑动(快速向下滑动)时,它必须平滑地滚动到底部。然而现在,它只能卡在中间,这使它看起来很丑。我已经尝试了许多可用的

  • 我正在开发一个Android应用程序,其中我使用、和来使用折叠工具栏功能。 我在布局中使用在相同的布局中展开和折叠。当我试图从屏幕中心向上滚动时,它不起作用,但当我试图从屏幕右角向上滚动屏幕时,它会平滑滚动。 下面提到的是我的 xml 文件 layout.xml 理想的结果是,当我尝试从屏幕中心向上滚动时,它应该像我从手机右角向上滚动一样工作。 请观看下面提到的视频,以便更清楚地了解问题 http

  • 我正在创建一个<code>RecyclerView</code>,其中当您向上滚动<code>RecyclerView>时,标题会折叠。我可以通过下面的布局实现这一点,使用透明的和,这是标题。视差效果很好。 但是,如果标头仍然可见,并且我抛出,RV会慢慢滚动到顶部,并且一些项目在工具栏下,直到RV到达视图的顶部。我一直在玩,但没有达到理想的结果。关于如何改善抛出体验以使项目不被剪切的任何建议? 观

  • 我正在一个屏幕上工作,其中有一个产品的工具栏信息和类似产品的列表。 除了滚动,一切都很好。当recyclerview出现在屏幕上时,滚动不顺畅。我认为这个想法应该是在没有滚动条的情况下使用recycler视图。因为现在看起来我有两个滚动条,这让滚动很痛苦。有没有一种方法可以让我禁用recyclerview的滚动条,并将其拉伸到所需的高度,让父滚动条平滑地滚动我的屏幕? 我使用的是StaggedGr

  • 我有以下布局: 当我尝试向上滚动时,它无法正常工作,而且会滞后和卡住,而向下滚动似乎很好。我不明白为什么。我找了很多,做了很多改变,但仍然没有效果。 以下是我的活动内容