Android RecyclerView : Change layout file LIST to GRID onOptionItemSelected

标签 android android-edittext android-recyclerview

我正在开发一个用于在线购物的 Android 应用程序。我使用 RecyclerView 为产品列表创建了以下 View ,因为我想在选择选项菜单项时更改 View :

我创建了以下名为 ProductAdapteradapter,因为我已经在 onCreateViewHolder 中实现了更改布局的代码,用于根据 bool 值选择布局文件值(value)。

适配器代码ProductAdapter:

    /***
     * ADAPTER for Product to binding rows in List
     */
    private class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductRowHolder> {

        private List<Product> productList;

        private Context mContext;

        public ProductAdapter(Context context, List<Product> feedItemList) {
            this.productList = feedItemList;
            this.mContext = context;
        }

        @Override
        public ProductRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View v = LayoutInflater.from(viewGroup.getContext()).inflate(isProductViewAsList ? R.layout.product_row_layout_list : R.layout.product_row_layout_grid, null);
            ProductRowHolder mh = new ProductRowHolder(v);
            return mh;
        }

        @Override
        public void onBindViewHolder(ProductRowHolder  productRowHolder, int i) {
            Product prodItem = productList.get(i);

//            Picasso.with(mContext).load(feedItem.getName())
//                    .error(R.drawable.ic_launcher)
//                    .placeholder(R.drawable.ic_launcher)
//                    .into(productRowHolder.thumbnail);


            double price = prodItem.getPrice();
            double discount = prodItem.getDiscount();
            double discountedPrice = price - (price * discount / 100);

            String code = "";
            if(prodItem.getCode() != null)
                code = "[" + prodItem.getCode() + "] ";

            productRowHolder.prodIsNewView.setVisibility(prodItem.getIsNew() == 1 ? View.VISIBLE : View.INVISIBLE);
            productRowHolder.prodNameView.setText(code + prodItem.getName());
            productRowHolder.prodOriginalRateView.setText("Rs." + new BigDecimal(price).setScale(2,RoundingMode.DOWN));
            productRowHolder.prodDiscView.setText("" + new BigDecimal(discount).setScale(2,RoundingMode.DOWN) + "% off");
            productRowHolder.prodDiscRateView.setText("Rs." + new BigDecimal(discountedPrice).setScale(2,RoundingMode.DOWN));

            productRowHolder.prodOriginalRateView.setPaintFlags(productRowHolder.prodOriginalRateView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
        }

        @Override
        public int getItemCount() {
            return (null != productList ? productList.size() : 0);
        }

        public class ProductRowHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            //Declaration of Views

            public ProductRowHolder(View view) {
                super(view);

                view.setOnClickListener(this);

                //Find Views
            }

            @Override
            public void onClick(View view) {
               //Onclick of row
            }
        }
    }

之后,我完成了将 RecyclerView 布局从 List 更改为 GridonOptionsItemSelected 中的反之亦然的代码,这里我正在调用 mAdapter.notifyDataSetChanged(); 所以它会再次调用适配器并更改值。

onOptionsItemSelected:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        switch (id) {
            case R.id.action_settings:
                return true;
            case android.R.id.home:
                finish();
                break;
            case R.id.product_show_as_view:
                isProductViewAsList = !isProductViewAsList;
                supportInvalidateOptionsMenu();
                mRecyclerView.setLayoutManager(isProductViewAsList ? new LinearLayoutManager(this) : new GridLayoutManager(this, 2));
                mAdapter.notifyDataSetChanged();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

我有点成功,比如:

Grid 布局的图片:

enter image description here

List 布局的图片:

enter image description here

但是现在当我滚动并显示 CHANGING VIEW 时像:

网格布局:

enter image description here

列表布局:

enter image description here

我不知道为什么滚动后会发生这种情况。有没有其他方法可以像这样改变 View 。

Today i just saw that this problem is because of ImageView, without it working perfectly.

请帮助,您的帮助将不胜感激。

最佳答案

我在我设置 LinearLayoutManager 的 Activity 开始时找到了解决方案,例如:

mLayoutManager = new LinearLayoutManager(this);
mProductListRecyclerView.setLayoutManager(mLayoutManager);

之后onOptionsItemSelected写成这样:

case R.id.menu_product_change_view:
     isViewWithCatalog = !isViewWithCatalog;
     supportInvalidateOptionsMenu();
     //loading = false;
     mProductListRecyclerView.setLayoutManager(isViewWithCatalog ? new LinearLayoutManager(this) : new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
     mProductListRecyclerView.setAdapter(mAdapter);
     break;

并在 onCreateViewHolder 中更改 View ,例如:

@Override
public ProductRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(isViewWithCatalog ? R.layout.product_row_layout_list : R.layout.product_row_layout_grid, null);
    ProductRowHolder mh = new ProductRowHolder(v);
    return mh;
}

从开始到结束,您必须管理 isViewWithCatalog 变量以首先显示哪个布局。

关于Android RecyclerView : Change layout file LIST to GRID onOptionItemSelected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28581712/

相关文章:

java - 从 recyclview 添加 jsonarray

android - 如何在滑动刷新android中刷新布局

android - 按项目位置在 GridView 中设置背景

android - SurfaceTexture.OnFrameAvailableListener 停止被调用

android - 如何获取 gradle sync 的详细消息?

android - 如何创建仅接受整数值的首选项

android - EditText 和 DialogFragment 导致 OutOfMemory 崩溃

android - 如何使用注册设备的电子邮件 ID 自动填充电子邮件编辑文本

android - 无法在标题 TextView 上设置辅助功能焦点

android - com.google.firebase.database.DatabaseException : Can't convert object of type java. lang.Long 键入 com.realty.drake.kunuk.Property