美文网首页
解决NestedScrollView 嵌套 RecyclerVi

解决NestedScrollView 嵌套 RecyclerVi

作者: 快乐小哥 | 来源:发表于2017-09-15 15:36 被阅读96次
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    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:id="@+id/scrollView"
    tools:context="com.zhongxin.nestscrolledview.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"/>
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"/>
    </LinearLayout>
    

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

代码如下

public class MainActivity extends AppCompatActivity {
    private NestedScrollView scrollView;
    private RecyclerView recyclerView;

    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scrollView = (NestedScrollView) findViewById(R.id.scrollView);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new DemoAdapter(this));
        recyclerView.setNestedScrollingEnabled(false);
    }
}

注意:setNestedScrollingEnabled该方法调用 设置为false即可实现嵌套的,NestedScrollView是专门为了兼容Mc design控件而生的

1.jpg 2.jpg

相关文章

网友评论

      本文标题:解决NestedScrollView 嵌套 RecyclerVi

      本文链接:https://www.haomeiwen.com/subject/neossxtx.html