更改recyclerview的布局

时间:2016-12-08 02:41:00

标签: android android-recyclerview

我正在制作目录产品,它显示产品列表,用户可以将视图模式从列表更改为网格,反之亦然。我使用RecyclerView来显示列表。我能够更改recyclerview的布局管理器,但无法更改item(xml)的布局。这是我的代码:

FragmentListProduct.java

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    productData = new JSONArray();
    linearLayoutManager = new LinearLayoutManager(fragmentActivity);
    gridLayoutManager = new GridLayoutManager(fragmentActivity, 2);

    lpAdapter = new ListProductAdapter(fragmentActivity, true, productData, null,
            new ListProductAdapter.OnProductClickListener() {
                @Override
                public void onProductClick(View v, long id) {
                    Toast.makeText(fragmentActivity, "id:"+id, Toast.LENGTH_SHORT).show();
                    MainActivity.setIdToRead((int) id);
                    MainActivity.replaceFragment(FragmentListProduct.this, new FragmentViewProduct());
                }
            });
    rvProductList = (RecyclerView) mainView.findViewById(R.id.rvProductList);
    rvProductList.setLayoutManager(gridLayoutManager);
    rvProductList.setAdapter(lpAdapter);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.list_mode) {
        if (!listMode) {
            listMode = true;
            item.setIcon(R.drawable.grid);
            rvProductList.setLayoutManager(linearLayoutManager);
            lpAdapter.setListMode(false);
            lpAdapter.notifyDataSetChanged();
        } else {
            listMode = false;
            item.setIcon(R.drawable.list);
            rvProductList.setLayoutManager(gridLayoutManager);
            lpAdapter.setListMode(true);
            lpAdapter.notifyDataSetChanged();
        }
        return true;
    }
    return super.onOptionsItemSelected(item);
}

ListProductAdapter.java

@Override
public ListProductHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Log.i("zihad", "onCreateViewHolder()");
    View v = LayoutInflater.from(activity).inflate(listMode ? R.layout.list_product_list : R.layout.list_product_grid, parent, false);
    return new ListProductHolder(v);
}

public void setListMode(boolean listMode) {
    this.listMode = listMode;
}

当我滚动Recyclerview时,Logcat说" 12-08 09:38:09.795 8067-8067 /? I / zihad:onCreateViewHolder()"但不能改变项目的布局。我的代码有什么问题,如何解决?

1 个答案:

答案 0 :(得分:0)

rvProductList.setAdapter(lpAdapter);放入onOptionItemSelected:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.list_mode) {
        if (!listMode) {
            listMode = true;
            item.setIcon(R.drawable.grid);
            rvProductList.setLayoutManager(linearLayoutManager);
            lpAdapter.setListMode(false);
            lpAdapter.notifyDataSetChanged();
        } else {
            listMode = false;
            item.setIcon(R.drawable.list);
            rvProductList.setLayoutManager(gridLayoutManager);
            lpAdapter.setListMode(true);
            lpAdapter.notifyDataSetChanged();
        }
        rvProductList.setAdapter(lpAdapter);
        return true;
    }
    return super.onOptionsItemSelected(item);
}