我正在开发一个在线购物的Android应用程序。我使用RecyclerView
为产品列表创建了以下视图,因为我想在选择选项菜单项时更改视图:
我创建了以下名为ProductAdapter
的 adapter ,因为我已经实现了在onCreateViewHolder
中更改布局的代码,用于根据布尔值选择布局文件。
适配器代码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
更改为Grid
以及onOptionsItemSelected
中的反Versa的代码,在这里我打电话给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
布局的图片:
List
布局的图片:
现在,当我滚动然后更改视图正在显示时:
Grid
布局:
List
布局:
我不知道滚动后为什么会这样。有没有其他方法来改变这样的观点。
今天我刚看到这个问题是因为
ImageView
而没有它完美运作。
请帮助,我们将不胜感激。
答案 0 :(得分:33)
我找到了解决方案,其中包含我设置的活动 LinearLayoutManager
,如:
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
中更改视图,例如:
@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 变量,以便首先显示哪个布局。
答案 1 :(得分:7)
我通过在更改布局管理器后再次将适配器设置为RecyclerView
来解决了这个问题。
listView.setLayoutManager(new LinearLayoutManager(this));
listView.setAdapter(adapter);
答案 2 :(得分:5)
在这个SDK samples中有一个完整的例子,但它为LayoutManagers使用了一个布局文件
为了在切换布局时保持滚动位置,他们使用了这个功能
public void setRecyclerViewLayoutManager(LayoutManagerType layoutManagerType) {
int scrollPosition = 0;
// If a layout manager has already been set, get current scroll position.
if (mRecyclerView.getLayoutManager() != null) {
scrollPosition = ((LinearLayoutManager) mRecyclerView.getLayoutManager())
.findFirstCompletelyVisibleItemPosition();
}
switch (layoutManagerType) {
case GRID_LAYOUT_MANAGER:
mLayoutManager = new GridLayoutManager(getActivity(), SPAN_COUNT);
mCurrentLayoutManagerType = LayoutManagerType.GRID_LAYOUT_MANAGER;
break;
case LINEAR_LAYOUT_MANAGER:
mLayoutManager = new LinearLayoutManager(getActivity());
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
break;
default:
mLayoutManager = new LinearLayoutManager(getActivity());
mCurrentLayoutManagerType = LayoutManagerType.LINEAR_LAYOUT_MANAGER;
}
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.scrollToPosition(scrollPosition);
}
答案 3 :(得分:2)
我认为在滚动并更改回网格视图后,不会重新创建第一个项目。我通过覆盖getItemViewType()
解决了这个问题,并相应地对onCreateViewHolder
中的布局文件进行了充气。
答案 4 :(得分:-2)
我使用viewflipper并使用动画更改类型显示列表。和美丽。
主xml中的
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ViewFlipper android:id="@+id/view_flipper"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.RecyclerView
android:id="@+id/small_list" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" />
<android.support.v7.widget.RecyclerView android:scrollbarStyle="insideInset"
android:id="@+id/big_list" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" />
<android.support.v7.widget.RecyclerView android:scrollbarStyle="insideInset"
android:id="@+id/grid_list" android:paddingLeft="2.0dip" android:paddingRight="2.0dip" android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" />
</ViewFlipper>
活动中的所有代码
private RecyclerView smallRecycleView;
private RecyclerView gridRecycleView;
private RecyclerView bigRecycleView;
private StaggeredGridLayoutManager gridLayoutManager;
private LinearLayoutManager bigLayoutManager;
private LinearLayoutManager smallLayoutManager;
private AAdapter adapterGrid;
private AAdapter adapterSmall;
private AAdapter adapterBig;
private ViewFlipper viewFlipper;
private TypeShow typeShowList = TypeShow.Small;
private AList list = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
setLayout( R.layout.main_layout);
super.onCreate(savedInstanceState);
try {
list = getItems(10);
viewFlipper = ((ViewFlipper)findViewById(R.id.view_flipper));
viewFlipper.setOutAnimation(MainActivity.this, R.anim.anim_fade_out_flip);
viewFlipper.setInAnimation(MainActivity.this, R.anim.anim_fade_in_flip);
smallRecycleView = ((RecyclerView)findViewById(R.id.small_list));
gridRecycleView = ((RecyclerView)findViewById(R.id.grid_list));
bigRecycleView = ((RecyclerView)findViewById(R.id.big_list));
smallRecycleView.setHasFixedSize(false);
gridRecycleView.setHasFixedSize(false);
bigRecycleView.setHasFixedSize(false);
smallLayoutManager = new LinearLayoutManager(MainActivity.this);
smallRecycleView.setLayoutManager(smallLayoutManager);
gridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
gridRecycleView.setLayoutManager(gridLayoutManager);
gridRecycleView.setItemAnimator(null);
bigLayoutManager = new LinearLayoutManager(MainActivity.this);
bigRecycleView.setLayoutManager(bigLayoutManager);
adapterSmall = new AAdapter(MainActivity.this,list,MainActivity.this,TypeShow.Small);
smallRecycleView.setAdapter(adapterSmall);
adapterGrid = new AAdapter(MainActivity.this,list,MainActivity.this,TypeShow.Grid);
gridRecycleView.setAdapter(adapterGrid);
adapterBig = new AAdapter(MainActivity.this,list,MainActivity.this,TypeShow.Big);
bigRecycleView.setAdapter(adapterBig);
if (Build.VERSION.SDK_INT >= 11)
{
smallRecycleView.setVerticalScrollbarPosition(1);
gridRecycleView.setVerticalScrollbarPosition(1);
bigRecycleView.setVerticalScrollbarPosition(1);
}
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.floating_action_button);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
smallRecycleView.setSelected(false);
bigRecycleView.setSelected(false);
gridRecycleView.setSelected(false);
viewFlipper.showNext();
if(typeShowList == TypeShow.Small){
typeShowList = TypeShow.Big;
fab.setImageResource(R.drawable.ic_type_grid);
}
else if(typeShowList == TypeShow.Big){
typeShowList = TypeShow.Grid;
fab.setImageResource(R.drawable.ic_atype_small);
}
else {
typeShowList = TypeShow.Small;
fab.setImageResource(R.drawable.ic_type_big);
}
}
});
fab.setImageResource(R.drawable.ic_type_big);
}
catch (Exception e){
}
}
输入
public enum TypeShow {
Grid,
Small,
Big }
适配器
public class AAdapter extends RecyclerView.Adapter<AAdapter.PersonViewHolder> {
private AList noteList = null;
private MainActivity mContext;
private TypeShow typeShow;
public AAdapter(MainActivity mContext, AList noteList, OnItemClickListener mListener, TypeShow typeShow){
this.noteList = noteList;
this.mContext= mContext;
this.mListener= mListener;
this.typeShow= typeShow;
}
@Override
public int getItemCount() {
return noteList.size();
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
if(typeShow == TypeShow.Grid) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_row_grid, viewGroup, false);
return new PersonViewHolder(v);
}
else if(typeShow == TypeShow.Small) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_row_small, viewGroup, false);
return new PersonViewHolder(v);
}
else {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_row_big, viewGroup, false);
return new PersonViewHolder(v);
}
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
}
public static class PersonViewHolder extends RecyclerView.ViewHolder {
PersonViewHolder(View itemView) {
super(itemView);
}
}
}