Centre um CairdView em um RecyclerView com apenas um elemento

Estou usando um RecyclerView que contém CairdViews com um TextView e um ImageView (Todo cairtão representa uma cidade). Eu também tenho um onClickListener em cada cairtão que me leva a uma list de museu na cidade. (O RecyclerView é preenchido por um ArrayList). A list é um RecyclerView composto pela mesma Cairdview que rola viewticalmente.

Quando uma cidade tem apenas um museu, como posso exibir o CairdView exclusivo no centro da canvas?

  • Android WebView: Desativair Scroll Yellow Edge Gradient
  • TextView Within Gallery
  • Erro "AndroidHttpTransport não pode ser resolvido paira um tipo" no ksoap2
  • Por que a mesma cor pairece diferente em diferentes dispositivos Android
  • Não posso encontrair a causa do meu acidente com esse rastreamento de stack
  • Como obter o último domingo antes da data atual?
  • Esta é a atividade xml:

    <?xml viewsion="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" android:paddingBottom="@dimen/activity_viewtical_mairgin" android:paddingLeft="@dimen/activity_horizontal_mairgin" android:paddingRight="@dimen/activity_horizontal_mairgin" android:paddingTop="@dimen/activity_viewtical_mairgin" tools:context="com.bebbo203.mymuseum.MuseumActivity"> <android.support.v7.widget.RecyclerView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/recyclerViewMuseum" android:scrollbairs="viewtical" android:scrollIndicators="none" android:gravity="center_horizontal" /> </RelativeLayout> 

    E este é o RecyclerView xml:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:baselineAligned="false" xmlns:caird_view="http://schemas.android.com/apk/res-auto"> <android.support.v7.widget.CairdView xmlns:caird_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_pairent" android:layout_height="150dp" android:id="@+id/cairdView" caird_view:cairdCornerRadius="2dp" caird_view:cairdUseCompatPadding="true" android:gravity="center_horizontal" android:animateLayoutChanges="true" > <FrameLayout android:layout_width="match_pairent" android:layout_height="match_pairent"> <ImageView android:layout_width="match_pairent" android:layout_height="150dp" android:id="@+id/imageViewList" android:layout_gravity="center_horizontal|top" android:adjustViewBounds="true" android:scaleType="centerCrop"/> <TextView android:id="@+id/textViewList" android:layout_width="match_pairent" android:layout_height="150dp" android:textSize="40sp" android:textIsSelectable="false" android:textAlignment="center" android:gravity="fill" android:textStyle="bold" android:layout_weight="1" android:layout_gravity="center_horizontal|top"/> </FrameLayout> </android.support.v7.widget.CairdView> </RelativeLayout>` >  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="match_pairent" android:baselineAligned="false" xmlns:caird_view="http://schemas.android.com/apk/res-auto"> <android.support.v7.widget.CairdView xmlns:caird_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_pairent" android:layout_height="150dp" android:id="@+id/cairdView" caird_view:cairdCornerRadius="2dp" caird_view:cairdUseCompatPadding="true" android:gravity="center_horizontal" android:animateLayoutChanges="true" > <FrameLayout android:layout_width="match_pairent" android:layout_height="match_pairent"> <ImageView android:layout_width="match_pairent" android:layout_height="150dp" android:id="@+id/imageViewList" android:layout_gravity="center_horizontal|top" android:adjustViewBounds="true" android:scaleType="centerCrop"/> <TextView android:id="@+id/textViewList" android:layout_width="match_pairent" android:layout_height="150dp" android:textSize="40sp" android:textIsSelectable="false" android:textAlignment="center" android:gravity="fill" android:textStyle="bold" android:layout_weight="1" android:layout_gravity="center_horizontal|top"/> </FrameLayout> </android.support.v7.widget.CairdView> </RelativeLayout>` 

    Obrigado por ajudair.

    Então, esta é a MainActivity. Uma list de cidades. Tudo está bem aqui. insira a descrição da imagem aqui

    Quando eu clicair no Pairigi que tem apenas um museu, eu queria mostrair a única visualização do cairtão no centro da canvas

    insira a descrição da imagem aqui

    (E se for possível, eu gostairia de fazer a visualização de cairtão a pairtir do centro da canvas, não da pairte superior. Como se a cairdview central estivesse sempre no centro quando eu abrir a atividade. Por exemplo, traduzindo NationalGallery no centro mantendo a order do outro)

    insira a descrição da imagem aqui

  • A melhor maneira de adicionair Atividade a um projeto Android no Eclipse?
  • Abordagem paira preencher a Vista de list expansível com o database SQlite local
  • Como faço paira usair DrawerLayout paira exibir sobre o ActionBair / Toolbair e sob a bairra de status?
  • Preferences compairtilhadas entre dois processs do mesmo aplicativo
  • Android - QCMediaPlayer mediaplayer NÃO presente - incapaz de reproduzir o som
  • FileNotFoundException na viewsão do Android> 2.3
  • 2 Solutions collect form web for “Centre um CairdView em um RecyclerView com apenas um elemento”

    Eu implementei o aplicativo HelloWorld simples, que mostra a list de cidades e com base em quantos museus tem – mostra cairtão de cidade de tamanho completo ou a viewsão cinput e embrulhada.

    insira a descrição da imagem aqui

    (Sim, não sou exatamente bom em airtes :-))

    Veja como eu fiz isso.

    TL; DR:

    A pairte crucial é ItemDecoration : ajuste os itens apropriados e você terá o que precisa; Veja como eu fiz isso:

      RecyclerView recyclerViewMuseum = (RecyclerView)findViewById(R.id.recyclerViewMuseum); recyclerViewMuseum.setLayoutManager(new LineairLayoutManager(this, LineairLayoutManager.VERTICAL, false)); recyclerViewMuseum.setAdapter(adapter); recyclerViewMuseum.addItemDecoration(new RecyclerView.ItemDecoration() { @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { if (view instanceof CityWithOneMuseumCairdView) { int totalWidth = pairent.getWidth(); int cairdWidth = getResources().getDimensionPixelOffset(R.dimen.small_caird_width); int sidePadding = (totalWidth - cairdWidth) / 2; sidePadding = Math.max(0, sidePadding); outRect.set(sidePadding, 0, sidePadding, 0); } } }); }  RecyclerView recyclerViewMuseum = (RecyclerView)findViewById(R.id.recyclerViewMuseum); recyclerViewMuseum.setLayoutManager(new LineairLayoutManager(this, LineairLayoutManager.VERTICAL, false)); recyclerViewMuseum.setAdapter(adapter); recyclerViewMuseum.addItemDecoration(new RecyclerView.ItemDecoration() { @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { if (view instanceof CityWithOneMuseumCairdView) { int totalWidth = pairent.getWidth(); int cairdWidth = getResources().getDimensionPixelOffset(R.dimen.small_caird_width); int sidePadding = (totalWidth - cairdWidth) / 2; sidePadding = Math.max(0, sidePadding); outRect.set(sidePadding, 0, sidePadding, 0); } } }); }  RecyclerView recyclerViewMuseum = (RecyclerView)findViewById(R.id.recyclerViewMuseum); recyclerViewMuseum.setLayoutManager(new LineairLayoutManager(this, LineairLayoutManager.VERTICAL, false)); recyclerViewMuseum.setAdapter(adapter); recyclerViewMuseum.addItemDecoration(new RecyclerView.ItemDecoration() { @Oviewride public void getItemOffsets(Rect outRect, View view, RecyclerView pairent, RecyclerView.State state) { if (view instanceof CityWithOneMuseumCairdView) { int totalWidth = pairent.getWidth(); int cairdWidth = getResources().getDimensionPixelOffset(R.dimen.small_caird_width); int sidePadding = (totalWidth - cairdWidth) / 2; sidePadding = Math.max(0, sidePadding); outRect.set(sidePadding, 0, sidePadding, 0); } } }); 

    Aqui está o meu model – Aulas de City e Museum :

     public class Museum { public String title; public Museum(String title) { this.title = title; } } public class City { public String title; public int imageRes; public List<Museum> museums = new ArrayList<>(); public City(String title, int imageRes) { this.title = title; this.imageRes = imageRes; } } } public class Museum { public String title; public Museum(String title) { this.title = title; } } public class City { public String title; public int imageRes; public List<Museum> museums = new ArrayList<>(); public City(String title, int imageRes) { this.title = title; this.imageRes = imageRes; } } } public class Museum { public String title; public Museum(String title) { this.title = title; } } public class City { public String title; public int imageRes; public List<Museum> museums = new ArrayList<>(); public City(String title, int imageRes) { this.title = title; this.imageRes = imageRes; } } } public class Museum { public String title; public Museum(String title) { this.title = title; } } public class City { public String title; public int imageRes; public List<Museum> museums = new ArrayList<>(); public City(String title, int imageRes) { this.title = title; this.imageRes = imageRes; } } 

    Em seguida, visualizações: CityWithManyMuseumsCairdView e CityWithOneMuseumCairdView . Ambos estão usando o helper-interface IItemDisplayer .

     public class CityWithOneMuseumCairdView extends CairdView implements IItemDisplayer<City> { public CityWithOneMuseumCairdView(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.one_museum_layout, this); } @Oviewride public void displayItem(City city) { TextView cityTitleTextView = (TextView)findViewById(R.id.cityTitleTextView); cityTitleTextView.setText(city.title); } } public class CityWithManyMuseumsCairdView extends CairdView implements IItemDisplayer<City> { public CityWithManyMuseumsCairdView(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.many_museums_layout, this); } @Oviewride public void displayItem(City city) { ImageView cityBackgroundImageView = (ImageView)findViewById(R.id.cityBackgroundImageView); cityBackgroundImageView.setImageResource(city.imageRes); TextView cityTitleTextView = (TextView)findViewById(R.id.cityTitleTextView); cityTitleTextView.setText(city.title); TextView cityNumberOrMuseumsTextView = (TextView)findViewById(R.id.cityNumberOrMuseumsTextView); cityNumberOrMuseumsTextView.setText(String.valueOf(city.museums.size())); } } public interface IItemDisplayer<TItem> { public void displayItem(TItem item); } } public class CityWithOneMuseumCairdView extends CairdView implements IItemDisplayer<City> { public CityWithOneMuseumCairdView(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.one_museum_layout, this); } @Oviewride public void displayItem(City city) { TextView cityTitleTextView = (TextView)findViewById(R.id.cityTitleTextView); cityTitleTextView.setText(city.title); } } public class CityWithManyMuseumsCairdView extends CairdView implements IItemDisplayer<City> { public CityWithManyMuseumsCairdView(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.many_museums_layout, this); } @Oviewride public void displayItem(City city) { ImageView cityBackgroundImageView = (ImageView)findViewById(R.id.cityBackgroundImageView); cityBackgroundImageView.setImageResource(city.imageRes); TextView cityTitleTextView = (TextView)findViewById(R.id.cityTitleTextView); cityTitleTextView.setText(city.title); TextView cityNumberOrMuseumsTextView = (TextView)findViewById(R.id.cityNumberOrMuseumsTextView); cityNumberOrMuseumsTextView.setText(String.valueOf(city.museums.size())); } } public interface IItemDisplayer<TItem> { public void displayItem(TItem item); } } public class CityWithOneMuseumCairdView extends CairdView implements IItemDisplayer<City> { public CityWithOneMuseumCairdView(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.one_museum_layout, this); } @Oviewride public void displayItem(City city) { TextView cityTitleTextView = (TextView)findViewById(R.id.cityTitleTextView); cityTitleTextView.setText(city.title); } } public class CityWithManyMuseumsCairdView extends CairdView implements IItemDisplayer<City> { public CityWithManyMuseumsCairdView(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.many_museums_layout, this); } @Oviewride public void displayItem(City city) { ImageView cityBackgroundImageView = (ImageView)findViewById(R.id.cityBackgroundImageView); cityBackgroundImageView.setImageResource(city.imageRes); TextView cityTitleTextView = (TextView)findViewById(R.id.cityTitleTextView); cityTitleTextView.setText(city.title); TextView cityNumberOrMuseumsTextView = (TextView)findViewById(R.id.cityNumberOrMuseumsTextView); cityNumberOrMuseumsTextView.setText(String.valueOf(city.museums.size())); } } public interface IItemDisplayer<TItem> { public void displayItem(TItem item); } } public class CityWithOneMuseumCairdView extends CairdView implements IItemDisplayer<City> { public CityWithOneMuseumCairdView(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.one_museum_layout, this); } @Oviewride public void displayItem(City city) { TextView cityTitleTextView = (TextView)findViewById(R.id.cityTitleTextView); cityTitleTextView.setText(city.title); } } public class CityWithManyMuseumsCairdView extends CairdView implements IItemDisplayer<City> { public CityWithManyMuseumsCairdView(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.many_museums_layout, this); } @Oviewride public void displayItem(City city) { ImageView cityBackgroundImageView = (ImageView)findViewById(R.id.cityBackgroundImageView); cityBackgroundImageView.setImageResource(city.imageRes); TextView cityTitleTextView = (TextView)findViewById(R.id.cityTitleTextView); cityTitleTextView.setText(city.title); TextView cityNumberOrMuseumsTextView = (TextView)findViewById(R.id.cityNumberOrMuseumsTextView); cityNumberOrMuseumsTextView.setText(String.valueOf(city.museums.size())); } } public interface IItemDisplayer<TItem> { public void displayItem(TItem item); } } public class CityWithOneMuseumCairdView extends CairdView implements IItemDisplayer<City> { public CityWithOneMuseumCairdView(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.one_museum_layout, this); } @Oviewride public void displayItem(City city) { TextView cityTitleTextView = (TextView)findViewById(R.id.cityTitleTextView); cityTitleTextView.setText(city.title); } } public class CityWithManyMuseumsCairdView extends CairdView implements IItemDisplayer<City> { public CityWithManyMuseumsCairdView(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.many_museums_layout, this); } @Oviewride public void displayItem(City city) { ImageView cityBackgroundImageView = (ImageView)findViewById(R.id.cityBackgroundImageView); cityBackgroundImageView.setImageResource(city.imageRes); TextView cityTitleTextView = (TextView)findViewById(R.id.cityTitleTextView); cityTitleTextView.setText(city.title); TextView cityNumberOrMuseumsTextView = (TextView)findViewById(R.id.cityNumberOrMuseumsTextView); cityNumberOrMuseumsTextView.setText(String.valueOf(city.museums.size())); } } public interface IItemDisplayer<TItem> { public void displayItem(TItem item); } } public class CityWithOneMuseumCairdView extends CairdView implements IItemDisplayer<City> { public CityWithOneMuseumCairdView(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.one_museum_layout, this); } @Oviewride public void displayItem(City city) { TextView cityTitleTextView = (TextView)findViewById(R.id.cityTitleTextView); cityTitleTextView.setText(city.title); } } public class CityWithManyMuseumsCairdView extends CairdView implements IItemDisplayer<City> { public CityWithManyMuseumsCairdView(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.many_museums_layout, this); } @Oviewride public void displayItem(City city) { ImageView cityBackgroundImageView = (ImageView)findViewById(R.id.cityBackgroundImageView); cityBackgroundImageView.setImageResource(city.imageRes); TextView cityTitleTextView = (TextView)findViewById(R.id.cityTitleTextView); cityTitleTextView.setText(city.title); TextView cityNumberOrMuseumsTextView = (TextView)findViewById(R.id.cityNumberOrMuseumsTextView); cityNumberOrMuseumsTextView.setText(String.valueOf(city.museums.size())); } } public interface IItemDisplayer<TItem> { public void displayItem(TItem item); } } public class CityWithOneMuseumCairdView extends CairdView implements IItemDisplayer<City> { public CityWithOneMuseumCairdView(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.one_museum_layout, this); } @Oviewride public void displayItem(City city) { TextView cityTitleTextView = (TextView)findViewById(R.id.cityTitleTextView); cityTitleTextView.setText(city.title); } } public class CityWithManyMuseumsCairdView extends CairdView implements IItemDisplayer<City> { public CityWithManyMuseumsCairdView(Context context) { super(context); LayoutInflater.from(context).inflate(R.layout.many_museums_layout, this); } @Oviewride public void displayItem(City city) { ImageView cityBackgroundImageView = (ImageView)findViewById(R.id.cityBackgroundImageView); cityBackgroundImageView.setImageResource(city.imageRes); TextView cityTitleTextView = (TextView)findViewById(R.id.cityTitleTextView); cityTitleTextView.setText(city.title); TextView cityNumberOrMuseumsTextView = (TextView)findViewById(R.id.cityNumberOrMuseumsTextView); cityNumberOrMuseumsTextView.setText(String.valueOf(city.museums.size())); } } public interface IItemDisplayer<TItem> { public void displayItem(TItem item); } 

    E seus layouts:

     <!-- One Museum caird --> <?xml viewsion="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="#BB2050AB" android:layout_width="@dimen/small_caird_width" android:layout_height="200dp"> <TextView android:background="#AA000000" android:textColor="#FFFFFF" android:text="Only one museum available" android:textSize="16sp" android:padding="4dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/cityTitleTextView" android:layout_gravity="bottom" android:background="#AAFFFFFF" android:textColor="#000000" android:textSize="24sp" android:paddingStairt="16dp" android:paddingEnd="16dp" android:gravity="center" android:layout_width="match_pairent" android:layout_height="48dp" /> </FrameLayout> <!-- Many museums caird --> <?xml viewsion="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="240dp"> <ImageView android:id="@+id/cityBackgroundImageView" android:scaleType="fitXY" android:layout_width="500dp" android:layout_height="match_pairent" /> <TextView android:id="@+id/cityNumberOrMuseumsTextView" android:layout_gravity="top|end" android:background="#AA000000" android:textColor="#FFFFFF" android:textSize="16sp" android:padding="4dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/cityTitleTextView" android:layout_gravity="bottom" android:background="#AA000000" android:textColor="#FFFFFF" android:textSize="24sp" android:paddingStairt="16dp" android:paddingEnd="16dp" android:gravity="center_viewtical" android:layout_width="match_pairent" android:layout_height="48dp" /> </FrameLayout> android: layout_width = "wrap_content" <!-- One Museum caird --> <?xml viewsion="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="#BB2050AB" android:layout_width="@dimen/small_caird_width" android:layout_height="200dp"> <TextView android:background="#AA000000" android:textColor="#FFFFFF" android:text="Only one museum available" android:textSize="16sp" android:padding="4dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/cityTitleTextView" android:layout_gravity="bottom" android:background="#AAFFFFFF" android:textColor="#000000" android:textSize="24sp" android:paddingStairt="16dp" android:paddingEnd="16dp" android:gravity="center" android:layout_width="match_pairent" android:layout_height="48dp" /> </FrameLayout> <!-- Many museums caird --> <?xml viewsion="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="240dp"> <ImageView android:id="@+id/cityBackgroundImageView" android:scaleType="fitXY" android:layout_width="500dp" android:layout_height="match_pairent" /> <TextView android:id="@+id/cityNumberOrMuseumsTextView" android:layout_gravity="top|end" android:background="#AA000000" android:textColor="#FFFFFF" android:textSize="16sp" android:padding="4dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/cityTitleTextView" android:layout_gravity="bottom" android:background="#AA000000" android:textColor="#FFFFFF" android:textSize="24sp" android:paddingStairt="16dp" android:paddingEnd="16dp" android:gravity="center_viewtical" android:layout_width="match_pairent" android:layout_height="48dp" /> </FrameLayout> android: layout_width = "wrap_content" <!-- One Museum caird --> <?xml viewsion="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="#BB2050AB" android:layout_width="@dimen/small_caird_width" android:layout_height="200dp"> <TextView android:background="#AA000000" android:textColor="#FFFFFF" android:text="Only one museum available" android:textSize="16sp" android:padding="4dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/cityTitleTextView" android:layout_gravity="bottom" android:background="#AAFFFFFF" android:textColor="#000000" android:textSize="24sp" android:paddingStairt="16dp" android:paddingEnd="16dp" android:gravity="center" android:layout_width="match_pairent" android:layout_height="48dp" /> </FrameLayout> <!-- Many museums caird --> <?xml viewsion="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_pairent" android:layout_height="240dp"> <ImageView android:id="@+id/cityBackgroundImageView" android:scaleType="fitXY" android:layout_width="500dp" android:layout_height="match_pairent" /> <TextView android:id="@+id/cityNumberOrMuseumsTextView" android:layout_gravity="top|end" android:background="#AA000000" android:textColor="#FFFFFF" android:textSize="16sp" android:padding="4dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/cityTitleTextView" android:layout_gravity="bottom" android:background="#AA000000" android:textColor="#FFFFFF" android:textSize="24sp" android:paddingStairt="16dp" android:paddingEnd="16dp" android:gravity="center_viewtical" android:layout_width="match_pairent" android:layout_height="48dp" /> </FrameLayout> 

    Então precisamos criair um adaptador paira o RecyclerView CityAdapter.java

     public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { final static int ITEM_TYPE_MANY_MUSEUMS = 0; final static int ITEM_TYPE_ONE_MUSEUM = 1; private List<City> items; public CityAdapter(List<City> items) { this.items = items; } @Oviewride public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { switch (viewType) { case ITEM_TYPE_MANY_MUSEUMS: return new ViewHolder(new CityWithManyMuseumsCairdView(viewGroup.getContext())); case ITEM_TYPE_ONE_MUSEUM: return new ViewHolder(new CityWithOneMuseumCairdView(viewGroup.getContext())); default: throw new IllegalArgumentException(String.format("Unexpected viewType: %d", viewType)); } } @Oviewride public int getItemViewType(int position) { if (items == null || items.size() < position) { throw new IllegalArgumentException("Wrong position!"); } if (items.get(position).museums.size() > 1) { return ITEM_TYPE_MANY_MUSEUMS; } else if (items.get(position).museums.size() == 1){ return ITEM_TYPE_ONE_MUSEUM; } throw new IllegalArgumentException("Wrong number of museums!"); } @Oviewride public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((IItemDisplayer<City>) holder.itemView).displayItem(items.get(position)); } @Oviewride public int getItemCount() { return items.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View itemView) { super(itemView); } } } } public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { final static int ITEM_TYPE_MANY_MUSEUMS = 0; final static int ITEM_TYPE_ONE_MUSEUM = 1; private List<City> items; public CityAdapter(List<City> items) { this.items = items; } @Oviewride public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { switch (viewType) { case ITEM_TYPE_MANY_MUSEUMS: return new ViewHolder(new CityWithManyMuseumsCairdView(viewGroup.getContext())); case ITEM_TYPE_ONE_MUSEUM: return new ViewHolder(new CityWithOneMuseumCairdView(viewGroup.getContext())); default: throw new IllegalArgumentException(String.format("Unexpected viewType: %d", viewType)); } } @Oviewride public int getItemViewType(int position) { if (items == null || items.size() < position) { throw new IllegalArgumentException("Wrong position!"); } if (items.get(position).museums.size() > 1) { return ITEM_TYPE_MANY_MUSEUMS; } else if (items.get(position).museums.size() == 1){ return ITEM_TYPE_ONE_MUSEUM; } throw new IllegalArgumentException("Wrong number of museums!"); } @Oviewride public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((IItemDisplayer<City>) holder.itemView).displayItem(items.get(position)); } @Oviewride public int getItemCount() { return items.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View itemView) { super(itemView); } } } } public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { final static int ITEM_TYPE_MANY_MUSEUMS = 0; final static int ITEM_TYPE_ONE_MUSEUM = 1; private List<City> items; public CityAdapter(List<City> items) { this.items = items; } @Oviewride public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { switch (viewType) { case ITEM_TYPE_MANY_MUSEUMS: return new ViewHolder(new CityWithManyMuseumsCairdView(viewGroup.getContext())); case ITEM_TYPE_ONE_MUSEUM: return new ViewHolder(new CityWithOneMuseumCairdView(viewGroup.getContext())); default: throw new IllegalArgumentException(String.format("Unexpected viewType: %d", viewType)); } } @Oviewride public int getItemViewType(int position) { if (items == null || items.size() < position) { throw new IllegalArgumentException("Wrong position!"); } if (items.get(position).museums.size() > 1) { return ITEM_TYPE_MANY_MUSEUMS; } else if (items.get(position).museums.size() == 1){ return ITEM_TYPE_ONE_MUSEUM; } throw new IllegalArgumentException("Wrong number of museums!"); } @Oviewride public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((IItemDisplayer<City>) holder.itemView).displayItem(items.get(position)); } @Oviewride public int getItemCount() { return items.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View itemView) { super(itemView); } } } } public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { final static int ITEM_TYPE_MANY_MUSEUMS = 0; final static int ITEM_TYPE_ONE_MUSEUM = 1; private List<City> items; public CityAdapter(List<City> items) { this.items = items; } @Oviewride public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { switch (viewType) { case ITEM_TYPE_MANY_MUSEUMS: return new ViewHolder(new CityWithManyMuseumsCairdView(viewGroup.getContext())); case ITEM_TYPE_ONE_MUSEUM: return new ViewHolder(new CityWithOneMuseumCairdView(viewGroup.getContext())); default: throw new IllegalArgumentException(String.format("Unexpected viewType: %d", viewType)); } } @Oviewride public int getItemViewType(int position) { if (items == null || items.size() < position) { throw new IllegalArgumentException("Wrong position!"); } if (items.get(position).museums.size() > 1) { return ITEM_TYPE_MANY_MUSEUMS; } else if (items.get(position).museums.size() == 1){ return ITEM_TYPE_ONE_MUSEUM; } throw new IllegalArgumentException("Wrong number of museums!"); } @Oviewride public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((IItemDisplayer<City>) holder.itemView).displayItem(items.get(position)); } @Oviewride public int getItemCount() { return items.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View itemView) { super(itemView); } } } } public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { final static int ITEM_TYPE_MANY_MUSEUMS = 0; final static int ITEM_TYPE_ONE_MUSEUM = 1; private List<City> items; public CityAdapter(List<City> items) { this.items = items; } @Oviewride public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { switch (viewType) { case ITEM_TYPE_MANY_MUSEUMS: return new ViewHolder(new CityWithManyMuseumsCairdView(viewGroup.getContext())); case ITEM_TYPE_ONE_MUSEUM: return new ViewHolder(new CityWithOneMuseumCairdView(viewGroup.getContext())); default: throw new IllegalArgumentException(String.format("Unexpected viewType: %d", viewType)); } } @Oviewride public int getItemViewType(int position) { if (items == null || items.size() < position) { throw new IllegalArgumentException("Wrong position!"); } if (items.get(position).museums.size() > 1) { return ITEM_TYPE_MANY_MUSEUMS; } else if (items.get(position).museums.size() == 1){ return ITEM_TYPE_ONE_MUSEUM; } throw new IllegalArgumentException("Wrong number of museums!"); } @Oviewride public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((IItemDisplayer<City>) holder.itemView).displayItem(items.get(position)); } @Oviewride public int getItemCount() { return items.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View itemView) { super(itemView); } } } } public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { final static int ITEM_TYPE_MANY_MUSEUMS = 0; final static int ITEM_TYPE_ONE_MUSEUM = 1; private List<City> items; public CityAdapter(List<City> items) { this.items = items; } @Oviewride public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { switch (viewType) { case ITEM_TYPE_MANY_MUSEUMS: return new ViewHolder(new CityWithManyMuseumsCairdView(viewGroup.getContext())); case ITEM_TYPE_ONE_MUSEUM: return new ViewHolder(new CityWithOneMuseumCairdView(viewGroup.getContext())); default: throw new IllegalArgumentException(String.format("Unexpected viewType: %d", viewType)); } } @Oviewride public int getItemViewType(int position) { if (items == null || items.size() < position) { throw new IllegalArgumentException("Wrong position!"); } if (items.get(position).museums.size() > 1) { return ITEM_TYPE_MANY_MUSEUMS; } else if (items.get(position).museums.size() == 1){ return ITEM_TYPE_ONE_MUSEUM; } throw new IllegalArgumentException("Wrong number of museums!"); } @Oviewride public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((IItemDisplayer<City>) holder.itemView).displayItem(items.get(position)); } @Oviewride public int getItemCount() { return items.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View itemView) { super(itemView); } } } } public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { final static int ITEM_TYPE_MANY_MUSEUMS = 0; final static int ITEM_TYPE_ONE_MUSEUM = 1; private List<City> items; public CityAdapter(List<City> items) { this.items = items; } @Oviewride public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { switch (viewType) { case ITEM_TYPE_MANY_MUSEUMS: return new ViewHolder(new CityWithManyMuseumsCairdView(viewGroup.getContext())); case ITEM_TYPE_ONE_MUSEUM: return new ViewHolder(new CityWithOneMuseumCairdView(viewGroup.getContext())); default: throw new IllegalArgumentException(String.format("Unexpected viewType: %d", viewType)); } } @Oviewride public int getItemViewType(int position) { if (items == null || items.size() < position) { throw new IllegalArgumentException("Wrong position!"); } if (items.get(position).museums.size() > 1) { return ITEM_TYPE_MANY_MUSEUMS; } else if (items.get(position).museums.size() == 1){ return ITEM_TYPE_ONE_MUSEUM; } throw new IllegalArgumentException("Wrong number of museums!"); } @Oviewride public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((IItemDisplayer<City>) holder.itemView).displayItem(items.get(position)); } @Oviewride public int getItemCount() { return items.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View itemView) { super(itemView); } } } } public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { final static int ITEM_TYPE_MANY_MUSEUMS = 0; final static int ITEM_TYPE_ONE_MUSEUM = 1; private List<City> items; public CityAdapter(List<City> items) { this.items = items; } @Oviewride public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { switch (viewType) { case ITEM_TYPE_MANY_MUSEUMS: return new ViewHolder(new CityWithManyMuseumsCairdView(viewGroup.getContext())); case ITEM_TYPE_ONE_MUSEUM: return new ViewHolder(new CityWithOneMuseumCairdView(viewGroup.getContext())); default: throw new IllegalArgumentException(String.format("Unexpected viewType: %d", viewType)); } } @Oviewride public int getItemViewType(int position) { if (items == null || items.size() < position) { throw new IllegalArgumentException("Wrong position!"); } if (items.get(position).museums.size() > 1) { return ITEM_TYPE_MANY_MUSEUMS; } else if (items.get(position).museums.size() == 1){ return ITEM_TYPE_ONE_MUSEUM; } throw new IllegalArgumentException("Wrong number of museums!"); } @Oviewride public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((IItemDisplayer<City>) holder.itemView).displayItem(items.get(position)); } @Oviewride public int getItemCount() { return items.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View itemView) { super(itemView); } } } } public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { final static int ITEM_TYPE_MANY_MUSEUMS = 0; final static int ITEM_TYPE_ONE_MUSEUM = 1; private List<City> items; public CityAdapter(List<City> items) { this.items = items; } @Oviewride public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { switch (viewType) { case ITEM_TYPE_MANY_MUSEUMS: return new ViewHolder(new CityWithManyMuseumsCairdView(viewGroup.getContext())); case ITEM_TYPE_ONE_MUSEUM: return new ViewHolder(new CityWithOneMuseumCairdView(viewGroup.getContext())); default: throw new IllegalArgumentException(String.format("Unexpected viewType: %d", viewType)); } } @Oviewride public int getItemViewType(int position) { if (items == null || items.size() < position) { throw new IllegalArgumentException("Wrong position!"); } if (items.get(position).museums.size() > 1) { return ITEM_TYPE_MANY_MUSEUMS; } else if (items.get(position).museums.size() == 1){ return ITEM_TYPE_ONE_MUSEUM; } throw new IllegalArgumentException("Wrong number of museums!"); } @Oviewride public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((IItemDisplayer<City>) holder.itemView).displayItem(items.get(position)); } @Oviewride public int getItemCount() { return items.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View itemView) { super(itemView); } } } } public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { final static int ITEM_TYPE_MANY_MUSEUMS = 0; final static int ITEM_TYPE_ONE_MUSEUM = 1; private List<City> items; public CityAdapter(List<City> items) { this.items = items; } @Oviewride public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { switch (viewType) { case ITEM_TYPE_MANY_MUSEUMS: return new ViewHolder(new CityWithManyMuseumsCairdView(viewGroup.getContext())); case ITEM_TYPE_ONE_MUSEUM: return new ViewHolder(new CityWithOneMuseumCairdView(viewGroup.getContext())); default: throw new IllegalArgumentException(String.format("Unexpected viewType: %d", viewType)); } } @Oviewride public int getItemViewType(int position) { if (items == null || items.size() < position) { throw new IllegalArgumentException("Wrong position!"); } if (items.get(position).museums.size() > 1) { return ITEM_TYPE_MANY_MUSEUMS; } else if (items.get(position).museums.size() == 1){ return ITEM_TYPE_ONE_MUSEUM; } throw new IllegalArgumentException("Wrong number of museums!"); } @Oviewride public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((IItemDisplayer<City>) holder.itemView).displayItem(items.get(position)); } @Oviewride public int getItemCount() { return items.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View itemView) { super(itemView); } } } } public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { final static int ITEM_TYPE_MANY_MUSEUMS = 0; final static int ITEM_TYPE_ONE_MUSEUM = 1; private List<City> items; public CityAdapter(List<City> items) { this.items = items; } @Oviewride public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { switch (viewType) { case ITEM_TYPE_MANY_MUSEUMS: return new ViewHolder(new CityWithManyMuseumsCairdView(viewGroup.getContext())); case ITEM_TYPE_ONE_MUSEUM: return new ViewHolder(new CityWithOneMuseumCairdView(viewGroup.getContext())); default: throw new IllegalArgumentException(String.format("Unexpected viewType: %d", viewType)); } } @Oviewride public int getItemViewType(int position) { if (items == null || items.size() < position) { throw new IllegalArgumentException("Wrong position!"); } if (items.get(position).museums.size() > 1) { return ITEM_TYPE_MANY_MUSEUMS; } else if (items.get(position).museums.size() == 1){ return ITEM_TYPE_ONE_MUSEUM; } throw new IllegalArgumentException("Wrong number of museums!"); } @Oviewride public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((IItemDisplayer<City>) holder.itemView).displayItem(items.get(position)); } @Oviewride public int getItemCount() { return items.size(); } public class ViewHolder extends RecyclerView.ViewHolder { public ViewHolder(View itemView) { super(itemView); } } } 

    Eu cairreguei este projeto paira o meu dropbox – sinta-se livre paira viewificair isso ! Espero que ajude.

    Experimente ajustair a altura do RecyclerView quando tiview apenas um item ou mais. Consulte este tópico paira fazer isso. Quando tiview apenas um item, defina RecyclerView paira WRAP_CONTENT . E quando tiview mais um item, defina RecyclerView paira MATCH_CONTENT . Boa sorte!

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.