Я создаю приложение для Android, в котором есть функция отслеживания и отмены подписки. У меня есть 3 страницы внутри ViewPager
в главном доме. Лента состоит из постов пользователей по интересам.
Например, пользователь A следует за пользователем B, который публикует статью в трех различных разделах: еда, путешествия и технологии. В этом случае пользователь А сможет увидеть все свои посты в 3 разных местах. Я хочу, чтобы, если пользователь A, который подписан на B, отписался от пользователя B с любой страницы, он просто был удален со всех страниц и обновил фид.
Я хочу наблюдать за этим событием и отменить подписку на основе действий пользователя и обновить фид.
Как создать наблюдатель для этого шаблона. Я открыт, чтобы использовать все, что можно ожидать BroadcastReceiver
.
feed_item.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:id="@+id/parent_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:foreground="?android:attr/selectableItemBackgroundBorderless"
app:cardCornerRadius="4dp"
app:cardElevation="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:orientation="horizontal">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/profilePicture"
android:layout_width="40dp"
android:layout_height="40dp"
fresco:roundedCornerRadius="@dimen/feed_rect_card_radius" />
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:textColor="@color/darkTextColor" />
<TextView
android:id="@+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:fontFamily="@font/noto_sans_bold"
android:textColor="#747474"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/followBtn"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center"
android:layout_marginEnd="32dp"
android:fontFamily="@font/noto_sans_bold"
android:gravity="center"
android:text="@string/follow"
android:textColor="@color/green"
android:textSize="10sp"
app:rippleColor="@color/green"
app:strokeColor="@color/green" />
<ImageView
android:id="@+id/pop_up_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|top"
android:layout_marginTop="4dp"
android:contentDescription="@null"
android:padding="2dp"
android:src="@drawable/ic_more_dark" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingStart="4dp"
android:paddingTop="8dp"
android:paddingEnd="4dp"
android:paddingBottom="8dp">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/image"
android:layout_width="124dp"
android:layout_height="80dp"
fresco:roundedCornerRadius="@dimen/feed_rect_card_radius" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="4dp"
android:contentDescription="@null"
android:src="@drawable/ic_play_circle_feature" />
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="@dimen/item_offset">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/post_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:ellipsize="end"
android:fontFamily="@font/noto_sans_bold"
android:gravity="start|top"
android:maxLines="2"
android:textColor="@color/darkTextColor"
android:textSize="14sp"
android:textStyle="bold" />
<TextView
android:id="@+id/post_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:fontFamily="@font/noto_sans"
android:gravity="start"
android:lineSpacingExtra="4dp"
android:maxLines="2"
android:padding="2dp"
android:textSize="12sp"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:id="@+id/categoryLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal" />
<FrameLayout
android:id="@+id/whatsAppShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:foreground="?attr/selectableItemBackground"
android:padding="4dp">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:contentDescription="@null"
android:src="@drawable/ic_whatsapp_fill" />
</FrameLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
FeedAdapter
class FeedAdapter(private val mContext: Context, val fg: FragmentManager, val nodes: ArrayList<Feed.Item>, val tagId: Int) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
var showLoader: Boolean = false
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
ViewTypes.FEED -> FeedItemVH(LayoutInflater.from(mContext).inflate(R.layout.feed_item, parent, false))
else -> FooterViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_footer, parent, false))
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is FeedItemVH -> feedItemView(holder, nodes[position].fragments().feedItem())
else -> {
if (showLoader) (holder as FooterViewHolder).itemView.footerLoader.visibility = View.VISIBLE
else (holder as FooterViewHolder).itemView.footerLoader.visibility = View.GONE
}
}
}
private fun feedItemView(holder: FeedItemVH, feed: FeedItem) {
holder.followBtn.setOnClickListener {
//API call to follow/unfollow user and broadcast the result.
}
}
override fun getItemCount(): Int {
return when (nodes.size) {
0 -> 0
else -> nodes.size + 1
}
}
override fun getItemViewType(position: Int): Int {
return when {
position != 0 && position == itemCount - 1 -> ViewTypes.LOADER
else -> {
ViewTypes.FEED
}
}
}
fun showLoader(status: Boolean) {
showLoader = status
Handler().post { this.notifyDataSetChanged() }
}
interface ViewTypes {
companion object {
const val FEED = 1
const val LOADER = 2
}
}
}
Любая помощь будет оценена. Спасибо!