Ich entwickle eine Android-Anwendung für Online-Shopping. Ich habe folgende Ansicht zur Liste der Produkte erstellt RecyclerView
mit, dass ich das Bild ändern zur Auswahl Option Menüpunkt will: zum Ändern LayoutAndroid RecyclerView: Layout-Datei LIST zu GRID onOptionItemSelected
Ich habe erstellt folgenden AdapterProductAdapter
genannt, daß ich habe implementiert Code in onCreateViewHolder
für die Auswahl der Layout-Datei basierend auf booleschen Wert.
Code of Adapter 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
}
}
}
Danach habe ich Code getan für das Ändern RecyclerView
Layout von List
zu Grid
und Vice Versa in onOptionsItemSelected
, hier rufe ich mAdapter.notifyDataSetChanged();
so wird es Adapter erneut aufrufen und Wert ändern.
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);
}
Ich habe wenig Erfolg wie:
Bild von Grid
Layout:
Bild von List
Layout:
aber jetzt, wenn ich scrollen und dann ändert sich VIEW wie angezeigt:
Grid
Layout:
List
Layout:
Ich weiß nicht, warum es nach dem Scrollen passiert. Gibt es eine andere Möglichkeit, die Ansicht so zu ändern?
Heute habe ich gerade gesehen, dass dieses Problem wegen
ImageView
ist, ohne dass es perfekt funktioniert.
Hilfe bitte, Sie helfen wird geschätzt.
Diese Antwort helfen könnte tauschen: http://stackoverflow.com/a/24933117/2649012 –
Nutzung diese RecyclerView.setHasFixedSize (true); –
seine 'mRecyclerView.setHasFixedSize (true);' aber nicht für mich arbeiten. irgendeine andere Lösung? –