How to add element from one TabLayout(All) to another TabLayout(Favorites) by pressing "favourites" btn | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to add element from one TabLayout(All) to another TabLayout(Favorites) by pressing "favourites" btn

I have two Tab Layouts: All, Favorites. All tab layout has RecyclerView. Every element in RecyclerView has title,description,date and favourite button. I need to realize the next logic: when i press the favourite btn, need to add this selected element of RecyclerView to Favourites tab layout. Please, I need your help with this task! I try to add the example of code this: AllFragment.kt class AllFragment : Fragment() { private var layoutManager: RecyclerView.LayoutManager? = null private var adapter: RecyclerView.Adapter<RecyclerAdapter.ViewHolder>? = null override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView) layoutManager = LinearLayoutManager(requireActivity()) recyclerView?.layoutManager = layoutManager adapter = RecyclerAdapter() recyclerView?.adapter = adapter } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater.inflate(R.layout.fragment_all, container, false) } } FavouritesFragment.kt class FavouritesFragment : Fragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_favourites, container, false) } }

7th Jun 2021, 9:50 AM
Andrey Rudnev
Andrey Rudnev - avatar
5 Answers
0
...continuation from RecyclerAdapter.kt inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){ var itemTitle: TextView var itemDescription: TextView var itemDate: TextView var itemLike: CheckBox init { itemTitle = itemView.findViewById(R.id.itemTitle) itemDescription = itemView.findViewById(R.id.itemDescription) itemDate = itemView.findViewById(R.id.itemDate) itemLike = itemView.findViewById(R.id.itemLike) itemView.setOnClickListener { val position: Int = absoluteAdapterPosition Toast.makeText(itemView.context, "You clicked on ${titles[position]}", Toast.LENGTH_LONG).show() } itemLike.setOnCheckedChangeListener { checkBox, isChecked -> if (isChecked){ Toast.makeText(itemLike.context,"Item added to Favourites", Toast.LENGTH_SHORT).show() } else { Toast.makeText(itemLike.context,"Item removed from Favourites", Toast.LENGTH_SHORT).show() } } } } }
7th Jun 2021, 9:52 AM
Andrey Rudnev
Andrey Rudnev - avatar
0
RecyclerAdapter.kt inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView){ var itemTitle: TextView var itemDescription: TextView var itemDate: TextView var itemLike: CheckBox init { itemTitle = itemView.findViewById(R.id.itemTitle) itemDescription = itemView.findViewById(R.id.itemDescription) itemDate = itemView.findViewById(R.id.itemDate) itemLike = itemView.findViewById(R.id.itemLike) itemView.setOnClickListener { val position: Int = absoluteAdapterPosition Toast.makeText(itemView.context, "You clicked on ${titles[position]}", Toast.LENGTH_LONG).show() } itemLike.setOnCheckedChangeListener { checkBox, isChecked -> if (isChecked){ Toast.makeText(itemLike.context,"Item added to Favourites", Toast.LENGTH_SHORT).show() } else { Toast.makeText(itemLike.context,"Item removed from Favourites", Toast.LENGTH_SHORT).show() } } } } } continue in the next post...
7th Jun 2021, 9:53 AM
Andrey Rudnev
Andrey Rudnev - avatar
0
MainActivity.kt class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val tabLayout = findViewById<TabLayout>(R.id.tabLayout) val viewPager2 = findViewById<ViewPager2>(R.id.viewPager2) val adapter=ViewPagerAdapter(supportFragmentManager,lifecycle) viewPager2.adapter=adapter TabLayoutMediator(tabLayout,viewPager2){tab,position-> when(position){ 0->{ tab.text="All" } 1->{ tab.text="Favourites" } } }.attach() } }
7th Jun 2021, 9:53 AM
Andrey Rudnev
Andrey Rudnev - avatar
0
model.xml <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" app:cardCornerRadius="3dp" app:cardElevation="3dp" app:cardUseCompatPadding="true"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp"> <TextView android:id="@+id/itemTitle" style="@style/TextAppearance.AppCompat.Title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Title" android:textColor="@color/black" /> <TextView android:id="@+id/itemDescription" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/itemTitle" android:text="Some description..." /> <TextView android:id="@+id/itemDate" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/itemDescription" android:text="Date" android:textAlignment="textEnd" /> <CheckBox android:id="@+id/itemLike" android:layout_width="wrap_content" android:layout_height="wrap_content" android:button="@drawable/icon_favourite_black" android:layout_alignEnd="@+id/itemTitle" android:layout_marginTop="-10dp"/> </RelativeLayout> </androidx.cardview.widget.CardView>
7th Jun 2021, 9:54 AM
Andrey Rudnev
Andrey Rudnev - avatar
0
Emma Watson Can you give an example? And maybe you give me some advice what material can help me figure it out?
7th Jun 2021, 1:57 PM
Andrey Rudnev
Andrey Rudnev - avatar