Refactor ExploreRvAdapter
This commit is contained in:
parent
61b21b0476
commit
005b023638
2 changed files with 42 additions and 58 deletions
|
@ -1,5 +1,6 @@
|
|||
package net.osmand.plus.wikivoyage.explore;
|
||||
|
||||
import android.support.annotation.LayoutRes;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
|
@ -7,13 +8,13 @@ import android.view.View;
|
|||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.OsmandSettings;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.wikivoyage.data.TravelArticle;
|
||||
import net.osmand.plus.wikivoyage.explore.travelcards.ArticleTravelCard;
|
||||
import net.osmand.plus.wikivoyage.explore.travelcards.ArticleTravelCard.ArticleTravelVH;
|
||||
import net.osmand.plus.wikivoyage.explore.travelcards.OpenBetaTravelCard;
|
||||
import net.osmand.plus.wikivoyage.explore.travelcards.OpenBetaTravelCard.OpenBetaTravelVH;
|
||||
import net.osmand.plus.wikivoyage.explore.travelcards.StartEditingTravelCard;
|
||||
import net.osmand.plus.wikivoyage.explore.travelcards.StartEditingTravelCard.StartEditingTravelVH;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -22,63 +23,53 @@ public class ExploreRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
|
|||
|
||||
private static final int HEADER_TYPE = 3;
|
||||
|
||||
private final OsmandSettings settings;
|
||||
|
||||
private final List<Object> items = new ArrayList<>();
|
||||
|
||||
private OsmandApplication app;
|
||||
|
||||
ExploreRvAdapter(OsmandApplication app) {
|
||||
this.app = app;
|
||||
this.settings = app.getSettings();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
if (viewType == OpenBetaTravelCard.TYPE) {
|
||||
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.wikivoyage_open_beta_card, parent, false);
|
||||
return new OpenBetaTravelCard.OpenBetaTravelVH(itemView);
|
||||
}
|
||||
if (viewType == StartEditingTravelCard.TYPE) {
|
||||
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.wikivoyage_start_editing_card, parent, false);
|
||||
return new StartEditingTravelCard.StartEditingTravelVH(itemView);
|
||||
switch (viewType) {
|
||||
case OpenBetaTravelCard.TYPE:
|
||||
return new OpenBetaTravelVH(inflate(parent, R.layout.wikivoyage_open_beta_card));
|
||||
|
||||
case StartEditingTravelCard.TYPE:
|
||||
return new StartEditingTravelVH(inflate(parent, R.layout.wikivoyage_start_editing_card));
|
||||
|
||||
case ArticleTravelCard.TYPE:
|
||||
int layoutId = ArticleTravelCard.USE_ALTERNATIVE_CARD
|
||||
? R.layout.wikivoyage_article_card_alternative
|
||||
: R.layout.wikivoyage_article_card;
|
||||
return new ArticleTravelVH(inflate(parent, layoutId));
|
||||
|
||||
case HEADER_TYPE:
|
||||
return new HeaderVH(inflate(parent, R.layout.wikivoyage_list_header));
|
||||
|
||||
default:
|
||||
throw new RuntimeException("Unsupported view type: " + viewType);
|
||||
}
|
||||
if (viewType == ArticleTravelCard.TYPE) {
|
||||
int layoutId = ArticleTravelCard.USE_ALTERNATIVE_CARD ? R.layout.wikivoyage_article_card_alternative : R.layout.wikivoyage_article_card;
|
||||
View itemView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
|
||||
return new ArticleTravelCard.ArticleTravelVH(itemView);
|
||||
}
|
||||
if (viewType == HEADER_TYPE) {
|
||||
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.wikivoyage_list_header, parent, false);
|
||||
return new HeaderVH(itemView);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private View inflate(@NonNull ViewGroup parent, @LayoutRes int layoutId) {
|
||||
return LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
|
||||
Object object = getItem(position);
|
||||
if (viewHolder instanceof HeaderVH) {
|
||||
Object item = getItem(position);
|
||||
if (viewHolder instanceof HeaderVH && item instanceof String) {
|
||||
final HeaderVH holder = (HeaderVH) viewHolder;
|
||||
holder.title.setText((String) object);
|
||||
holder.title.setText((String) item);
|
||||
holder.description.setText(String.valueOf(getArticleItemCount()));
|
||||
} else if (viewHolder instanceof ArticleTravelCard.ArticleTravelVH) {
|
||||
if (object instanceof ArticleTravelCard) {
|
||||
ArticleTravelCard articleTravelCard = (ArticleTravelCard) object;
|
||||
articleTravelCard.bindViewHolder(viewHolder);
|
||||
}
|
||||
} else if (viewHolder instanceof OpenBetaTravelCard.OpenBetaTravelVH) {
|
||||
if (object instanceof OpenBetaTravelCard) {
|
||||
OpenBetaTravelCard openBetaTravelCard = (OpenBetaTravelCard) object;
|
||||
openBetaTravelCard.bindViewHolder(viewHolder);
|
||||
}
|
||||
} else if (viewHolder instanceof StartEditingTravelCard.StartEditingTravelVH) {
|
||||
if (object instanceof StartEditingTravelCard) {
|
||||
StartEditingTravelCard startEditingTravelCard = (StartEditingTravelCard) object;
|
||||
startEditingTravelCard.bindViewHolder(viewHolder);
|
||||
}
|
||||
} else if (viewHolder instanceof ArticleTravelVH && item instanceof ArticleTravelCard) {
|
||||
ArticleTravelCard articleTravelCard = (ArticleTravelCard) item;
|
||||
articleTravelCard.bindViewHolder(viewHolder);
|
||||
} else if (viewHolder instanceof OpenBetaTravelVH && item instanceof OpenBetaTravelCard) {
|
||||
OpenBetaTravelCard openBetaTravelCard = (OpenBetaTravelCard) item;
|
||||
openBetaTravelCard.bindViewHolder(viewHolder);
|
||||
} else if (viewHolder instanceof StartEditingTravelVH && item instanceof StartEditingTravelCard) {
|
||||
StartEditingTravelCard startEditingTravelCard = (StartEditingTravelCard) item;
|
||||
startEditingTravelCard.bindViewHolder(viewHolder);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -87,14 +78,11 @@ public class ExploreRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
|
|||
Object object = getItem(position);
|
||||
if (object instanceof String) {
|
||||
return HEADER_TYPE;
|
||||
}
|
||||
if (object instanceof OpenBetaTravelCard) {
|
||||
} else if (object instanceof OpenBetaTravelCard) {
|
||||
return ((OpenBetaTravelCard) object).getCardType();
|
||||
}
|
||||
if (object instanceof StartEditingTravelCard) {
|
||||
} else if (object instanceof StartEditingTravelCard) {
|
||||
return ((StartEditingTravelCard) object).getCardType();
|
||||
}
|
||||
if (object instanceof ArticleTravelCard) {
|
||||
} else if (object instanceof ArticleTravelCard) {
|
||||
return ((ArticleTravelCard) object).getCardType();
|
||||
}
|
||||
return -1;
|
||||
|
|
|
@ -3,10 +3,8 @@ package net.osmand.plus.wikivoyage.explore;
|
|||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.FragmentManager;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
@ -14,14 +12,12 @@ import android.view.ViewGroup;
|
|||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.base.BaseOsmAndFragment;
|
||||
import net.osmand.plus.wikivoyage.article.WikivoyageArticleDialogFragment;
|
||||
import net.osmand.plus.wikivoyage.data.TravelArticle;
|
||||
import net.osmand.plus.wikivoyage.explore.travelcards.ArticleTravelCard;
|
||||
import net.osmand.plus.wikivoyage.explore.travelcards.OpenBetaTravelCard;
|
||||
import net.osmand.plus.wikivoyage.explore.travelcards.StartEditingTravelCard;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class ExploreTabFragment extends BaseOsmAndFragment {
|
||||
|
@ -31,7 +27,7 @@ public class ExploreTabFragment extends BaseOsmAndFragment {
|
|||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
final View mainView = inflater.inflate(R.layout.fragment_explore_tab, container, false);
|
||||
ExploreRvAdapter adapter = new ExploreRvAdapter(getMyApplication());
|
||||
ExploreRvAdapter adapter = new ExploreRvAdapter();
|
||||
final RecyclerView rv = (RecyclerView) mainView.findViewById(R.id.recycler_view);
|
||||
rv.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
adapter.setItems(getItems());
|
||||
|
|
Loading…
Reference in a new issue