2

嗨,我需要创建像whatsapp,滚动操作栏,但不是tabLayout的布局。我用viewpager加载了片段。 在片段中,我添加了listview,但在Listview上滚动该操作栏不滚动那里。 在stackoverflow上使用一些教程我得到了这个目的需要使用nestedScrollView它适用于我。 但它停止滚动listview。 我用这个代码。列表视图不使用浏览页面滚动内部NestedScrollView

activity_my.xml
<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/main_content" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"></android.support.design.widget.CollapsingToolbarLayout> 

</android.support.design.widget.AppBarLayout> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:layout_gravity="bottom" 
    app:layout_anchor="@+id/appbar" 
    app:layout_anchorGravity="bottom" 
    app:layout_collapseMode="none"> 

    <ImageView 
     android:id="@+id/tabBg" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/primary" 
     android:scaleType="fitXY" /> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:layout_gravity="bottom" 
     android:background="#00000000" 
     app:layout_anchor="@+id/appbar" 
     app:layout_anchorGravity="bottom" 
     app:layout_collapseMode="none" 
     app:tabGravity="fill" 
     app:tabIndicatorColor="#fff" 
     app:tabIndicatorHeight="2dp" 
     app:tabMode="scrollable" 
     app:tabSelectedTextColor="#fff" 
     app:tabTextColor="#fff" /> 

</FrameLayout> 
<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_marginTop="?attr/actionBarSize" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />--> 
</android.support.design.widget.CoordinatorLayout> 

MyActivity.java
public class MyActivity extends AppCompatActivity { 
private TabLayout tabLayout; 
private ViewPager viewPager; 
ArrayList<Fragment> fragmentArrayList; 
TabAdapter tabAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my); 
    /*Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);*/ 

    viewPager = (ViewPager) findViewById(R.id.pager); 
    setupViewPager(viewPager); 

    tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(viewPager); 
} 

private void setupViewPager(ViewPager viewPager) { 
    TabAdapter adapter = new TabAdapter(getSupportFragmentManager()); 
    adapter.addFragment(new OneFragment(), "ONE"); 
    adapter.addFragment(new OneFragment(), "TWO"); 
    adapter.addFragment(new OneFragment(), "THREE"); 
    viewPager.setAdapter(adapter); 
    } 

} 

FragmentOne.java
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; 
    } 
} 

fragment_one.xml
<?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> 

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

+0

尝试使用recyclerview而不是listview –

+0

它也不适用于我我使用,以及... – Rushvi

回答

10

欧凯首先你扭曲ViewPagerNestedScrollView,这是没有必要的。

所以更改,

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

现在你正在服用ListView,这是不符合AppBarLayout工作好,所以我建议你使用RecyclerView代替。

注:,如果你仍然想使用ListView你可以使用NonScrollListview包木窗它NestedScrollView下。

NonScrollListView

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

而只是改变你的fragment_one liske这一点。

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

我希望这会帮助你。 快乐编码..

+0

嘿它的伟大...................非常感谢.... – Rushvi

+0

不客气..如果它确实帮助你..你可以接受它;) –

+0

工作得很好。大。 –

相关问题