简体   繁体   中英

How to work with RecyclerView in Android?

I have used following code in main_activity.xml to add 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 :

 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 .

Purpose of FloatingButton : To refresh the data from API calls . In short I am calling getWareHouse() from OnClickListner of 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();

}

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.

Why is it happening?

See the screenshot:

First click of refresh button: 在此处输入图片说明

After 10-20 times: 在此处输入图片说明

Edit-1 getWarehouse method definition

 //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 . This is causing the items in the RecyclerView to become smaller every time you click the refresh button. 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() :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM