RecyclerView不会在行下面扩展

时间:2018-03-05 07:56:28

标签: android android-recyclerview

我试图在RecyclerView

上获得与此相似的效果

enter image description here

我已经尝试了Google I / O方法,这里的类似问题也是如此。我的适配器代码如下所示。

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    ViewHolder itemViewHolder = (ViewHolder) holder;
    final boolean isExpanded = position == mExpandedPosition;
    itemViewHolder.mNotesTV.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
    holder.itemView.setActivated(isExpanded);
    holder.itemView.setOnClickListener(v -> {
        mExpandedPosition = isExpanded ? -1 : position;
        notifyItemChanged(position);
    });
}

private class ViewHolder extends RecyclerView.ViewHolder {
    TextView mNotesTV;

    ViewHolder(View itemView) {
        super(itemView);
        mNotesTV = itemView.findViewById(R.id.notesTV);
    }
}

{I}尝试展开的xml文件看起来像这样

TextView

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/notesTV" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Details" /> </RelativeLayout> xml文件看起来像这样

RecyclerView

而我得到的,而不是上面显示的效果(<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="false"> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end|bottom" android:layout_margin="16dp" android:clickable="true" android:src="@drawable/ic_add_white_24dp" /> <android.support.v7.widget.RecyclerView android:id="@+id/historyLV" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" /> </android.support.design.widget.CoordinatorLayout> 项目展开,下面是所需的项目),我只是将RecyclerView放在实际行而不是下面。

enter image description here

2 个答案:

答案 0 :(得分:1)

如果您还没有viewholder的布局,请制作如下所示的布局。根据您的偏好更改ID和颜色:

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

  <TextView
    android:id="@+id/alwaysVisible"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:gravity="center"
    android:padding="@dimen/activity_horizontal_margin"
    android:text="02 Mar, 2018"
    android:textColor="@android:color/holo_blue_bright" />

  <TextView
    android:id="@+id/conditionallyVisible"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_blue_dark"
    android:gravity="center"
    android:padding="@dimen/activity_horizontal_margin"
    android:text="Details"
    android:textColor="@android:color/white"
    android:visibility="gone" />

</LinearLayout>

接下来,像这样在你的适配器中充气(假设上面的布局被称为item_layout

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_layout, parent, false);
    return new ViewHolder(itemView);
}

viewholder课程中,获取上述布局中的TextView,如下所示:

private class ViewHolder extends RecyclerView.ViewHolder {
    TextView mDateTV;
    TextView mDetailsTV;

    ViewHolder(View itemView) {
        super(itemView);
        mDateTV = itemView.findViewById(R.id.alwaysVisible);
        mDetailsTV = itemView.findViewById(R.id.conditionallyVisible);
    }
}

onBindViewHolder中,执行以下操作:

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    ViewHolder itemViewHolder = (ViewHolder) holder;
    final boolean isExpanded = position == mExpandedPosition;
    itemViewHolder.mDetailsTV.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
    holder.itemView.setActivated(isExpanded);
    holder.itemView.setOnClickListener(v -> {
        mExpandedPosition = isExpanded ? -1 : position;
        notifyItemChanged(position);
    });
}

这就是全部。我希望你能让它发挥作用。

答案 1 :(得分:0)

也许您的要求是制作可扩展的Recyclerview。 您可以查看此库documentation