我的应用程序中有一个recyclerview,显示内容列表。如果订阅了用户,则他们可以继续阅读全部内容,如果没有订阅,则隐藏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),该布局与回收者视图位于同一布局文件中,并且还将整个回收者视图隐藏在适配器中?
答案 0 :(得分:1)
您需要的是创建 Adapter 时作为参数传入的界面。
示例:
class Adapter(private val actions: Actions) : RecyclerView.Adapter<ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
// Create ViewHolder
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// Setup Binder
holder.readMoreButton.setOnClickListener(View.OnClickListener {
if (userIsSubscribed) {
actions.launchActivity() //Launch the next Activity
} else {
//Show the subscribe layout
holder.rlSubscribeToView.setVisibility(View.VISIBLE)
actions.hideRecylerView() //Then hide the entire recyclerView
}
})
}
}
internal interface Actions {
fun launchActivity()
fun hideRecylerView()
}
答案 1 :(得分:1)
从一点点研究来看,获得的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();
}