Android 在NestedScrollView中使用视图寻呼机,列表视图不在片段中滚动

Android 在NestedScrollView中使用视图寻呼机,列表视图不在片段中滚动,android,listview,android-viewpager,android-nestedscrollview,Android,Listview,Android Viewpager,Android Nestedscrollview,大家好,我需要创建像whatsapp这样的布局,它可以滚动操作栏,但不能进行表格布局。我使用viewpager来加载其中的片段。 在片段中,我添加了listview,但在listview上,滚动操作栏并没有滚动到那个里。 通过使用有关stackoverflow的一些教程,我了解到为此需要使用nestedScrollView,而且它对我很有用。 但它停止滚动listview。 我用了这个密码 activity_my.xml MyActivity.java 公共类MyActivity扩展了App

大家好,我需要创建像whatsapp这样的布局,它可以滚动操作栏,但不能进行表格布局。我使用viewpager来加载其中的片段。 在片段中,我添加了listview,但在listview上,滚动操作栏并没有滚动到那个里。 通过使用有关stackoverflow的一些教程,我了解到为此需要使用nestedScrollView,而且它对我很有用。 但它停止滚动listview。 我用了这个密码

activity_my.xml

MyActivity.java
公共类MyActivity扩展了AppCompatActivity{
私人摊位摊位;
私人视页机视页机;
ArrayList片段ArrayList;
TabAdapter TabAdapter;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
/*Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
getSupportActionBar().setDisplayHomeAsUpEnabled(false)*/
viewPager=(viewPager)findViewById(R.id.pager);
设置viewPager(viewPager);
tabLayout=(tabLayout)findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
专用无效设置ViewPager(ViewPager ViewPager){
TabAdapter=新TabAdapter(getSupportFragmentManager());
addFragment(新的OneFragment(),“一”);
addFragment(新的OneFragment(),“TWO”);
addFragment(新的OneFragment(),“三”);
viewPager.setAdapter(适配器);
}
}
FragmentOne.java
public类OneFragment扩展了Fragment{
列表视图列表测试;
ArrayList项目列表;
阵列适配器;
公共OneFragment(){
//必需的空公共构造函数
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u one,容器,错误);
listTest=(ListView)view.findViewById(R.id.listTest);
itemList=新的ArrayList();
对于(int i=0;i<50;i++){
项目列表。添加(“项目:+i”);
}
adapter=newarrayadapter(getActivity(),android.R.layout.simple\u list\u item\u 1,itemList);
setAdapter(适配器);
返回视图;
}
}
fragment_one.xml

我使用LinearLayout monHeight=1000dp,因为如果在nestedScrollView中使用android:fillViewPort=“true”并从LinearLayout中删除minHeight,则listview会显示,但不会滚动。 现在它滚动,但直到1000dp,然后其他项目不显示


您可以使用Observable scrollview

好的。首先,您在
嵌套的scrollview
内部扭曲了
ViewPager
,这是不必要的

所以改变

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="1000dp"
        android:orientation="vertical">

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>
只需将你的片段更改为一个liske this

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <packagename.NonScrollListView
        android:id="@+id/listTest"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></packagename.NonScrollListView>
</android.support.v4.widget.NestedScrollView>

我希望这能帮到你。
快乐编码

尝试使用recyclerview而不是ListView它对我也不起作用我也使用了它…你应该对此进行评论。。!!不客气。。如果真的对你有帮助。。你可以接受;)这确实有效。但是,关于创建
heightMeasureSpec\u custom
的更改实际做了什么,有什么解释吗?当然,只要调用您创建或修改的
onMeasure
列表。默认情况下,它将ListView包装为2个项目。因此,我们要做的是计算listview可以采用的最大高度,
Integer.MAX\u VALUE>>2
将实现这一技巧。因此,通过编写
int heightMeasureSpec\u custom=MeasureSpec.makeMeasureSpec(Integer.MAX\u VALUE>>2,MeasureSpec.AT\u最大值)这一行我们得到实际高度(直到listview的最后一项),我们通过编写
super.onMeasure(widthMeasureSpec,heightMeasureSpec_custom)简单地用
heightMeasureSpec\u自定义来覆盖默认高度。我希望这是有帮助的
public class OneFragment extends Fragment {
ListView listTest;
ArrayList<String> itemList;
ArrayAdapter<String> adapter;

public OneFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_one, container, false);
    listTest = (ListView) view.findViewById(R.id.listTest);
    itemList = new ArrayList<>();
    for (int i = 0; i < 50; i++) {
        itemList.add("Item : " + i);
    }
    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, itemList);
    listTest.setAdapter(adapter);
    return view;
   }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/listTest"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</RelativeLayout>
<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="1000dp"
        android:orientation="vertical">

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.v4.view.ViewPager
     android:id="@+id/pager"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_weight="1"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
public class NonScrollListView extends ListView {

    public NonScrollListView(Context context) {
         super(context);
    }

    public NonScrollListView(Context context, AttributeSet attrs) {
         super(context, attrs);
    }

    public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
         super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
         super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
         ViewGroup.LayoutParams params = getLayoutParams();
         params.height = getMeasuredHeight();
    }
}
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <packagename.NonScrollListView
        android:id="@+id/listTest"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></packagename.NonScrollListView>
</android.support.v4.widget.NestedScrollView>