[英]How to work with RecyclerView in Android?
I have used following code in main_activity.xml
to add Floating Button
: 我已经在
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" />
I am using following method to load data in RecyclerView
: 我正在使用以下方法在
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);
}
Purpose of getWareHouse()
method: Loading data in RecyclerView Staggered Grid View
. getWareHouse()
方法的目的:在RecyclerView Staggered Grid View
加载数据。
Purpose of FloatingButton
: To refresh the data from API calls
. FloatingButton
目的:刷新API calls
的数据。 In short I am calling getWareHouse()
from OnClickListner
of FloatingActionButton
: 简而言之,我从
FloatingActionButton
OnClickListner
调用getWareHouse()
:
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();
}
I can see the loaded data in RecyclerView
when clicking on FloatingButton
but Everytime I am clicking the refresh length of Staggered
is getting increases. 单击
FloatingButton
时,我可以在RecyclerView
看到加载的数据,但是每次单击Staggered
的刷新长度都会增加。
Why is it happening? 为什么会这样呢?
See the screenshot: 看截图:
First click of refresh button: 第一次单击刷新按钮:
getWarehouse
method definition getWarehouse
方法定义 //Making request to API
adapter.create(WarehouseAPI.class).getWarehouse()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<Warehouse>>() {
@Override
public void onCompleted() {
Log.d(this.getClass().getName(), "OnCompleted ()");
}
@Override
public void onError(Throwable e) {
Log.d(this.getClass().getName(), "Error:" + e.toString());
}
@Override
public void onNext(List<Warehouse> response) {
// code for `RecyclerView` I have posted above
}
});
With every call to wareHouseAPIWrapper.getWarehouse()
, you are adding another item decorator to mRecyclerView
. 随着每次调用
wareHouseAPIWrapper.getWarehouse()
您要添加另一个项目的装饰器mRecyclerView
。 This is causing the items in the RecyclerView
to become smaller every time you click the refresh button. 这导致每次单击刷新按钮时,
RecyclerView
的项目变小。 You should only add an item decorator when initializing the activity, like so: 您仅应在初始化活动时添加项目装饰器,如下所示:
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);
}
Item decorators no longer need to be added in getWarehouse()
: 不再需要在
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.