我见过许多这样的帖子,但是他们没有帮助我。当我在NestedScrollView中使用RecyclerView我有滚动问题 - 卡住了。我知道它,因为滚动内滚动,
rv.setNestedScrollingEnabled(false);
对我不起作用。
在几个线程上有信息,不必使用NestedScrollView - 但随后工具栏不会崩溃。
我的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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleGravity="bottom"
app:expandedTitleMarginStart="@dimen/activity_vertical_margin"
app:expandedTitleTextAppearance="@style/CollapsedAppBarTopic"
app:layout_collapseMode="parallax"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_collapseMode="parallax">
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:alpha="0.5"
app:layout_collapseMode="parallax"
app:srcCompat="@drawable/logo_white" />
<TextView
android:id="@+id/yourVotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:layout_toLeftOf="@+id/votesSum"
android:layout_toStartOf="@+id/votesSum"
android:text="@string/YourVotes"
android:textAlignment="viewStart" />
<TextView
android:id="@+id/votesSum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:text="1" />
</RelativeLayout>
</FrameLayout>
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarDetails"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorBackground">
<TableRow>
<TextView
android:layout_weight="1"
android:elevation="1dp"
android:text="@string/VotesSum"
android:textAlignment="center" />
<TextView
android:layout_weight="1"
android:elevation="0dp"
android:text="@string/TodayVotes"
android:textAlignment="center" />
<TextView
android:layout_weight="1"
android:text="@string/TodayAdded"
android:textAlignment="center" />
</TableRow>
<TableRow>
<TextView
android:layout_weight="1"
android:text="1"
android:textAlignment="center" />
<TextView
android:id="@+id/textView3"
android:layout_weight="1"
android:text="2"
android:textAlignment="center" />
<TextView
android:layout_weight="1"
android:text="3"
android:textAlignment="center" />
</TableRow>
</TableLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/tracks_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:background="@color/colorBackground" />
<ProgressBar
android:id="@+id/progressBarDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:background="@android:color/transparent" />
</LinearLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/colorPrimary"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@drawable/bottom_nav_colors"
app:itemTextColor="@drawable/bottom_nav_colors"
app:menu="@menu/navigation" />
</android.support.design.widget.CoordinatorLayout>
所以,请帮助我完成这项工作
public class PlaylistDetailActivityWithoutFragmet extends AppCompatActivity {
private TextView votesLeft;
private RecyclerView lvTracks;
private SinglePlaylistFragment_.OnFragmentInteractionListener mListener;
SinglTracksAdapter recyclerViewAdapter;
final ArrayList<PlaylistTracks> playlistsTracks = new ArrayList<PlaylistTracks>();
final ArrayList<PlaylistTracks> playlistsTracks2 = new ArrayList<PlaylistTracks>();
int lastId = 0;
private boolean loading = true;
ProgressBar progressBar;
Toolbar toolbarDetails;
CollapsingToolbarLayout collapsingToolbarLayout;
int firstVisibleItem, visibleItemCount, totalItemCount;
private int visibleThreshold = 2;
final LinearLayoutManager llm = new LinearLayoutManager(this);
int height;
NestedScrollView nestedScrollView;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
// mTextMessage.setText(R.string.Playlist);
return true;
case R.id.navigation_dashboard:
// mTextMessage.setText(R.string.Statistics);
return true;
case R.id.navigation_notifications:
// mTextMessage.setText(R.string.Users);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_coordinator_layout);
toolbarDetails = (Toolbar) findViewById(R.id.toolbarDetails);
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
setSupportActionBar(toolbarDetails);
collapsingToolbarLayout.setTitle(playlistName);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbarDetails.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_black_24dp));
toolbarDetails.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mWebSocketClient.close();
onBackPressed();
}
});
llm.setOrientation(LinearLayoutManager.VERTICAL);
// nestedScrollView = (NestedScrollView) findViewById(R.id.nestedScroll);
lvTracks = (RecyclerView) findViewById(R.id.tracks_recycler_view);
lvTracks.setLayoutManager(llm);
lvTracks.setNestedScrollingEnabled(false);
// nestedScrollView.setSmoothScrollingEnabled(true);
progressBar = (ProgressBar) findViewById(R.id.progressBarDetails);
progressBar.setVisibility(View.GONE);
loading = true;
getTracks();
lvTracks.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = llm.getChildCount();
totalItemCount = llm.getItemCount();
firstVisibleItem = llm.findFirstVisibleItemPosition();
Log.i("dx", String.valueOf(dx));
Log.i("dy", String.valueOf(dy));
Log.i("visibleItemCount", String.valueOf(visibleItemCount));
Log.i("firstVisibleItem", String.valueOf(firstVisibleItem));
Log.i("totalItemCount", String.valueOf(totalItemCount));
if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
loading = true;
getMoreTracks(1, 2);
}
}
});
//
// nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
// @Override
// public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
// if (!loading && v.getChildAt(v.getChildCount() - 1) != null) {
// if ((scrollY >= (v.getChildAt(v.getChildCount() - 1).getMeasuredHeight() - v.getMeasuredHeight())) &&
// scrollY > oldScrollY) {
// progressBar.setVisibility(View.VISIBLE);
// loading = true;
//
// getMoreTracks(scrollX, scrollY);
//
//
// }
// }
// }
// });
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
@Override
public void onBackPressed() {
mWebSocketClient.close();
super.onBackPressed();
}
}
}
更新了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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleGravity="bottom"
app:expandedTitleMarginStart="@dimen/activity_vertical_margin"
app:expandedTitleTextAppearance="@style/CollapsedAppBarTopic"
app:layout_collapseMode="parallax"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_collapseMode="parallax">
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:alpha="0.5"
app:layout_collapseMode="parallax"
app:srcCompat="@drawable/logo_white" />
<TextView
android:id="@+id/yourVotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:layout_toLeftOf="@+id/votesSum"
android:layout_toStartOf="@+id/votesSum"
android:text="@string/YourVotes"
android:textAlignment="viewStart" />
<TextView
android:id="@+id/votesSum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:text="1" />
</RelativeLayout>
</FrameLayout>
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarDetails"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/tracks_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="60dp"
android:background="@color/colorBackground"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/colorPrimary"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@drawable/bottom_nav_colors"
app:itemTextColor="@drawable/bottom_nav_colors"
app:menu="@menu/navigation" />
</android.support.design.widget.CoordinatorLayout>
答案 0 :(得分:1)
由于最终目标是在您滚动recyclerView时让ToolBar diasappear / collapse崩溃,所以不应使用NestedScrollView,而应使用CoordinatorLayout。
看看here。
以下是我提供的有关如何创建折叠工具栏的文档的快速示例。
首先在你的xml中:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
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"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"></android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
确保在您的活动中,在onCreate()
中设置正确的方式:
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle("Title");
希望它有所帮助。
答案 1 :(得分:0)
您所要做的就是在活动类中使用以下行:
ViewCompat.setNestedScrollingEnabled(recycler_view, false);
它兼容较低版本。如果你想兼容API&gt; 21,那么只能使用;
recycler_view.setNestedScrollingEnabled(false);