RecyclerView inside ScrollView is not working

前端 未结 26 1546
梦如初夏
梦如初夏 2020-11-22 05:37

I\'m trying to implement a layout which contains RecyclerView and ScrollView at the same layout.

Layout template:


    

        
26条回答
  •  逝去的感伤
    2020-11-22 06:07

    RecyclerViews are fine to put in ScrollViews so long as they aren't scrolling themselves. In this case, it makes sense to make it a fixed height.

    The proper solution is to use wrap_content on the RecyclerView height and then implement a custom LinearLayoutManager that can properly handle the wrapping.

    Copy this LinearLayoutManager into your project: https://github.com/serso/android-linear-layout-manager/blob/master/lib/src/main/java/org/solovyev/android/views/llm/LinearLayoutManager.java

    Then wrap the RecyclerView:

    
    

    And set it up like so:

        RecyclerView list = (RecyclerView)findViewById(R.id.list);
        list.setHasFixedSize(true);
        list.setLayoutManager(new com.example.myapp.LinearLayoutManager(list.getContext()));
        list.setAdapter(new MyViewAdapter(data));
    

    Edit: This can cause complications with scrolling because the RecyclerView can steal the ScrollView's touch events. My solution was just to ditch the RecyclerView in all and go with a LinearLayout, programmatically inflate subviews, and add them to the layout.

提交回复
热议问题