canberk cakmak
canberk cakmak

Reputation: 145

Android RecyclerView array list change dynamically

I'm using RecyclerView in my project and I have a problem with changing RecyclerView items automatically as the application runs.

Here is my ArrayList, and when the application starts the RecylcerView shows mResIds:

List<StickerView> mStickers = new ArrayList<>();
int[] mResIds = new int[]{R.mipmap.ic_sticker_01, R.mipmap.ic_sticker_02} 
int[] mLocIds = new int[]{R.mipmap.ic_sticker_03, R.mipmap.ic_sticker_04}

But I want to change these items when I push to image button with mLocIds dynamically.

Here is my other RecyclerView code:

private void initEvent() {
    mGalleryAdapter.setOnItemClickListener(new GalleryAdapter.OnRecyclerViewItemClickListener() {
        @Override
        public void onItemClick(View view, int resId) {
            addStickerItem(resId);
        }
    });
 }

Adding Sticker:

 private void addStickerItem(int resId) {
    resetStickersFocus();
    StickerView stickerView = new StickerView(this);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(

            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);
    params.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.image);
    params.addRule(RelativeLayout.ALIGN_TOP, R.id.image);
    ((ViewGroup) mImageView.getParent()).addView(stickerView, params);

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId);
    stickerView.setWaterMark(bitmap);
    mStickers.add(stickerView);

    stickerView.setOnStickerDeleteListener(new StickerView.OnStickerDeleteListener() {
        @Override
        public void onDelete(StickerView stickerView) {
            if (mStickers.contains(stickerView))
                mStickers.remove(stickerView);
        }
    });

}

Reseting focus items:

private void resetStickersFocus() {
    for (StickerView stickerView : mStickers) {
        stickerView.setFocusable(false);
    }
}

Others uses:

private void initView() {
    mStatusBarHeight = getStatusBarHeight();
    mToolBarHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setUpToolbar();
    mImageView = (ImageView) findViewById(R.id.image);
    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    mEffectImg = (ImageView) findViewById(R.id.effect_image);

    mGalleryAdapter = new GalleryAdapter(mResIds);


    LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);


    mRecyclerView.setLayoutManager(layoutManager);
    mRecyclerView.setAdapter(mGalleryAdapter);
}

And here is my onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pht);
    initView();
    initEvent();
    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    mImageView = (ImageView) findViewById(R.id.effect_image);
    btnFab = (FloatingActionButton) findViewById(R.id.btnFloatingAction);
}

So basically when I click the btnFab button, and onClick is called, I want to change my RecylerView items mResIds to mLocIds.

Upvotes: 4

Views: 1376

Answers (1)

Darwind
Darwind

Reputation: 7371

It's fairly easy to do that.

Add a setter method to your GalleryAdapter class that overwrites your resIds array inside the Adapter and then call notifyDataSetChanged() inside this method.

This could look something like this:

public class GalleryAdapter extends RecyclerView.Adapter {

    // Other methods left out for brevity.

    public void setData(int[] mLocIds) {
        this.mResIds = mLocIds;
        notifyDataSetChanged();
    }
}

Now GalleryAdapter#setData needs to be called from within your FAB's onClickListener.

Upvotes: 2

Related Questions