我在滚动视图时遇到了一种非常奇怪的行为。片段" A"使用RecyclerView和Fragment" B"使用listView。
对于Recycler视图,我使用以下代码:
list.setOnScrollListener(new RecyclerView.OnScrollListener() {
int mLastFirstVisibleItem = 0;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
final int currentFirstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
if (currentFirstVisibleItem > this.mLastFirstVisibleItem) {
((AppCompatActivity)getActivity()).getSupportActionBar().hide();
} else if (currentFirstVisibleItem < this.mLastFirstVisibleItem) {
((AppCompatActivity)getActivity()).getSupportActionBar().show();
}
this.mLastFirstVisibleItem = currentFirstVisibleItem;
}
});
对于ListView,我使用以下代码:
list.setOnScrollListener(new AbsListView.OnScrollListener() {
int mLastFirstVisibleItem = 0;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) { }
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (view.getId() == list.getId()) {
final int currentFirstVisibleItem = list.getFirstVisiblePosition();
if (currentFirstVisibleItem > mLastFirstVisibleItem) {
// getSherlockActivity().getSupportActionBar().hide();
((AppCompatActivity)getActivity()).getSupportActionBar().hide();
} else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
// getSherlockActivity().getSupportActionBar().show();
((AppCompatActivity)getActivity()).getSupportActionBar().show();
}
mLastFirstVisibleItem = currentFirstVisibleItem;
}
}
});
以下是GIF,所以,基本上,我滚动并按住它并且不要释放我的手指,我会得到以下行为。
答案 0 :(得分:1)
如果您正在寻找的appBar行为类似于WhatsApp,我认为更好的方法是从您的布局而不是Java中控制appBar行为。下面是一个如何为包含RecyclerView的片段和包含ListView的片段执行此操作的示例:
<强> activity_main.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:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbarlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways|snap"/>
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<强> fragmenta.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.RecyclerView
android:id="@+id/rec_fragmenta"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:splitMotionEvents="false"
android:clipToPadding="false"/>
</android.support.design.widget.CoordinatorLayout>
<强> fragmentb.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:id="@+id/list_fragmentb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="true"/>
</android.support.design.widget.CoordinatorLayout>
<强> MainActivity.java 强>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
SectionsPagerAdapter mSectionsPagerAdapter = new
SectionsPagerAdapter(getSupportFragmentManager());
ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout.setupWithViewPager(mViewPager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
private class SectionsPagerAdapter extends FragmentPagerAdapter {
private SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentA();
case 1:
return new FragmentB();
case 2:
return new FragmentC();
}
return null;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "A";
case 1:
return "B";
case 2:
return "C";
}
return null;
}
}
}
<强> FragmentA 强>
public class FragmentA extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmenta, container, false);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
RecyclerView recyclerView = rootView.findViewById(R.id.rec_fragmenta);
recyclerView.setLayoutManager(layoutManager);
List<String> list = getList();
MyListAdapter adapter = new MyListAdapter(list);
recyclerView.setAdapter(adapter);
return rootView;
}
}
<强> FragmentB 强>
public class FragmentB extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragmentb, container, false);
ListView listView = rootView.findViewById(R.id.list_fragmentb);
List<String> list = getList();
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, list);
listView.setAdapter(adapter);
return rootView;
}
}
答案 1 :(得分:0)
隐藏Toolbar
后,ListView
&amp; RecyclerView
被扩展到顶部,再次显示第一个项目,因此再次显示Toolbar
。
您应该检查变量dy
是正面还是负面;如果dy
为肯定,则用户向上滚动,否则向下滚动。如果我们采用这个代码就变成这样:
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
final int currentFirstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
if (currentFirstVisibleItem > this.mLastFirstVisibleItem && dy > 0) {
((AppCompatActivity)getActivity()).getSupportActionBar().hide();
} else if (currentFirstVisibleItem < this.mLastFirstVisibleItem && dy < 0) {
((AppCompatActivity)getActivity()).getSupportActionBar().show();
}
this.mLastFirstVisibleItem = currentFirstVisibleItem;
}
或者你可以忽略firstVisibleItem
之类的属性,也可以使用dy
(如果你真的不需要检查第一项是否显示):
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
((AppCompatActivity)getActivity()).getSupportActionBar().hide();
} else if (dy < 0) {
((AppCompatActivity)getActivity()).getSupportActionBar().show();
}
}