gpt4 book ai didi

java - RecyclerView 的奇怪行为

转载 作者:行者123 更新时间:2023-12-02 02:57:25 28 4
gpt4 key购买 nike

例如,我有以下代码。

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

List<String> names = new ArrayList<>();
names.add("one");
names.add("two");
names.add("three");
names.add("four");
names.add("five");
names.add("six");
names.add("seven");
names.add("eight");
names.add("nine");
names.add("ten");

RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);

RvAdapter adapter = new RvAdapter(names);
rv.setAdapter(adapter);
}
}

RvAdapter.java

public class RvAdapter extends RecyclerView.Adapter<RvAdapter.PersonViewHolder> {
public static final String TAG = RvAdapter.class.getSimpleName();
List<String> persons;

RvAdapter(List<String> persons) {
this.persons = persons;
}

@Override
public RvAdapter.PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new PersonViewHolder(v);
}

@Override
public void onBindViewHolder(RvAdapter.PersonViewHolder holder, int position) {
holder.name.setText(persons.get(position));
Log.d(TAG, "onBindViewHolder: " + position);
}

@Override
public int getItemCount() {
return persons.size();
}

public static class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView name;

PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
name = (TextView) itemView.findViewById(R.id.name);

}
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.mrmayhem.myapplication.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>
</RelativeLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">

<android.support.v7.widget.CardView
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="200dp">

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
</LinearLayout>

它工作得很好而且正确。当我滚动列表时,我在日志中一一收到来自适配器的连续消息:“onBindViewHolder:”+位置。但接下来我的问题是。当我尝试在 RecyclerView 上方添加一些 View 时,如下面的代码所示。适配器同时显示onBindViewHolder方法的所有调用。通过下一个代码将 RecyclerView 包装在 activity_main.xml 中。

activity_main.xml(带标题)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.mrmayhem.myapplication.MainActivity">

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HEADER" />

<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>
</LinearLayout>

</android.support.v4.widget.NestedScrollView>

</RelativeLayout>

最佳答案

RecyclerView是一个ListView。它只会显示您当前可以看到的项目,并在滚动时重复使用部分内容。

现在您将 RecyclerView 包装在 LinearLayout 中在NestedScrollView内,会发生什么?

一个NestedScrollView需要知道 LinearLayout大小处理滚动。
LinearLayout如何在不知道 RecyclerViews 总长度的情况下知道它应该有多长? 事实并非如此。

因此,recyclerView 完全膨胀,所有 View 同时出现,全部排成一长串。没有回收,没有再利用。这就是您所经历的现象。
现在LinearLayout可以正确告诉NestedScrollView它的高度(TextView 和 RecyclerView 的所有项目的总高度)和 NestedScrollView可以处理滚动。

<小时/>

这就是为什么嵌套滚动总是是一个坏主意。如果可能的话,尝试移动您的TextView作为项目进入 RecyclerView 并摆脱包装 NestedScrollview .
例如如下所示:Is there an addHeaderView equivalent for RecyclerView?

另一个选择是从 ScrollView 中删除 recyclerview 并将其放在下面。不过,标题不会滚动。

关于java - RecyclerView 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42873347/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com