我在main_activity.xml
中使用了以下代码来添加Floating Button
:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/ic_refresh" />
我使用以下方法在RecyclerView
中加载数据:
public void getWarehouse(){
.....
//Some retrofit calls
.....
mRecyclerView = (RecyclerView) mActivity.findViewById(R.id.staggering_grid);
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
mAdapter = new StaggeredGridAdapter(mContext);
mAdapter.addItems(response);
mRecyclerView.setAdapter(mAdapter);
GridItemSpaces decoration = new GridItemSpaces(10);
mRecyclerView.addItemDecoration(decoration);
}
getWareHouse()
方法的目的:
在RecyclerView Staggered Grid View
。
FloatingButton
的目的:
从API calls
刷新数据。简而言之,我从getWareHouse()
的{{1}}致电OnClickListner
:
FloatingActionButton
点击protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
wareHousAPIWrapper = new WareHousAPIWrapper(getApplicationContext(), MainActivity.this);
mToolbar = (Toolbar) findViewById(R.id.tool_bar);
FloatingActionButton mFab = (FloatingActionButton) findViewById(R.id.fab);
mFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wareHousAPIWrapper.getWarehouse();
}
});
if (mToolbar != null) {
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("sample");
}
wareHousAPIWrapper.getWarehouse();
}
时,我可以在RecyclerView
中看到加载的数据,但每次点击FloatingButton
的刷新长度都会增加。
为什么会这样?
见截图:
Staggered
方法定义getWarehouse
答案 0 :(得分:1)
每次调用wareHouseAPIWrapper.getWarehouse()
,您都会向mRecyclerView
添加另一个项目装饰器。这会导致每次单击刷新按钮时RecyclerView
中的项目变小。您应该只在初始化活动时添加项目装饰器,如下所示:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
wareHousAPIWrapper = new WareHousAPIWrapper(getApplicationContext(), MainActivity.this);
mToolbar = (Toolbar) findViewById(R.id.tool_bar);
FloatingActionButton mFab = (FloatingActionButton) findViewById(R.id.fab);
mFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wareHousAPIWrapper.getWarehouse();
}
});
if (mToolbar != null) {
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("sample");
}
wareHousAPIWrapper.getWarehouse();
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.staggering_grid);
GridItemSpaces decoration = new GridItemSpaces(10);
mRecyclerView.addItemDecoration(decoration);
}
不再需要在getWarehouse()
中添加项装饰器:
public void getWarehouse(){
.....
//Some retrofit calls
.....
mRecyclerView = (RecyclerView) mActivity.findViewById(R.id.staggering_grid);
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
mAdapter = new StaggeredGridAdapter(mContext);
mAdapter.addItems(response);
mRecyclerView.setAdapter(mAdapter);
}