Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RecyclerView的奇怪行为_Java_Android_Xml_Android Recyclerview - Fatal编程技术网

Java RecyclerView的奇怪行为

Java RecyclerView的奇怪行为,java,android,xml,android-recyclerview,Java,Android,Xml,Android Recyclerview,例如,我有以下代码 MainActivity.java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

例如,我有以下代码

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);
    }
}
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);

        }
    }
}

除非您有充分的理由在层次结构中嵌套一个
NestedScrollView
,否则您可以在顶部实现一个TextView,下面是一个RecyclerView,只需此布局代码即可:

<?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:orientation="vertical"
    tools:context="com.example.mrmayhem.myapplication.MainActivity">

    <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>

除非您有充分的理由在层次结构中嵌套一个
滚动视图
,否则您可以通过以下布局代码在顶部实现一个带有RecyclerView的TextView:

<?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:orientation="vertical"
    tools:context="com.example.mrmayhem.myapplication.MainActivity">

    <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>

RecyclerView是一个列表视图。它将只显示当前可以看到的项目,在滚动时重复使用零件

现在将RecyclerView包装在
嵌套滚动视图中的
线性布局中
,会发生什么

NestedScrollView
需要知道
LinearLayout
的大小才能处理滚动。
LinearLayout
如何知道它应该有多长,而不知道RecyclerViews的总长度?没有

因此,recyclerView完全膨胀,所有视图同时显示,所有视图排成一条长队。没有回收,没有再利用。这就是你所经历的现象。
现在,
LinearLayout
可以正确地告诉
NestedScrollView
它的高度(TextView和RecyclerView的所有项目的总高度),而
NestedScrollView
可以处理滚动


这就是为什么嵌套滚动总是一个坏主意。如果可能,尝试将您的
TextView
作为一个项目移动到RecyclerView中,并摆脱包装
NestedScrollview

e、 g.如图所示:


另一个选项是从滚动视图中删除recyclerview并将其放在下面。不过,此时标题不会滚动。

RecyclerView是一个列表视图。它将只显示当前可以看到的项目,在滚动时重复使用零件

现在将RecyclerView包装在
嵌套滚动视图中的
线性布局中
,会发生什么

NestedScrollView
需要知道
LinearLayout
的大小才能处理滚动。
LinearLayout
如何知道它应该有多长,而不知道RecyclerViews的总长度?没有

因此,recyclerView完全膨胀,所有视图同时显示,所有视图排成一条长队。没有回收,没有再利用。这就是你所经历的现象。
现在,
LinearLayout
可以正确地告诉
NestedScrollView
它的高度(TextView和RecyclerView的所有项目的总高度),而
NestedScrollView
可以处理滚动


这就是为什么嵌套滚动总是一个坏主意。如果可能,尝试将您的
TextView
作为一个项目移动到RecyclerView中,并摆脱包装
NestedScrollview

e、 g.如图所示:


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

这是一个很好的解释。它真的很累。我按照说明做了,并添加了标题,一切正常,但我的最后一个项目不完全适合回收视图。最后一个项目是在页眉高度上裁剪的,这是一个很好的解释。这真的很累人。我按照要求做了,并添加了页眉,一切都很好,但我的最后一个项目并不完全适合回收视图。最后一项根据收割台的高度进行裁剪,
<?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>
<?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:orientation="vertical"
    tools:context="com.example.mrmayhem.myapplication.MainActivity">

    <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>