更新: 我决定为这个问题创建一个 github 存储库,任何人都可以尝试重现它并尝试修复它。 https://github.com/LolusKekus/NestsedRecyclerScrollItselfChallenge
Update2 花一些时间研究,我终于找到了满足我要求的解决方案。
我需要的只是扩展 recyclerview类并覆盖 forcusSearch方法,如下所示:
@Override
public View focusSearch(View focused, int direction) {
View view = super.focusSearch(focused, direction);
Log.d(TAG, "focusSearch: ");
if (view instanceof CRecyclerView) {
int position = ((LinearLayoutManager)((CRecyclerView) view).getLayoutManager()).findFirstVisibleItemPosition();
view = ((CRecyclerView) view).getLayoutManager().findViewByPosition(position);
}
return view;
}
CRecyclerView - 我的扩展 RecyclerView。
在正常情况下,focusSearch 返回我的项目视图,一切都很好。但由于某些原因,focusSearch 返回 RecyclerView 实例,结果我有这个烦人的自动滚动。我没有深入研究这个问题,因为我已经得到了我需要的一切。但如果有人可以,我将不胜感激。
我有一个带有一堆嵌套水平回收器的垂直回收器。当我在垂直回收器上滚动时,嵌套的回收器会毫无理由地自行滚动。
它看起来像这样。在滚动之前,让我们想象一下 31 上的选择器:
_____________
|31 32 33 34|
|41 42 43 44|
|51 52 53 54|
_____________
现在按下 dpad(我正在开发 android tv 应用程序):
_____________
|24 25 26 27|
|31 32 33 34|
|41 42 43 44|
_____________
隐藏的 recyclerview 出现,绑定和滚动多达 24 个元素。但一定不是!选择的项目必须是21!以此类推,直到立式回收机顶部... 14... 04...
我注意到,嵌套的回收器滚动到最后一个可见的项目。
此行为出现在“com.android.support:recyclerview-v7:24.2.1”之后
现在我正在使用“com.android.support:recyclerview-v7:27.1.1”并面临这个问题。
活动:
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));
}
垂直适配器:
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
}
}
水平适配器:
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));
}
主要活动布局:
<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>
垂直项目视图布局:
<?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"/>
水平项目视图布局:
<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>
我想停止这种烦人的自动滚动行为。谢谢。