How can I get access to outside views from an Adapter - android

I have a recyclerview in my app showing a list of content. If a user is subscribed, they can proceed to read the full content and if not the recyclerview is hidden and a subscribe layout is shown.
Layout file
<?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>
and the
RecyclerView Adapter
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);
}
}
How can I get access to the subscribe layout (rlSubscribeToView), which is on the same layout file as the recycler view and also hide the entire recycler view in the Adapter?

From a little bit of research, the are 2 callbacks that get can get you a reference to the actual RecyclerView, the onAttachedToRecyclerView and the onDetachedFromRecycler methods. My guess is that you are calling the Adapter constructor and passing in a context. If so use the below code, it will produce your desired result.
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;
}
and in your onBindViewHolder you can now get access the subscribe layout
public void onBindViewHolder(#NonNull final ReadViewholder, final int position) {
...
rlSubscribeToView.setVisibility();
}

What you need is an interface passed in as a parameter when you create your Adapter.
Example:
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()
}

Related

RecyclerView data not showing

My ReycyclerView is not showing data, but once i removed .setHasFixedSize(true), then only it will display. However, this causes another problem. On the first time entering the fragment, it works fine, my actionBar is displaying, but on the second time, it somehow overlaps or pushes away my ActionBar.
I guess .sethasFixedSize is necessary but if i keep it, it will not display data. What causes this?
First time entering:
Second time entering (before notifyItemInserted()):
After notifyItemInserted():
fragment.xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/cartItemRv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
fragment.kt:
onCreateView() {
binding.cartItemRv.layoutManager = LinearLayoutManager(context)
binding.cartItemRv.setHasFixedSize(true)
cartAdapter = CartAdapter(productNameList, productVariationList, cartItemList)
binding.cartItemRv.adapter = cartAdapter
//code for retrieving data from Firebase
cartAdapter.notifyItemInserted(productNameList.size-1)
}
Adapter:
class CartAdapter(...: RecyclerView.Adapter<CartAdapter.CartViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CartViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.cart_item, parent, false)
return CartViewHolder(itemView, mListener)
}
override fun onBindViewHolder(holder: CartViewHolder, position: Int) {
...
}
override fun getItemCount(): Int {
return productNameList.size
}
inner class CartViewHolder(itemView: View, listener: onItemClickListener) : RecyclerView.ViewHolder(itemView){
...
}
}
about sethasFixedSize , if the size of your the RecyclerView depends on the adapter's content you have to set the sethasFixedSize vlaue : false
otherwise set it true .
// for depending on adapter
mRecyclerView.setHasFixedSize(false);
// for doesn't depend on adapter
mRecyclerView.setHasFixedSize(true);
add NestedScrollView on the parent of RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/cartItemRv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"/>
</androidx.core.widget.NestedScrollView>
</RelativeLayout>
Then set RecyclerView
cartItemRv.setHasFixedSize(false);

ViewPager child to swipe next the parent

I have a ViewPager2 which I'm using with a RecyclerView Adapter. I'm binding the children of each viewpager item via the ViewHolder and everything is working okay.
I have a bunch of elements in this ViewPager item XML, some RadioButton components and a Button. I want this button to move it to the next item.
I know how to do that externally, assigning a sibling to the ViewPager2 and then setting a click-listener in the activity and then comparing currentItem of the ViewPager with the adapter's total item count.
I want the "Move to next" button inside the ViewPager as I want to change it based on the inputs supplied to the RadioButton components.
I'm currently stuck at failing getting the ViewPager item's button in the activity to set the click-listener. Is there any workaround to get the child element via the adapter itself?
Here's the activity_quiz.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".ui.quiz.view.QuizActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/quiz_list"
android:layout_width="match_parent"
android:layout_height="600dp"
android:clipToPadding="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="16dp"
tools:listitem="#layout/quiz_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
The quiz_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:padding="#dimen/bigSpacing"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/question"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/choices">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/choice_1"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/choice_2"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/choice_3"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/choice_4"/>
</RadioGroup>
<Button
android:id="#+id/result_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Next question" />
</LinearLayout>
The MyAdapter class (kotlin)
class MyAdapter: RecyclerView.Adapter<MyAdapter.MyViewHolder> {
override fun onCreateViewHolder(...) {
...
}
override fun getItemCount(): Int {
...
}
override fun onBindViewHolder(...) {
...
}
class MyViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
fun bind(someData: SomeData) {
itemView.question.text = somedata.question
itemView.choice_1.text = somedata.choice_1
itemView.choice_2.text = somedata.choice_2
itemView.choice_3.text = somedata.choice_3
itemView.choice_4.text = somedata.choice_4
val answerKey = someData.answerKey
var rightOrWrong = ""
itemView.choices.setOnCheckedChangeListener {_, checkedID ->
val checkedIndex: Int = itemView.choices.indexOfChild(itemView.choices.findViewById(checkedID))
if(checkedIndex + 1 == answerKey.toInt()) {
rightOrWrong = "Correct! Next Question."
} else {
rightOrWrong = "Incorrect :/ Next Question."
}
itemView.result_button.text = rightOrWrong
}
}
}
And the QuizActivity.kt file
class QuizActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_quiz)
val myAdapter = MyAdapter()
quiz_list.adapter = myAdapter
// quiz_list.result_button.setOnClickListener {
// No idea how to get the children correctly, as this one throws errors
// }
}
}
First, why do you want to use RecyclerView.Adapter<MyAdapter.MyViewHolder> as an adapter for viewpager2? There is FragmentStateAdapter a built-in class implementation of PagerAdapter that uses a Fragment to manage each page, which is recommended for viewpager2.
Second, you are not inflating the views in MyViewHolder, I don't know if you left them intentionally for this post.
You cant access child views like this quiz_list.result_button.setOnClickListener {} while using something like RecyclerView, Viewpagger. You can access them in the ViewHolder after you inflate them, you can set any listener there aswell

How to call fragment function from RecyclerView.Adapter click for android application build using dagger2

Can someone suggest me the best way to call a fragment function from the RecyclerView without causing any memmory leaks
My fragment code is given below
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".ui.auth.AuthActivity">
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/videoID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
app:resize_mode="fill"
app:controller_layout_id="#layout/player_control_view"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/materialList"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/videoID">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
My recycler view layout is
<androidx.constraintlayout.widget.ConstraintLayout
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="wrap_content"
android:orientation="vertical"
android:padding="#dimen/_10sdp">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:cardBackgroundColor="#color/circle_color_light"
app:layout_constraintVertical_bias="0.6">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Please ignore any error on the above layouts since my issue lies on java code
My fragment code is given below
public class MaterialFragment extends DaggerFragment implements View.OnClickListener {
SimpleExoPlayer player;
PlayerView playerView;
#Inject
MaterialAdapter adapter;
#Inject
ViewModelProviderFactory providerFactory;
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
makeVideo("https://samplevideo.com/test.mp4");
materialList = view.findViewById(R.id.materialList);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),3,GridLayoutManager.VERTICAL,false);
materialList.setLayoutManager(gridLayoutManager);
materialList.setAdapter(adapter);
// The above code succesfully load the grid view using recycler adapter(Code for adapter is given below)
}
public void makeVideo(){
// Video loading code goes here : Loads video on findViewById(R.id.videoID)
}
}
Finally my Recycler Adapter view
public class MaterialAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final String TAG = "MaterialAdapter";
private List<Material> materials = new ArrayList<>();
private Context context;
public MaterialAdapter(){
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.material_grid_layout, parent, false);
PlayerView playerView = ((Activity)parent.getContext()).getWindow().getDecorView().findViewById(R.id.matVideo);
SimpleExoPlayer player = new SimpleExoPlayer.Builder(((Activity)parent.getContext()).getApplicationContext()).build();
context = parent.getContext();
return new MaterialAdapter.MaterialViewHolder(view,parent.getContext(),playerView,player);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
((MaterialAdapter.MaterialViewHolder)holder).bind(materials.get(position),context);
}
#Override
public int getItemCount() {
return materials.size();
}
public void setMaterials(List<Material> materials){
this.materials = materials;
notifyDataSetChanged();
}
public class MaterialViewHolder extends RecyclerView.ViewHolder {
TextView name,letter;
public Context cxt;
private SimpleExoPlayer player;
private PlayerView playerView;
public MaterialViewHolder(#NonNull View itemView, Context cxt, final PlayerView playerView, SimpleExoPlayer player) {
super(itemView);
this.cxt = cxt;
this.playerView = playerView;
this.player = player;
name = itemView.findViewById(R.id.name);
clayout = (ConstraintLayout) itemView.findViewById(R.id.card_layout);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// I need to change the video of the fragment on click of each block.
//The below code will work by implementing full makevideo function on this file
//But I need the bes method without repeting this function and call the methods of he fragment by passing video url or setting view contents of the outer fragment
//makeVideo("https://samplevideo.com/test.mp4"); // Not think this is he best solution as I need fullscreen function also.
// How to trigger button click of fragment, eg full screen
}
});
}
public void bind(Material material, Context context){
name.setText(material.getName());
}
}
}
Adapter is injected using module as given below
#Provides
static MaterialAdapter provideMaterialAdapter(){
return new MaterialAdapter();
}
After referring, I got some suggestion by using my own interface as a bridge between adapter and fragment , But It didn't work may be my dagger is causing some injection error,
Can someone give me a best solution to get rid of this issue
Screenshot of my screen is given below
Use this adapter = new MaterialAdapter(); in your fragment rather than injecting it. Just remove the #Inject annotation and add the above adapter creation statement in onViewCreated of your fragment. By this way your get more control on your adapter creation and you can pass an interface through the constructor.

Update an Activity Edit text from the adapter

I have a MainActivity that uses a RecyclerView with an Adapter. I would like to update the Textview title from the Adapter class. When the user clicks on the CheckBox within the CardView that is in the RecyclerView.
Clicking and getting the info work well but I don't know how to access the TextView title from the Adapter class to be able to update the info any time the user clicks on the check box and changes the info.
So, my problem is, how to access title TextView from Adapter class?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/fondo"
android:orientation="vertical"
tools:context=".Menu_21RutinaMati">
<TextView
android:id="#+id/titol"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/colorAccent"
android:gravity="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="18sp"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_View"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"/>
</LinearLayout>
If you're using Kotlin, I'd do something like this:
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
recycler_View.adapter = YourAdapter() { checkboxClicked ->
titol.text = if(checkboxClicked) "firstTitle" else "secondTitle"
}
}
class YourAdapter(private val onCheckboxClicked: (Boolean) -> Unit): RecyclerView.Adapter() {
...
override onCreateViewHolder(parent: ViewGroup, viewType: Int) {
return ViewHolder(LayoutInflater
.from(parent.context)
.inflate(R.layout.your_item_layout, parent, false)
.apply {
checkbox.setOnCheckedChangeListener { buttonView, isChecked -> onCheckboxClicked(isChecked) }
})
}
you can define an interface in your adapter:
public interface onItemClickListener{
void onClick(String title, Long id);//pass your object types.
}
and define this interface in adapter and initialize in adapter
onItemClickListener onItemClickListner;
public void setOnItemClickListener(adapter.onItemClickListener
onItemClickListner)
{
this.onItemClickListner = onItemClickListner;
}
in "onClickListener" method for cardview, use the interface object:
holder.cardview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onItemClickListner.onClick(model.getTitle(),model.getId());
}
});
now let's go to the activity
after defining your adapter object,implement interface method like this
Adapter adapter = new Adapter(context,list);
adapter.setOnItemClickListener(new adapter.onItemClickListener() {
#Override
public void onClick(String title, Long id) {
txt.setText(title);
}
});
hope this helps you!

smoothScrollToPosition does not work in nested RecyclerView

I have a recyclerView and each of it's item is a recyclerView. I want scroll to custom position of inner recyclerView at specific condition.
I used this code at innerRecyclerView but it didn't work:
innerRecyclerView.smoothScrollToPosition(position);
and this:
innerRecyclerView.scrollToPosition(position);
and this one:
layoutManager.scrollToPositionWithOffset(position, offset);
Now I have two question:
1) Is it possible scroll to custom position of inner recyclerView?
2) If that is possible, how?
Here is initialize main recyclerView:
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false);
adapter = new FreightsListRVAdapter(new FreightListCallBack(), new FreightListVHFactory());
rvFreightsList.setLayoutManager(layoutManager);
rvFreightsList.setItemViewCacheSize(30);
rvFreightsList.setNestedScrollingEnabled(true);
rvFreightsList.setAdapter(adapter);
Here is initialize innerRecyclerview:
LinearLayoutManager layoutManager = new LinearLayoutManager(itemView.getContext(), RecyclerView.VERTICAL, false);
layoutManager.setItemPrefetchEnabled(true);
layoutManager.setInitialPrefetchItemCount(7);
rvFreights.setNestedScrollingEnabled(false);
rvFreights.setLayoutManager(layoutManager);
rvFreights.setItemViewCacheSize(20);
adapter = new FreightsRVAdapter(new FreightCallBack(), new FreightSingleVHFactory());
rvFreights.setAdapter(adapter);
Here is my main recyclerView adapter:
public class FreightsListRVAdapter extends ListAdapter<FreightListModel, FreightListVH> {
private final FreightListVHFactory mFactory;
private final PublishSubject<FreightListVHAction> mClickUserPS = PublishSubject.create();
#Inject
public FreightsListRVAdapter(#NonNull FreightListCallBack diffCallback, FreightListVHFactory factory) {
super(diffCallback);
mFactory = factory;
}
#NonNull
#Override
public FreightListVH onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return mFactory.create(parent);
}
#Override
public void onBindViewHolder(#NonNull FreightListVH holder, int position) {
holder.getVM().setObject(getItem(position));
holder.bind();
holder.itemOnClick(mClickUserPS);
}
public PublishSubject<FreightListVHAction> getmClickUserPS() {
return mClickUserPS;
}
}
Here is layout of each row of mainRecyclerView:
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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="wrap_content"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatTextView 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:id="#+id/txtDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingRight="10dp"
android:textColor="#992C2C2C"
android:textSize="11sp"
app:fontFamily="#font/iransans"
tools:ignore="HardcodedText,SmallSp" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/txtDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="15dp"
android:gravity="right"
android:textColor="#2C2C2C"
android:textSize="20sp"
app:fontFamily="#font/iransans_medium"
tools:ignore="RtlHardcoded"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvFreights"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
android:scrollbars="none" />
</androidx.appcompat.widget.LinearLayoutCompat>
In Kotlin-androidX recyclerview solution
val parentViewHolder = parentRecyclerView?.findViewHolderForAdapterPosition(parentPosition) as? ParentViewHolder?
var childY = 0F
if (parentViewHolder != null)
childY = parentViewHolder.childRecyclerView?.getChildAt(lastPosition)?.y ?: 0F
parentRecyclerView.scrollBy(0, childY.toInt())
PrentViewHolder.kt
class PrentViewHolder(
view: InflateBinding
): RecyclerView.ViewHolder(view.root) {
var recyclerView: RecyclerView? = null
fun populateData(data: Data) {
viewDataBinding.recyclerview.adapter = adapter
recyclerView = viewDataBinding.recyclerview
}
}
Can you please try this:
float y = recyclerView.getY() + recyclerView.getChildAt(selectedPosition).getY();
mainRecyclerview.smoothScrollTo(0, (int) y);

Categories

Resources