我的应用程序中有一个显示内容列表的回收 View 。如果用户订阅了,他们可以继续阅读全部内容,如果没有,则隐藏 recyclerview 并显示订阅布局。
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="@color/light_grey"
android:orientation="vertical"
android:weightSum="10"
tools:layout_editor_absoluteY="25dp">
<!--RECYCLER VIEW-->
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_weight="8.3"
android:padding="2dp"
android:visibility="gone" />
<!--SUBSCRIBE TO VIEW CONTENT LAYOUT-->
<RelativeLayout
android:id="@+id/rlSubscribeToView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:visibility="gone"
android:layout_margin="10dp"
android:elevation="48dp"
android:layout_gravity="center"
android:layout_weight="8.3">
<TextView
android:id="@+id/tvSubscribe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:textColor="@color/maroon"
android:text="@string/subscribe_to_view"/>
<Button
android:id="@+id/btnSubscribe"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="15dp"
android:layout_marginStart="20dp"
android:layout_below="@id/tvSubscribe"
android:layout_marginEnd="20dp"
android:text="@string/subscribe"
android:textColor="@color/white" />
</RelativeLayout>
和
RecyclerView 适配器
public void onBindViewHolder(@NonNull final ReadViewholder, final int position) {
holder.readMoreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (userIsSubscribed) {
//Launch the next Activity
} else {
//Show the subscribe layout
holder.rlSubscribeToView .setVisibility(View.VISIBLE);
//Then hide the entire recyvlerView
}
}
});
}
class ReadViewholder extends RecyclerView.ViewHolder {
RelativeLayout rlSubscribeToView;
Button readMoreButton;
ReadViewholder(@NonNull View itemView) {
super(itemView);
//Not able to find (rlSubscribeToView ) since its not inside the recyclerview
rlSubscribeToView = itemView.findViewById(R.id.rlSubscribeToView);
readMoreButton= itemView.findViewById(R.id.readMoreButton);
}
}
我如何才能访问订阅布局 (rlSubscribeToView),它与回收器 View 位于同一布局文件中,并且还在适配器中隐藏整个回收器 View ?
最佳答案
通过一些研究,获得的 2 个回调可以让您引用实际的 RecyclerView,onAttachedToRecyclerView和 onDetachedFromRecycler方法。我的猜测是您正在调用 Adapter 构造函数并传入上下文。如果是这样,请使用以下代码,它将产生您想要的结果。
RelativeLayout rlSubscribeToView;
RecyclerView recyclerView;
public RecyclerAdapter(Context context) {
this.context = context;
this.videoItems = videoItems;
rlSubscribeToView = ((Activity) context).findViewById(R.id.rlSubscribeToView);
}
@Override
public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
this.recyclerView = recyclerView;
}
并且在您的 onBindViewHolder
中,您现在可以访问订阅布局
public void onBindViewHolder(@NonNull final ReadViewholder, final int position) {
...
rlSubscribeToView.setVisibility();
}
关于android - 如何从适配器访问外部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54251636/