Android. Nested recyclerview scrolling itself when traverse through parent recycler
Update:
I decide to create a github repo with this issue to anyone can try to reproduce it and try to fix it.
https://github.com/LolusKekus/NestsedRecyclerScrollItselfChallenge
I have vertical recycler with a bunch of nested horisontal recyclers.
When i'm scrolling over vertical recycler, nested recyclers scroll themself without any reason.
It's looks like this.
Before scroll, let's imagine that selector on 31:
_____________
|31 32 33 34|
|41 42 43 44|
|51 52 53 54|
_____________
now press up on dpad (i'm working on android tv app):
_____________
|24 25 26 27|
|31 32 33 34|
|41 42 43 44|
_____________
Hidden recyclerview appears, binds and scrolls up to 24 element. But it mustn't be! The selected item must be 21! And so on until the top of vertical recycler... 14... 04...
I'm noticed, nested recycler scrolls just to last visible item on it.
this behavior appears after "com.android.support:recyclerview-v7:24.2.1"
Now i'm using "com.android.support:recyclerview-v7:27.1.1" and faced with this issue.
Activity:
public class MainActivity extends AppCompatActivity {
List<String> hDataset;
List<Integer> vDataset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
hDataset = new ArrayList<>();
for (int i=0; i<hItemsCount; i++) {
hDataset.add(String.format("lolKek Cheburek %d", i));
}
vDataset = new ArrayList<>();
for (int i=0; i<vItemsCount; i++) {
vDataset.add(i);
}
vRecyclerView = findViewById(R.id.vRecyclerView);
vRecyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
vRecyclerView.setLayoutManager(layoutManager);
vRecyclerView.setAdapter(new VAdapter(vDataset, hDataset, context));
}
Vertical adapter:
public class VAdapter extends RecyclerView.Adapter<VAdapter.MyVertHolder>() {
private List<String> hDataset;
private Context context;
public VAdapter(List<String> hDataset, List<String> vDataset, Context context) {
this.hDataset = hDataset;
this.vDataset = vDataset;
this.context = context;
}
public static class MyVertHolder extends RecyclerView.ViewHolder {
RecyclerView hRecyclerView;
public MyVertHolder(final View itemView) {
super(itemView);
hRecyclerView = itemView.findViewById(R.id.hRecyclerView);
}
public void setAdapter(HAdapter adapter) {
hRecyclerView.setAdapter(adapter);
}
public void setLayoutManager(LinearLayoutManager layoutManager) {
hRecyclerView.setLayoutManager(layoutManager);
}
}
@NonNull
@Override
public MyVertHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater
.from(parent.getContext())
.inflate(R.layout.v_item_view, parent, false);
MyVertHolder holder = new MyVertHolder(itemView);
holder.setAdapter(new HAdapter(hDataset));
holder.setLayoutManager(new LinearLayoutManage(context, LinearLayoutManager.HORIZONTAL, false));
return holder;
}
@Override
public void onBindViewHolder(final @NonNull MyVertHolder holder, int position) {
... some bindings
}
}
Horisontal adapter:
class HAdapter extends RecyclerView.Adapter<HAdapter.MyHolder> {
private List<String> dataset;
public HAdapter(List<String> dataset) {
this.dataset = dataset;
}
public static class MyHolder extends RecyclerView.ViewHolder {
private TextView tvItemText;
public MyHolder(FrameLayout itemView) {
super(itemView);
tvItemText = itemView.findViewById(R.id.tvItemText);
}
}
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
FrameLayout itemView = (FrameLayout) LayoutInflater
.from(parent.getContext()).inflate(R.layout.item_view, parent, false);
return new MyHolder(itemView);
}
@Override
public void onBindViewHolder(final @NonNull MyHolder holder, final int position) {
holder.setItemText(dataset.get(position));
}
main activity layout:
<FrameLayout
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"
tools:context="MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/vRecyclerView"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
vertical item view layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/colorAccent"
android:id="@+id/hRecyclerView"
android:focusableInTouchMode="true"
android:focusable="true"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
horisontal item view layout:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/flContainer"
android:layout_margin="5dp"
android:focusable="true"
android:layout_width="250dp"
android:layout_height="150dp">
<TextView
android:id="@+id/tvItemText"
android:textColor="@color/white"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
I want stop this annoying auto scroll behavior.
Thanks.
java
add a comment |
Update:
I decide to create a github repo with this issue to anyone can try to reproduce it and try to fix it.
https://github.com/LolusKekus/NestsedRecyclerScrollItselfChallenge
I have vertical recycler with a bunch of nested horisontal recyclers.
When i'm scrolling over vertical recycler, nested recyclers scroll themself without any reason.
It's looks like this.
Before scroll, let's imagine that selector on 31:
_____________
|31 32 33 34|
|41 42 43 44|
|51 52 53 54|
_____________
now press up on dpad (i'm working on android tv app):
_____________
|24 25 26 27|
|31 32 33 34|
|41 42 43 44|
_____________
Hidden recyclerview appears, binds and scrolls up to 24 element. But it mustn't be! The selected item must be 21! And so on until the top of vertical recycler... 14... 04...
I'm noticed, nested recycler scrolls just to last visible item on it.
this behavior appears after "com.android.support:recyclerview-v7:24.2.1"
Now i'm using "com.android.support:recyclerview-v7:27.1.1" and faced with this issue.
Activity:
public class MainActivity extends AppCompatActivity {
List<String> hDataset;
List<Integer> vDataset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
hDataset = new ArrayList<>();
for (int i=0; i<hItemsCount; i++) {
hDataset.add(String.format("lolKek Cheburek %d", i));
}
vDataset = new ArrayList<>();
for (int i=0; i<vItemsCount; i++) {
vDataset.add(i);
}
vRecyclerView = findViewById(R.id.vRecyclerView);
vRecyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
vRecyclerView.setLayoutManager(layoutManager);
vRecyclerView.setAdapter(new VAdapter(vDataset, hDataset, context));
}
Vertical adapter:
public class VAdapter extends RecyclerView.Adapter<VAdapter.MyVertHolder>() {
private List<String> hDataset;
private Context context;
public VAdapter(List<String> hDataset, List<String> vDataset, Context context) {
this.hDataset = hDataset;
this.vDataset = vDataset;
this.context = context;
}
public static class MyVertHolder extends RecyclerView.ViewHolder {
RecyclerView hRecyclerView;
public MyVertHolder(final View itemView) {
super(itemView);
hRecyclerView = itemView.findViewById(R.id.hRecyclerView);
}
public void setAdapter(HAdapter adapter) {
hRecyclerView.setAdapter(adapter);
}
public void setLayoutManager(LinearLayoutManager layoutManager) {
hRecyclerView.setLayoutManager(layoutManager);
}
}
@NonNull
@Override
public MyVertHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater
.from(parent.getContext())
.inflate(R.layout.v_item_view, parent, false);
MyVertHolder holder = new MyVertHolder(itemView);
holder.setAdapter(new HAdapter(hDataset));
holder.setLayoutManager(new LinearLayoutManage(context, LinearLayoutManager.HORIZONTAL, false));
return holder;
}
@Override
public void onBindViewHolder(final @NonNull MyVertHolder holder, int position) {
... some bindings
}
}
Horisontal adapter:
class HAdapter extends RecyclerView.Adapter<HAdapter.MyHolder> {
private List<String> dataset;
public HAdapter(List<String> dataset) {
this.dataset = dataset;
}
public static class MyHolder extends RecyclerView.ViewHolder {
private TextView tvItemText;
public MyHolder(FrameLayout itemView) {
super(itemView);
tvItemText = itemView.findViewById(R.id.tvItemText);
}
}
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
FrameLayout itemView = (FrameLayout) LayoutInflater
.from(parent.getContext()).inflate(R.layout.item_view, parent, false);
return new MyHolder(itemView);
}
@Override
public void onBindViewHolder(final @NonNull MyHolder holder, final int position) {
holder.setItemText(dataset.get(position));
}
main activity layout:
<FrameLayout
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"
tools:context="MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/vRecyclerView"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
vertical item view layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/colorAccent"
android:id="@+id/hRecyclerView"
android:focusableInTouchMode="true"
android:focusable="true"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
horisontal item view layout:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/flContainer"
android:layout_margin="5dp"
android:focusable="true"
android:layout_width="250dp"
android:layout_height="150dp">
<TextView
android:id="@+id/tvItemText"
android:textColor="@color/white"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
I want stop this annoying auto scroll behavior.
Thanks.
java
None of the answers help yet. I decide to create a github repo with this issue to anyone can try to reproduce it and try to fix it. github.com/LolusKekus/NestsedRecyclerScrollItselfChallenge
– LolusKekus
17 hours ago
add a comment |
Update:
I decide to create a github repo with this issue to anyone can try to reproduce it and try to fix it.
https://github.com/LolusKekus/NestsedRecyclerScrollItselfChallenge
I have vertical recycler with a bunch of nested horisontal recyclers.
When i'm scrolling over vertical recycler, nested recyclers scroll themself without any reason.
It's looks like this.
Before scroll, let's imagine that selector on 31:
_____________
|31 32 33 34|
|41 42 43 44|
|51 52 53 54|
_____________
now press up on dpad (i'm working on android tv app):
_____________
|24 25 26 27|
|31 32 33 34|
|41 42 43 44|
_____________
Hidden recyclerview appears, binds and scrolls up to 24 element. But it mustn't be! The selected item must be 21! And so on until the top of vertical recycler... 14... 04...
I'm noticed, nested recycler scrolls just to last visible item on it.
this behavior appears after "com.android.support:recyclerview-v7:24.2.1"
Now i'm using "com.android.support:recyclerview-v7:27.1.1" and faced with this issue.
Activity:
public class MainActivity extends AppCompatActivity {
List<String> hDataset;
List<Integer> vDataset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
hDataset = new ArrayList<>();
for (int i=0; i<hItemsCount; i++) {
hDataset.add(String.format("lolKek Cheburek %d", i));
}
vDataset = new ArrayList<>();
for (int i=0; i<vItemsCount; i++) {
vDataset.add(i);
}
vRecyclerView = findViewById(R.id.vRecyclerView);
vRecyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
vRecyclerView.setLayoutManager(layoutManager);
vRecyclerView.setAdapter(new VAdapter(vDataset, hDataset, context));
}
Vertical adapter:
public class VAdapter extends RecyclerView.Adapter<VAdapter.MyVertHolder>() {
private List<String> hDataset;
private Context context;
public VAdapter(List<String> hDataset, List<String> vDataset, Context context) {
this.hDataset = hDataset;
this.vDataset = vDataset;
this.context = context;
}
public static class MyVertHolder extends RecyclerView.ViewHolder {
RecyclerView hRecyclerView;
public MyVertHolder(final View itemView) {
super(itemView);
hRecyclerView = itemView.findViewById(R.id.hRecyclerView);
}
public void setAdapter(HAdapter adapter) {
hRecyclerView.setAdapter(adapter);
}
public void setLayoutManager(LinearLayoutManager layoutManager) {
hRecyclerView.setLayoutManager(layoutManager);
}
}
@NonNull
@Override
public MyVertHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater
.from(parent.getContext())
.inflate(R.layout.v_item_view, parent, false);
MyVertHolder holder = new MyVertHolder(itemView);
holder.setAdapter(new HAdapter(hDataset));
holder.setLayoutManager(new LinearLayoutManage(context, LinearLayoutManager.HORIZONTAL, false));
return holder;
}
@Override
public void onBindViewHolder(final @NonNull MyVertHolder holder, int position) {
... some bindings
}
}
Horisontal adapter:
class HAdapter extends RecyclerView.Adapter<HAdapter.MyHolder> {
private List<String> dataset;
public HAdapter(List<String> dataset) {
this.dataset = dataset;
}
public static class MyHolder extends RecyclerView.ViewHolder {
private TextView tvItemText;
public MyHolder(FrameLayout itemView) {
super(itemView);
tvItemText = itemView.findViewById(R.id.tvItemText);
}
}
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
FrameLayout itemView = (FrameLayout) LayoutInflater
.from(parent.getContext()).inflate(R.layout.item_view, parent, false);
return new MyHolder(itemView);
}
@Override
public void onBindViewHolder(final @NonNull MyHolder holder, final int position) {
holder.setItemText(dataset.get(position));
}
main activity layout:
<FrameLayout
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"
tools:context="MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/vRecyclerView"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
vertical item view layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/colorAccent"
android:id="@+id/hRecyclerView"
android:focusableInTouchMode="true"
android:focusable="true"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
horisontal item view layout:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/flContainer"
android:layout_margin="5dp"
android:focusable="true"
android:layout_width="250dp"
android:layout_height="150dp">
<TextView
android:id="@+id/tvItemText"
android:textColor="@color/white"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
I want stop this annoying auto scroll behavior.
Thanks.
java
Update:
I decide to create a github repo with this issue to anyone can try to reproduce it and try to fix it.
https://github.com/LolusKekus/NestsedRecyclerScrollItselfChallenge
I have vertical recycler with a bunch of nested horisontal recyclers.
When i'm scrolling over vertical recycler, nested recyclers scroll themself without any reason.
It's looks like this.
Before scroll, let's imagine that selector on 31:
_____________
|31 32 33 34|
|41 42 43 44|
|51 52 53 54|
_____________
now press up on dpad (i'm working on android tv app):
_____________
|24 25 26 27|
|31 32 33 34|
|41 42 43 44|
_____________
Hidden recyclerview appears, binds and scrolls up to 24 element. But it mustn't be! The selected item must be 21! And so on until the top of vertical recycler... 14... 04...
I'm noticed, nested recycler scrolls just to last visible item on it.
this behavior appears after "com.android.support:recyclerview-v7:24.2.1"
Now i'm using "com.android.support:recyclerview-v7:27.1.1" and faced with this issue.
Activity:
public class MainActivity extends AppCompatActivity {
List<String> hDataset;
List<Integer> vDataset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
hDataset = new ArrayList<>();
for (int i=0; i<hItemsCount; i++) {
hDataset.add(String.format("lolKek Cheburek %d", i));
}
vDataset = new ArrayList<>();
for (int i=0; i<vItemsCount; i++) {
vDataset.add(i);
}
vRecyclerView = findViewById(R.id.vRecyclerView);
vRecyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
vRecyclerView.setLayoutManager(layoutManager);
vRecyclerView.setAdapter(new VAdapter(vDataset, hDataset, context));
}
Vertical adapter:
public class VAdapter extends RecyclerView.Adapter<VAdapter.MyVertHolder>() {
private List<String> hDataset;
private Context context;
public VAdapter(List<String> hDataset, List<String> vDataset, Context context) {
this.hDataset = hDataset;
this.vDataset = vDataset;
this.context = context;
}
public static class MyVertHolder extends RecyclerView.ViewHolder {
RecyclerView hRecyclerView;
public MyVertHolder(final View itemView) {
super(itemView);
hRecyclerView = itemView.findViewById(R.id.hRecyclerView);
}
public void setAdapter(HAdapter adapter) {
hRecyclerView.setAdapter(adapter);
}
public void setLayoutManager(LinearLayoutManager layoutManager) {
hRecyclerView.setLayoutManager(layoutManager);
}
}
@NonNull
@Override
public MyVertHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater
.from(parent.getContext())
.inflate(R.layout.v_item_view, parent, false);
MyVertHolder holder = new MyVertHolder(itemView);
holder.setAdapter(new HAdapter(hDataset));
holder.setLayoutManager(new LinearLayoutManage(context, LinearLayoutManager.HORIZONTAL, false));
return holder;
}
@Override
public void onBindViewHolder(final @NonNull MyVertHolder holder, int position) {
... some bindings
}
}
Horisontal adapter:
class HAdapter extends RecyclerView.Adapter<HAdapter.MyHolder> {
private List<String> dataset;
public HAdapter(List<String> dataset) {
this.dataset = dataset;
}
public static class MyHolder extends RecyclerView.ViewHolder {
private TextView tvItemText;
public MyHolder(FrameLayout itemView) {
super(itemView);
tvItemText = itemView.findViewById(R.id.tvItemText);
}
}
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
FrameLayout itemView = (FrameLayout) LayoutInflater
.from(parent.getContext()).inflate(R.layout.item_view, parent, false);
return new MyHolder(itemView);
}
@Override
public void onBindViewHolder(final @NonNull MyHolder holder, final int position) {
holder.setItemText(dataset.get(position));
}
main activity layout:
<FrameLayout
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"
tools:context="MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/vRecyclerView"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
vertical item view layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/colorAccent"
android:id="@+id/hRecyclerView"
android:focusableInTouchMode="true"
android:focusable="true"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
horisontal item view layout:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/flContainer"
android:layout_margin="5dp"
android:focusable="true"
android:layout_width="250dp"
android:layout_height="150dp">
<TextView
android:id="@+id/tvItemText"
android:textColor="@color/white"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
I want stop this annoying auto scroll behavior.
Thanks.
java
java
edited 17 hours ago
LolusKekus
asked Jan 18 at 12:23
LolusKekusLolusKekus
34
34
None of the answers help yet. I decide to create a github repo with this issue to anyone can try to reproduce it and try to fix it. github.com/LolusKekus/NestsedRecyclerScrollItselfChallenge
– LolusKekus
17 hours ago
add a comment |
None of the answers help yet. I decide to create a github repo with this issue to anyone can try to reproduce it and try to fix it. github.com/LolusKekus/NestsedRecyclerScrollItselfChallenge
– LolusKekus
17 hours ago
None of the answers help yet. I decide to create a github repo with this issue to anyone can try to reproduce it and try to fix it. github.com/LolusKekus/NestsedRecyclerScrollItselfChallenge
– LolusKekus
17 hours ago
None of the answers help yet. I decide to create a github repo with this issue to anyone can try to reproduce it and try to fix it. github.com/LolusKekus/NestsedRecyclerScrollItselfChallenge
– LolusKekus
17 hours ago
add a comment |
3 Answers
3
active
oldest
votes
please remove
android:focusableInTouchMode="true"
android:focusable="true"
and let me know the status.
Nested horisontal feed stops focusing. But also nested horizontal recyclers stop scrolling when traverse over parent vertical recycler.
– LolusKekus
17 hours ago
add a comment |
You need to keep the state/position of each Horizontal RecyclerView's Adapter. When you are scrolling the vertical RecyclerView you are, obviously, recycling the views, therefor, if the horizontal adapter's state is not saved, when you scroll the vertical RecyclerView you will end up in some position for a scrolled Horizontal RecyclerView different than the one you were before the view was recycled.
Unfortunatly i'ts not working. Nested horizontal recyclers keep scrolling on their own.
– LolusKekus
17 hours ago
In theory should work. Maybe you should debug it to understand whats going on. Also check the vertical recyclerview recycling, the problem could be there
– Ricardo
17 hours ago
Yes, you right. Horizontal recycler don't scroll now, but focus keep moving to last visible item.
– LolusKekus
16 hours ago
Check my edited answer
– Ricardo
16 hours ago
add a comment |
Add this to your HAdapter class
private View.OnKeyListener mOnKeyListener = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
RecyclerView rv = (RecyclerView) v.getParent();
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
rv.getLayoutManager().scrollToPosition(0);
}
return false;
}
};
and
itemView.setOnKeyListener(mOnKeyListener);
into the onCreateViewHolder method of your HAdapter class.
This one is only for the Arrow Up key, you can extend the if clause to include the Arrow Down key as well.
Edit:
Alternatively, you can put that into the if clause instead of scrollToPosition method call:
private View.OnKeyListener mOnKeyListener = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
RecyclerView rv = (RecyclerView) v.getParent();
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
int pos = rv.getLayoutManager().getPosition(v);
rv.scrollBy(-((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, pos * width, mContext.getResources().getDisplayMetrics())), 0);
}
return false;
}
};
where width is the width of the RecyclerView item(s) in dp.
Suggestion:
I would move the setAdapter and setLayoutManager methods from the ViewHolder class into the HAdapter class body. (Do the same in the VAdapter class as well) They have nothing to do with the ViewHolder pattern. ViewHolder pattern is used to optimize performance by caching the views returned by findViewById()
Edit 2:
Add this class to your project:
https://raw.githubusercontent.com/wuakitv/leanback-v17/master/src/android/support/v17/leanback/widget/PersistentFocusWrapper.java
Then, modify your v_item_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<lolkek.example.com.testrecycler.PersistentFocusWrapper
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hRecyclerView"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</lolkek.example.com.testrecycler.PersistentFocusWrapper>
I tested it, and it works.
rv.getLayoutManager().scrollToPosition(0); - problem here is that i don't need to scroll to first element every time.
– LolusKekus
17 hours ago
See the section Edit 2 in my answer.
– burakk
13 hours ago
I shared the project on GitHub github.com/bkadirbeyoglu/NestedRecyclerViewsRememberPosition
– burakk
12 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54253974%2fandroid-nested-recyclerview-scrolling-itself-when-traverse-through-parent-recyc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
please remove
android:focusableInTouchMode="true"
android:focusable="true"
and let me know the status.
Nested horisontal feed stops focusing. But also nested horizontal recyclers stop scrolling when traverse over parent vertical recycler.
– LolusKekus
17 hours ago
add a comment |
please remove
android:focusableInTouchMode="true"
android:focusable="true"
and let me know the status.
Nested horisontal feed stops focusing. But also nested horizontal recyclers stop scrolling when traverse over parent vertical recycler.
– LolusKekus
17 hours ago
add a comment |
please remove
android:focusableInTouchMode="true"
android:focusable="true"
and let me know the status.
please remove
android:focusableInTouchMode="true"
android:focusable="true"
and let me know the status.
answered Jan 18 at 13:39
Sumanta BanerjeeSumanta Banerjee
417
417
Nested horisontal feed stops focusing. But also nested horizontal recyclers stop scrolling when traverse over parent vertical recycler.
– LolusKekus
17 hours ago
add a comment |
Nested horisontal feed stops focusing. But also nested horizontal recyclers stop scrolling when traverse over parent vertical recycler.
– LolusKekus
17 hours ago
Nested horisontal feed stops focusing. But also nested horizontal recyclers stop scrolling when traverse over parent vertical recycler.
– LolusKekus
17 hours ago
Nested horisontal feed stops focusing. But also nested horizontal recyclers stop scrolling when traverse over parent vertical recycler.
– LolusKekus
17 hours ago
add a comment |
You need to keep the state/position of each Horizontal RecyclerView's Adapter. When you are scrolling the vertical RecyclerView you are, obviously, recycling the views, therefor, if the horizontal adapter's state is not saved, when you scroll the vertical RecyclerView you will end up in some position for a scrolled Horizontal RecyclerView different than the one you were before the view was recycled.
Unfortunatly i'ts not working. Nested horizontal recyclers keep scrolling on their own.
– LolusKekus
17 hours ago
In theory should work. Maybe you should debug it to understand whats going on. Also check the vertical recyclerview recycling, the problem could be there
– Ricardo
17 hours ago
Yes, you right. Horizontal recycler don't scroll now, but focus keep moving to last visible item.
– LolusKekus
16 hours ago
Check my edited answer
– Ricardo
16 hours ago
add a comment |
You need to keep the state/position of each Horizontal RecyclerView's Adapter. When you are scrolling the vertical RecyclerView you are, obviously, recycling the views, therefor, if the horizontal adapter's state is not saved, when you scroll the vertical RecyclerView you will end up in some position for a scrolled Horizontal RecyclerView different than the one you were before the view was recycled.
Unfortunatly i'ts not working. Nested horizontal recyclers keep scrolling on their own.
– LolusKekus
17 hours ago
In theory should work. Maybe you should debug it to understand whats going on. Also check the vertical recyclerview recycling, the problem could be there
– Ricardo
17 hours ago
Yes, you right. Horizontal recycler don't scroll now, but focus keep moving to last visible item.
– LolusKekus
16 hours ago
Check my edited answer
– Ricardo
16 hours ago
add a comment |
You need to keep the state/position of each Horizontal RecyclerView's Adapter. When you are scrolling the vertical RecyclerView you are, obviously, recycling the views, therefor, if the horizontal adapter's state is not saved, when you scroll the vertical RecyclerView you will end up in some position for a scrolled Horizontal RecyclerView different than the one you were before the view was recycled.
You need to keep the state/position of each Horizontal RecyclerView's Adapter. When you are scrolling the vertical RecyclerView you are, obviously, recycling the views, therefor, if the horizontal adapter's state is not saved, when you scroll the vertical RecyclerView you will end up in some position for a scrolled Horizontal RecyclerView different than the one you were before the view was recycled.
edited 16 hours ago
answered Jan 18 at 14:28
RicardoRicardo
3,77011322
3,77011322
Unfortunatly i'ts not working. Nested horizontal recyclers keep scrolling on their own.
– LolusKekus
17 hours ago
In theory should work. Maybe you should debug it to understand whats going on. Also check the vertical recyclerview recycling, the problem could be there
– Ricardo
17 hours ago
Yes, you right. Horizontal recycler don't scroll now, but focus keep moving to last visible item.
– LolusKekus
16 hours ago
Check my edited answer
– Ricardo
16 hours ago
add a comment |
Unfortunatly i'ts not working. Nested horizontal recyclers keep scrolling on their own.
– LolusKekus
17 hours ago
In theory should work. Maybe you should debug it to understand whats going on. Also check the vertical recyclerview recycling, the problem could be there
– Ricardo
17 hours ago
Yes, you right. Horizontal recycler don't scroll now, but focus keep moving to last visible item.
– LolusKekus
16 hours ago
Check my edited answer
– Ricardo
16 hours ago
Unfortunatly i'ts not working. Nested horizontal recyclers keep scrolling on their own.
– LolusKekus
17 hours ago
Unfortunatly i'ts not working. Nested horizontal recyclers keep scrolling on their own.
– LolusKekus
17 hours ago
In theory should work. Maybe you should debug it to understand whats going on. Also check the vertical recyclerview recycling, the problem could be there
– Ricardo
17 hours ago
In theory should work. Maybe you should debug it to understand whats going on. Also check the vertical recyclerview recycling, the problem could be there
– Ricardo
17 hours ago
Yes, you right. Horizontal recycler don't scroll now, but focus keep moving to last visible item.
– LolusKekus
16 hours ago
Yes, you right. Horizontal recycler don't scroll now, but focus keep moving to last visible item.
– LolusKekus
16 hours ago
Check my edited answer
– Ricardo
16 hours ago
Check my edited answer
– Ricardo
16 hours ago
add a comment |
Add this to your HAdapter class
private View.OnKeyListener mOnKeyListener = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
RecyclerView rv = (RecyclerView) v.getParent();
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
rv.getLayoutManager().scrollToPosition(0);
}
return false;
}
};
and
itemView.setOnKeyListener(mOnKeyListener);
into the onCreateViewHolder method of your HAdapter class.
This one is only for the Arrow Up key, you can extend the if clause to include the Arrow Down key as well.
Edit:
Alternatively, you can put that into the if clause instead of scrollToPosition method call:
private View.OnKeyListener mOnKeyListener = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
RecyclerView rv = (RecyclerView) v.getParent();
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
int pos = rv.getLayoutManager().getPosition(v);
rv.scrollBy(-((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, pos * width, mContext.getResources().getDisplayMetrics())), 0);
}
return false;
}
};
where width is the width of the RecyclerView item(s) in dp.
Suggestion:
I would move the setAdapter and setLayoutManager methods from the ViewHolder class into the HAdapter class body. (Do the same in the VAdapter class as well) They have nothing to do with the ViewHolder pattern. ViewHolder pattern is used to optimize performance by caching the views returned by findViewById()
Edit 2:
Add this class to your project:
https://raw.githubusercontent.com/wuakitv/leanback-v17/master/src/android/support/v17/leanback/widget/PersistentFocusWrapper.java
Then, modify your v_item_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<lolkek.example.com.testrecycler.PersistentFocusWrapper
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hRecyclerView"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</lolkek.example.com.testrecycler.PersistentFocusWrapper>
I tested it, and it works.
rv.getLayoutManager().scrollToPosition(0); - problem here is that i don't need to scroll to first element every time.
– LolusKekus
17 hours ago
See the section Edit 2 in my answer.
– burakk
13 hours ago
I shared the project on GitHub github.com/bkadirbeyoglu/NestedRecyclerViewsRememberPosition
– burakk
12 hours ago
add a comment |
Add this to your HAdapter class
private View.OnKeyListener mOnKeyListener = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
RecyclerView rv = (RecyclerView) v.getParent();
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
rv.getLayoutManager().scrollToPosition(0);
}
return false;
}
};
and
itemView.setOnKeyListener(mOnKeyListener);
into the onCreateViewHolder method of your HAdapter class.
This one is only for the Arrow Up key, you can extend the if clause to include the Arrow Down key as well.
Edit:
Alternatively, you can put that into the if clause instead of scrollToPosition method call:
private View.OnKeyListener mOnKeyListener = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
RecyclerView rv = (RecyclerView) v.getParent();
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
int pos = rv.getLayoutManager().getPosition(v);
rv.scrollBy(-((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, pos * width, mContext.getResources().getDisplayMetrics())), 0);
}
return false;
}
};
where width is the width of the RecyclerView item(s) in dp.
Suggestion:
I would move the setAdapter and setLayoutManager methods from the ViewHolder class into the HAdapter class body. (Do the same in the VAdapter class as well) They have nothing to do with the ViewHolder pattern. ViewHolder pattern is used to optimize performance by caching the views returned by findViewById()
Edit 2:
Add this class to your project:
https://raw.githubusercontent.com/wuakitv/leanback-v17/master/src/android/support/v17/leanback/widget/PersistentFocusWrapper.java
Then, modify your v_item_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<lolkek.example.com.testrecycler.PersistentFocusWrapper
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hRecyclerView"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</lolkek.example.com.testrecycler.PersistentFocusWrapper>
I tested it, and it works.
rv.getLayoutManager().scrollToPosition(0); - problem here is that i don't need to scroll to first element every time.
– LolusKekus
17 hours ago
See the section Edit 2 in my answer.
– burakk
13 hours ago
I shared the project on GitHub github.com/bkadirbeyoglu/NestedRecyclerViewsRememberPosition
– burakk
12 hours ago
add a comment |
Add this to your HAdapter class
private View.OnKeyListener mOnKeyListener = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
RecyclerView rv = (RecyclerView) v.getParent();
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
rv.getLayoutManager().scrollToPosition(0);
}
return false;
}
};
and
itemView.setOnKeyListener(mOnKeyListener);
into the onCreateViewHolder method of your HAdapter class.
This one is only for the Arrow Up key, you can extend the if clause to include the Arrow Down key as well.
Edit:
Alternatively, you can put that into the if clause instead of scrollToPosition method call:
private View.OnKeyListener mOnKeyListener = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
RecyclerView rv = (RecyclerView) v.getParent();
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
int pos = rv.getLayoutManager().getPosition(v);
rv.scrollBy(-((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, pos * width, mContext.getResources().getDisplayMetrics())), 0);
}
return false;
}
};
where width is the width of the RecyclerView item(s) in dp.
Suggestion:
I would move the setAdapter and setLayoutManager methods from the ViewHolder class into the HAdapter class body. (Do the same in the VAdapter class as well) They have nothing to do with the ViewHolder pattern. ViewHolder pattern is used to optimize performance by caching the views returned by findViewById()
Edit 2:
Add this class to your project:
https://raw.githubusercontent.com/wuakitv/leanback-v17/master/src/android/support/v17/leanback/widget/PersistentFocusWrapper.java
Then, modify your v_item_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<lolkek.example.com.testrecycler.PersistentFocusWrapper
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hRecyclerView"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</lolkek.example.com.testrecycler.PersistentFocusWrapper>
I tested it, and it works.
Add this to your HAdapter class
private View.OnKeyListener mOnKeyListener = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
RecyclerView rv = (RecyclerView) v.getParent();
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
rv.getLayoutManager().scrollToPosition(0);
}
return false;
}
};
and
itemView.setOnKeyListener(mOnKeyListener);
into the onCreateViewHolder method of your HAdapter class.
This one is only for the Arrow Up key, you can extend the if clause to include the Arrow Down key as well.
Edit:
Alternatively, you can put that into the if clause instead of scrollToPosition method call:
private View.OnKeyListener mOnKeyListener = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
RecyclerView rv = (RecyclerView) v.getParent();
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
int pos = rv.getLayoutManager().getPosition(v);
rv.scrollBy(-((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, pos * width, mContext.getResources().getDisplayMetrics())), 0);
}
return false;
}
};
where width is the width of the RecyclerView item(s) in dp.
Suggestion:
I would move the setAdapter and setLayoutManager methods from the ViewHolder class into the HAdapter class body. (Do the same in the VAdapter class as well) They have nothing to do with the ViewHolder pattern. ViewHolder pattern is used to optimize performance by caching the views returned by findViewById()
Edit 2:
Add this class to your project:
https://raw.githubusercontent.com/wuakitv/leanback-v17/master/src/android/support/v17/leanback/widget/PersistentFocusWrapper.java
Then, modify your v_item_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<lolkek.example.com.testrecycler.PersistentFocusWrapper
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hRecyclerView"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</lolkek.example.com.testrecycler.PersistentFocusWrapper>
I tested it, and it works.
edited 13 hours ago
answered Jan 18 at 14:14
burakkburakk
1402729
1402729
rv.getLayoutManager().scrollToPosition(0); - problem here is that i don't need to scroll to first element every time.
– LolusKekus
17 hours ago
See the section Edit 2 in my answer.
– burakk
13 hours ago
I shared the project on GitHub github.com/bkadirbeyoglu/NestedRecyclerViewsRememberPosition
– burakk
12 hours ago
add a comment |
rv.getLayoutManager().scrollToPosition(0); - problem here is that i don't need to scroll to first element every time.
– LolusKekus
17 hours ago
See the section Edit 2 in my answer.
– burakk
13 hours ago
I shared the project on GitHub github.com/bkadirbeyoglu/NestedRecyclerViewsRememberPosition
– burakk
12 hours ago
rv.getLayoutManager().scrollToPosition(0); - problem here is that i don't need to scroll to first element every time.
– LolusKekus
17 hours ago
rv.getLayoutManager().scrollToPosition(0); - problem here is that i don't need to scroll to first element every time.
– LolusKekus
17 hours ago
See the section Edit 2 in my answer.
– burakk
13 hours ago
See the section Edit 2 in my answer.
– burakk
13 hours ago
I shared the project on GitHub github.com/bkadirbeyoglu/NestedRecyclerViewsRememberPosition
– burakk
12 hours ago
I shared the project on GitHub github.com/bkadirbeyoglu/NestedRecyclerViewsRememberPosition
– burakk
12 hours ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54253974%2fandroid-nested-recyclerview-scrolling-itself-when-traverse-through-parent-recyc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
None of the answers help yet. I decide to create a github repo with this issue to anyone can try to reproduce it and try to fix it. github.com/LolusKekus/NestsedRecyclerScrollItselfChallenge
– LolusKekus
17 hours ago