Android RecyclerView数组列表动态更改

时间:2016-03-09 21:03:40

标签: android arraylist android-recyclerview

我在我的项目中使用RecyclerView,我在应用程序运行时自动更改RecyclerView项目时遇到问题。

以下是我的ArrayList,当应用程序启动时,RecylcerView显示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}

但是当我动态推送mLocIds图片按钮时,我想更改这些项目。

这是我的其他RecyclerView代码:

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

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

}

重新设定焦点项目:

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

其他人使用:

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

这是我的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);
}

所以基本上当我点击btnFab按钮并调用onClick时,我想将RecylerViewmResIds更改为mLocIds

1 个答案:

答案 0 :(得分:2)

这样做很容易。

GalleryAdapter类添加一个setter方法,覆盖Adapter中的resIds数组,然后在此方法中调用notifyDataSetChanged()

这看起来像这样:

public class GalleryAdapter extends RecyclerView.Adapter {

    // Other methods left out for brevity.

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

现在需要从您的FAB GalleryAdapter#setData内调用onClickListener