gpt4 book ai didi

java - 如何在 Android 中使用 RecyclerView?

转载 作者:行者123 更新时间:2023-11-30 01:48:56 26 4
gpt4 key购买 nike

我在 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 调用 的数据。简而言之,我从 FloatingActionButtonOnClickListner 调用 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();

}

单击 FloatingButton 时,我可以在 RecyclerView 中看到加载的数据,但每次单击 Staggered 的刷新长度都在增加。

为什么会这样?

看截图:

第一次点击刷新按钮: enter image description here

10-20次后: enter image description here

Edit-1 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
}

});

最佳答案

每次调用 wareHouseAPIWrapper.getWarehouse() 时,您都会向 mRecyclerView 添加另一个项目装饰器。这会导致每次单击刷新按钮时 RecyclerView 中的项目变小。您应该只在初始化 Activity 时添加项目装饰器,如下所示:

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);
}

关于java - 如何在 Android 中使用 RecyclerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33327327/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com