I have three different layouts cardsListLayout
, titleLayout
, cardMagazineLayout
and there may be more in the future, which used as a views on onCreateViewHolder
method.
I want to switch between views in onCreateViewHolder
method so when user choose from AlertDialog list
this onOptionsItemSelected in MainActivity Where there is menu Item "change the layout" so that when the user presses it it appears AlertDialog list
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.change_layout) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.choose_layout));
String[] layouts = {"Title Layout", "Cards List", "Card Magazine Layout"};
builder.setItems(layouts, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int index) {
switch (index) {
case 0: // Title layout
Toast.makeText(MainActivity.this,
"Title layout", Toast.LENGTH_LONG).show();
break;
case 1: // Cards List
Toast.makeText(MainActivity.this,
"Card list", Toast.LENGTH_LONG).show();
break;
case 2: // Cards Magazine Layout
Toast.makeText(MainActivity.this,
"Card Magazine Layout", Toast.LENGTH_LONG).show();
}
}
});
and this my custom adapter class PostAdapter
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> {
private Context context;
private List<Item> items;
public PostAdapter(Context context, List<Item> items) {
this.context = context;
this.items = items;
}
@NonNull
@Override
public PostAdapter.PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
// here's the three layouts that I can't switch between it
View cardsListLayout = inflater.inflate(R.layout.post_item_card_layout, parent, false);
View titleLayout = inflater.inflate(R.layout.post_item_grid_layout, parent, false);
View cardMagazineLayout = inflater.inflate(R.layout.card_magazine_layout,parent,false);
return new PostViewHolder(titleLayout);
}
@Override
public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
final Item item = items.get(position);
holder.postTitle.setText(item.getTitle());
final Document document = Jsoup.parse(item.getContent());
Elements elements = document.select("img");
Log.e("CODE", "Image: " + elements.get(0).attr("src"));
// Log.d("Text",document.text());
// holder.postDescription.setText(document.text());
Glide.with(context).load(elements.get(0).attr("src"))
.into(holder.postImage);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, DetailsActivity.class);
intent.putExtra("url", item.getUrl());
intent.putExtra("title", item.getTitle());
intent.putExtra("content", item.getContent());
int youtubeThumbnailImagesetVisibility = 0;
Element element = document.body();
String youtubeThumbnailImageSrc = "";
String youTubeLink = "";
for (Element e : element.getElementsByClass
("YOUTUBE-iframe-video")) {
youtubeThumbnailImageSrc = e.attr("data-thumbnail-src");
youTubeLink = e.attr("src");
Log.e("YouTube thumbnail", youtubeThumbnailImageSrc);
Log.e("Youtube link", youTubeLink);
}
if (youtubeThumbnailImageSrc.isEmpty()) {
youtubeThumbnailImagesetVisibility = 8;
intent.putExtra("youtubeThumbnailImagesetVisibility",
youtubeThumbnailImagesetVisibility);
} else {
intent.putExtra("youtubeThumbnailImageSrc", youtubeThumbnailImageSrc);
intent.putExtra("youTubeLink", youTubeLink);
}
// String imageSrc = elements.get(0).attr("src");
// intent.putExtra("blogImage",imageSrc);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return items.size();
}
public class PostViewHolder extends RecyclerView.ViewHolder {
ImageView postImage;
TextView postTitle;
TextView postDescription;
public PostViewHolder(View itemView) {
super(itemView);
postImage = itemView.findViewById(R.id.postImage);
postTitle = itemView.findViewById(R.id.postTitle);
postDescription = itemView.findViewById(R.id.postDescription);
}
}
}
I created two different Layout Manager for each one, there's a LinearLayoutManager and GridLayoutManager, currently I used GridLayout so I commented the LinearLayoutManger temporarily until I know how to switch between them
// linearLayoutManager = new LinearLayoutManager(this);
gridLayoutManager = new GridLayoutManager(this, 2, RecyclerView.VERTICAL, false);
// recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setLayoutManager(gridLayoutManager);
The result I aspire to