您已经在我看到的问题中得到了一些回复。我想这些都是针对你的问题,这个问题实际上并不清楚,你试图达到的解决方案也不是这样做的正确方法。
从您的布局代码中,我明白了您需要一个向上箭头按钮,当点击该按钮时,它将滚动到RecyclerView
的顶部,并且您希望该按钮保持在页面底部'你需要你的RecyclerView
滚动,箭头按钮保持原状。如果这是你的情况,那么你需要重新考虑布局和流程设计。您需要使RecyclerView
和箭头按钮完全脱节。
因此,根据我对您的问题的假设,您需要一个Activity
,其中包含一个浮动操作按钮并保存Fragment
。 Fragment
将使RecyclerView
使浮动动作按钮(用于向上箭头)和RecyclerView
完全不相交。
所以你需要有一个Activity
和Activity
应该是这样的布局。
<RelativeLayout 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">
<RelativeLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="@dimen/activity_horizontal_margin"
android:src="@drawable/ic_up_arrow"
app:backgroundTint="@color/colorPrimary"
app:borderWidth="0dp"
app:elevation="4dp"
app:fabSize="normal" />
</RelativeLayout>
而且Activity
的onCreate
功能应该是这样的。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mFabUpArrow = (FloatingActionButton) findViewById(R.id.fab_up);
mFabUpArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
jumpToTopOfTheRecyclerView();
}
});
// Now launch the Fragment from here.
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new RecyclerViewFragment()).commit();
}
现在RecyclerViewFragment
应具有以下布局保持了RecyclerView
<?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="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/your_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
我想你已经知道如何填充您的RecyclerView
,做那件事在你Fragment
。
您可能会问如何在Activity
中实现jumpToTopOfTheRecyclerView()
函数。它很简单,你有几个选择。一种方法可能是在Fragment
中输入BroadcastReceiver
,然后从Activity
发送广播,这不是一个好方法。您可能会发现在您的Activity
和Fragment
之间非常容易沟通。祝你好运。
这是正确的答案,谢谢! – idish
@idish欢迎您,很高兴我能提供帮助 –