如何在Android中使用RecyclerView?

时间:2015-10-25 07:36:08

标签: java android android-layout android-button android-recyclerview

我在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的刷新长度都会增加。

为什么会这样?

见截图:

首先点击刷新按钮: enter image description here

10-20次后: enter image description here

Edit-1 Staggered方法定义

getWarehouse

1 个答案:

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