I am making an app in which I want to set images onto gridview and on itemclick I want to show the description of an image via textview. I have already done the setting of images onto gridview, but now I want to do the second part of it.
Following Images shows the visualization of The layout :
Before :
After :
Basically, I want to Add a new layout in between of 2 rows of gridview below the clicked image. I have done a lot of research and also seen many libraries and StackOverflow answers but can't figure out to do so. I need example code to do so for gridview or any other views. Thanks.
All the stackoverflow Answers and libraries that i have gone through :=
Android expandable GridView like Google images
Add view between 2 rows Gridview
Dynamically adding a whole row in GridView, or splitting the View
Libraries ->
https://github.com/thewaychung/ExpandableGridView
https://github.com/paolorotolo/ExpandableHeightListView
You can try the following approach for your reference :
I am using text view instead of ImageView. But the concept remain same.
It is displayed something like this.
Const.java : Class to keep constants.
public class Const {
public static final int DES_VIEW=0;
public static final int FOOD_VIEW=1;
public static final int FAKE_VIEW=2;
public static final int COLUMN_NUMBER = 2;
}
RowData.java : An Interface which will be used by the adapter class to get view type of each element.
public interface RowData {
int getViewType();
}
FoodItem.java : Just a plain pojo class which implements RowData ( The parent item FoodItem item 0 is populated )
public class FoodItem implements RowData{
public int getViewType() {
return viewType;
}
public void setViewType(int viewType) {
this.viewType = viewType;
}
private int viewType;
public boolean isExpanded() {
return isExpanded;
}
public void setExpanded(boolean expanded) {
isExpanded = expanded;
}
private boolean isExpanded;
public String getFoodName() {
return foodName;
}
public void setFoodName(String foodName) {
this.foodName = foodName;
}
private String foodName;
public String getDescription() {
return description == null ? "DEFAULT" : description;
}
public void setDescription(String description) {
this.description = description;
}
private String description;
}
FakeData.java : Pojo class to populate empty views.
public class FakeData implements RowData {
#Override
public int getViewType() {
return Const.FAKE_VIEW;
}
}
FoodAdapter.java : Adapter class.
public class FoodAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public void setFoodItems(List<RowData> foodItems) {
this.foodItems = foodItems;
// addBlankViews();
}
private List<RowData> foodItems;
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case Const.FOOD_VIEW:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout, parent, false);
return new VH(view);
case Const.DES_VIEW:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.des, parent, false);
return new DesView(view);
case Const.FAKE_VIEW:
Log.e("CALLED", " ---> " + viewType);
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout, parent, false);
return new FakeView(view);
default:
return null;
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
int view = getItemViewType(position);
switch (view) {
case Const.FAKE_VIEW: {
FakeView fakeView = (FakeView) holder;
Log.e("CALLED", position + " " + view);
}
break;
case Const.DES_VIEW: {
DesView desView = (DesView) holder;
desView.setData((FoodItem) getFoodItem(position));
}
break;
case Const.FOOD_VIEW: {
VH foodView = (VH) holder;
foodView.setData((FoodItem) getFoodItem(position));
}
break;
}
}
RowData getFoodItem(int position) {
if (foodItems != null) {
return foodItems.get(position);
}
return null;
}
#Override
public int getItemViewType(int position) {
RowData foodItem = getFoodItem(position);
if (foodItem != null) {
return foodItem.getViewType();
}
return -1;
}
#Override
public int getItemCount() {
if (foodItems == null) {
return 0;
} else {
return foodItems.size();
}
}
class VH extends RecyclerView.ViewHolder {
TextView foodItemName;
public VH(View itemView) {
super(itemView);
foodItemName = itemView.findViewById(R.id.food_item);
foodItemName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FoodAdapter.this.onClick(getAdapterPosition());
}
});
}
void setData(FoodItem data) {
foodItemName.setText(data.getFoodName());
}
}
class DesView extends RecyclerView.ViewHolder {
TextView foodDesView;
public DesView(View itemView) {
super(itemView);
foodDesView = itemView.findViewById(R.id.des_item);
foodDesView.setTextColor(Color.BLUE);
}
void setData(FoodItem data) {
foodDesView.setText(data.getDescription());
}
}
class FakeView extends RecyclerView.ViewHolder {
public FakeView(View itemView) {
super(itemView);
}
}
private int currentExpandedIndex = -1;
private void onClick(int position) {
// calculation to show and hide the child view (with magenta background ).
if (currentExpandedIndex != -1) {
foodItems.remove(currentExpandedIndex);
currentExpandedIndex = -1;
notifyDataSetChanged();
return;
}
int startIndex = position % Const.COLUMN_NUMBER;
if (startIndex == 0) {
startIndex = position;
} else {
startIndex = position - startIndex;
}
startIndex = startIndex + Const.COLUMN_NUMBER;
if (currentExpandedIndex == -1) {
FoodItem des = new FoodItem();
des.setViewType(Const.DES_VIEW);
FoodItem foodItem = (FoodItem) getFoodItem(position);
des.setDescription(foodItem.getDescription());
foodItems.add(startIndex, des);
currentExpandedIndex = startIndex;
}
notifyDataSetChanged();
}
}
MainActivity.java : Activity class.
public class MainActivity extends AppCompatActivity {
private FoodAdapter foodAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.reycleView);
((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
foodAdapter = new FoodAdapter();
foodAdapter.setFoodItems(getFoodItems());
GridLayoutManager mLayoutManager = new GridLayoutManager(this,Const.COLUMN_NUMBER);
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int position) {
switch (foodAdapter.getItemViewType(position)) {
case Const.DES_VIEW:
return Const.COLUMN_NUMBER;
default:
return 1;
}
}
});
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(foodAdapter);
}
private List<RowData> foodItems;
private List<RowData> getFoodItems() {
foodItems = new ArrayList<>();
for (int i = 0; i < 18; i++) {
FoodItem foodItem = new FoodItem();
foodItem.setFoodName(" FoodItem item " + i);
foodItem.setExpanded(false);
foodItem.setDescription("Fake des of " + i);
foodItem.setViewType(Const.FOOD_VIEW);
foodItems.add(foodItem);
}
// calculation to add empty view.
// these view will be added when, there is shortage of parent views to fill row completely
int size = foodItems.size();
int blankView = size % Const.COLUMN_NUMBER;
if (blankView != 0) {
blankView = Const.COLUMN_NUMBER - blankView;
for (int i = 0; i < blankView; i++) {
foodItems.add(new FakeData());
}
}
return foodItems;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="check.service.com.genericrecylerview.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/reycleView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
des.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#color/colorAccent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/des_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="#+id/food_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Related
Hi,
I'm making a dynamic Nested RecyclerView.
The Outer RecyclerView (Routine) consists of parts (A, B, C...), add button,
and delete button, and the inner RecyclerView (Detail Routine) consists of text view (set, weight and count).
When i add an outer item, it have one inner RecyclerView item by default.
I thought the implementation was successful, but it wasn't.
The picture is a condition that I have made some items in advance.
This is a picture that creates additional items there.
However, in the picture, when I added the G item, you can see that the F item
and the B item have a change.
And the G item, oddly enough, has two basic items. Originally, it is one.
The second picture is the later item addition.
If i add a new outer recyclerview item, there will be a new change in the whole
item again.
I am not sure why this change is happening. If you add a new item, you want the
existing item to remain.
Please tell me the cause
Here is Code
RoutineAdapter.java ( Outer RecyclerView Adpater )
public class RoutineAdapter extends RecyclerView.Adapter<RoutineAdapter.ViewHolder> {
Context context;
ArrayList<RoutineModel> routineItems = new ArrayList<>();
public RoutineDetailAdapter detailAdapter;
OnRoutineItemClickListener listener;
public void setOnRoutineClickListener(OnRoutineItemClickListener listener) {
this.listener = listener;
}
public void addItem(RoutineModel item) {
routineItems.add(item);
notifyDataSetChanged();
}
public void addDetailItem(RoutineDetailAdapter curDetailAdapter) {
curDetailAdapter.addItem(new RoutineDetailModel());
}
public void deleteDetailItem(RoutineDetailAdapter curDetailAdapter) {
curDetailAdapter.deleteItem();
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View itemView = inflater.inflate(R.layout.routine_item, parent, false);
ViewHolder holder = new ViewHolder(itemView);
detailAdapter = new RoutineDetailAdapter();
holder.setRoutineDetailRecyClerView();
holder.rv_detail.setAdapter(detailAdapter);
detailAdapter.addItem(new RoutineDetailModel());
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
RoutineModel curRoutineItem = routineItems.get(position);
holder.setItems(curRoutineItem);
}
#Override
public int getItemCount() {
return routineItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
RecyclerView rv_detail;
TextView routine;
Button addSet;
Button deleteSet;
public ViewHolder(#NonNull View itemView) {
super(itemView);
initViews();
addSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RoutineDetailAdapter curDetailAdapter = (RoutineDetailAdapter) rv_detail.getAdapter();
listener.OnAddBtnClick(curDetailAdapter);
}
});
deleteSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RoutineDetailAdapter curDetailAdapter = (RoutineDetailAdapter) rv_detail.getAdapter();
listener.OnDeleteBtnClick(curDetailAdapter);
}
});
}
private void initViews() {
routine = itemView.findViewById(R.id.routine);
rv_detail = itemView.findViewById(R.id.detail_routine);
addSet = itemView.findViewById(R.id.add_set);
deleteSet = itemView.findViewById(R.id.delete_set);
}
private void setItems(RoutineModel routineItem) {
routine.setText(routineItem.getRoutine());
}
public void setRoutineDetailRecyClerView() {
rv_detail.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
}
}
public interface OnRoutineItemClickListener {
public void OnAddBtnClick(RoutineDetailAdapter detailAdapter);
public void OnDeleteBtnClick(RoutineDetailAdapter detailAdapter);
}
}
RoutineDetailAdapter.java ( Inner RecyclerView Adapter )
public class RoutineDetailAdapter extends RecyclerView.Adapter<RoutineDetailAdapter.ViewHolder>{
ArrayList<RoutineDetailModel> items = new ArrayList<>();
Context context;
int insertPosition;
int deletePosition;
public void addItem(RoutineDetailModel item) {
items.add(item);
notifyItemInserted(insertPosition);
}
public void deleteItem() {
try {
if(items.size() > 1) { // Leave at least one set
items.remove(deletePosition);
notifyItemRemoved(deletePosition);
deletePosition -= 1;
}
} catch (Exception e) {
// empty
}
}
public ArrayList<RoutineDetailModel> getItem() {
return this.items;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.routine_detail_item, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
RoutineDetailModel item = items.get(position);
holder.setItem(item, position);
insertPosition = position + 1;
deletePosition = position;
}
#Override
public int getItemCount() {
return items.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView set;
public ViewHolder(#NonNull View itemView) {
super(itemView);
set = itemView.findViewById(R.id.set);
}
private void setItem(RoutineDetailModel item, int setCount) {
set.setText(setCount + 1 + "set");
}
}
}
WriteRoutineActivity.java
public class WriteRoutineActivity extends AppCompatActivity {
Button add_routine_btn;
TextView title;
RecyclerView routine_rv;
RoutineAdapter routineAdapter;
LinearLayoutManager routineLayoutManger;
ArrayList<String> titleData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write_routine);
initViews();
setPageTitle(getIntent());
setRoutineRecyclerview();
routineAdapter = new RoutineAdapter();
routine_rv.setAdapter(routineAdapter);
add_routine_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
WorkoutListDialogFragment routineDialog = new WorkoutListDialogFragment();
routineDialog.show(getSupportFragmentManager(), "RoutineListDialog");
}
});
routineAdapter.setOnRoutineClickListener(new RoutineAdapter.OnRoutineItemClickListener() {
#Override
public void OnAddBtnClick(RoutineDetailAdapter curDetailAdapter) {
routineAdapter.addDetailItem(curDetailAdapter);
}
#Override
public void OnDeleteBtnClick(RoutineDetailAdapter curDetailAdapter) {
routineAdapter.deleteDetailItem(curDetailAdapter);
}
});
}
private void initViews() {
title = findViewById(R.id.body_part_detail_title);
routine_rv = findViewById(R.id.routine_recyclerview);
add_routine_btn = findViewById(R.id.add_routine);
}
private void setRoutineRecyclerview() {
routineLayoutManger = new LinearLayoutManager(getApplicationContext(), RecyclerView.VERTICAL, false);
routine_rv.setLayoutManager(routineLayoutManger);
routine_rv.setHasFixedSize(true);
RecyclerView.ItemDecoration divider = new DividerItemDecorator(ContextCompat.getDrawable(getApplicationContext(), R.drawable.divider));
routine_rv.addItemDecoration(divider);
}
public void setPageTitle(Intent intent) {
if(intent != null) {
titleData = intent.getStringArrayListExtra("bodypart");
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String new_title = String.join(" / ", titleData);
title.setText(new_title);
}
else {
StringBuilder new_title = new StringBuilder();
for (int i = 0; i < titleData.size(); i++) {
if (i == titleData.size() - 1) {
new_title.append(titleData.get(i));
break;
}
new_title.append(titleData.get(i)).append(" / ");
}
title.setText(new_title);
}
}
}
public void addRoutine(String routine) {
routineAdapter.addItem(new RoutineModel(routine));
routine_rv.smoothScrollToPosition(routineAdapter.getItemCount() - 1);
}
}
ADDED
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".data.DailyRecordDetailActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.AppBarOverlay"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/body_part_detail_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PART"
android:textColor="#color/white"
android:textAppearance="#style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<Button
android:id="#+id/add_routine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ROUTINE ADD"
android:backgroundTint="#color/light_green"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#+id/routine_recyclerview"
app:layout_constraintRight_toRightOf="parent"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/routine_recyclerview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/add_routine"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
RoutineItem.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/white"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<LinearLayout
android:id="#+id/linear_routine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
<TextView
android:id="#+id/routine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="10dp"
android:text="ROUTINE"
android:textSize="16dp"
android:textStyle="bold"
android:layout_weight="2"/>
<Button
android:id="#+id/add_set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:text="ADD"
android:textColor="#color/black"
android:gravity="center"
android:layout_weight="1"/>
<Button
android:id="#+id/delete_set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:text="DELETE"
android:textColor="#color/black"
android:layout_weight="1"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/detail_routine"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="#+id/linear_routine"
app:layout_constraintLeft_toLeftOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
RoutineDetail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="5dp"
android:padding="10dp"
android:clipToPadding="false">
<TextView
android:id="#+id/set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="SET"/>
<TextView
android:id="#+id/weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0 kg"/>
<TextView
android:id="#+id/reps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"/>
</LinearLayout>
For every RoutineModel item you recreate detailAdapter but you need new instance. Check TODO in code below:
public class RoutineAdapter extends RecyclerView.Adapter<RoutineAdapter.ViewHolder> {
Context context;
ArrayList<RoutineModel> routineItems = new ArrayList<>();
//public RoutineDetailAdapter detailAdapter; //TODO move it to local variable in method onCreateViewHolder
OnRoutineItemClickListener listener;
like this
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View itemView = inflater.inflate(R.layout.routine_item, parent, false);
ViewHolder holder = new ViewHolder(itemView);
RoutineDetailAdapter detailAdapter = new RoutineDetailAdapter(); //TODO Create local variable
detailAdapter.addItem(new RoutineDetailModel());
holder.setRoutineDetailRecyClerView();
holder.rv_detail.setAdapter(detailAdapter);
return holder;
}
But this is not good way to solve this... Its better to use one Adapter with multiple item views. I have no time now to explain, I will post some code later...
EDITED
I had same problem and and solved this on this (better) way:
Create Adapter that support multiple views
public class MultipleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<Object> items; //List<Object or Generic class or abstarct base Model class parent of Routine and RoutineDetails>
private OnItemClickListener listener; //TODO new interface
..............
}
Override onCreateViewHolder like this
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if(viewType == 1){
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.routine_item, parent, false);
return new RoutineViewHolder (itemView);
}/*else{*/
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.routine_detail_item, parent, false);
return new RoutineDetailsViewHolder (itemView);
//}
}
Override onBindViewHolder like this and create your updateViews methods
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Object object = items.get(position); //Object or Generic class or abstarct base Model class parent of Routine and RoutineDetails>
if(object instanceof RoutineModel) {
//create method
updateRoutineViews((RoutineViewHolder) holder, (RoutineModel) object, position);
}else if(object instanceof RoutineDetailsModel) {
//create method
updateRoutineDetailsViewHolder((RoutineDetailsViewHolder) holder, (RoutineDetailModel) object);
}
}
and override getItemViewType method
#Override
public int getItemViewType(int position) {
Object object = items.get(position);
if(object instanceof RoutineModel){
return 1;
}
//else if instanceOf RoutineDetailModel return 0
return 0;
}
This is so simple and better for performance...
Also in OnItemClickListener get item by position and check instanceOf object like above. You can add new item to the clickedItemPosition + 1 with adapter.notifyDataSetChanges()
In MainActivity you have to create mixed list with Routine and Routine details
List<Object> mixedList = new ArrayList<>();
for(RoutineModel rm: routineList){
mixedList.add(rm);
if(rm has routineDetailModels){ //pseudo code
for(RoutineDetailModels rmdetilas: rm.getRoutineDetailModels()){
mixedList.add(rmdetilas);
}
}
}
adpater.swapData(mixedList); //create your method to swap or set data to adapter
EDITED
handle click
public interface OnItemClickListener{
void onClick(View view, int position);
void onLongClick(View view, int position);
}
and
adapter.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onClick(View view, int position) {
Object item = (Object) adapter.getItem(position);
if(item instanceof Routine) {
if (view.getId() == R.id.delete_id) {
//TODO delete form list, notifyItemRemoved or generate all mixeddata again and swap
} else if (view.getId() == R.id.whateveryouwant) {
//TODO doSomething()
}
}
}
#Override
public void onLongClick(View view, int position) {
}
});
The code above is just an example of how to solve the problem
For more help feel free to comment
EDITED 2
public class RoutineViewHolder extends RecyclerView.ViewHolder {
public RecyclerView rv_detail;
public TextView routine;
public Button addSet;
public Button deleteSet;
public ViewHolder(#NonNull View itemView) {
super(itemView);
//initViews(); in constructor
routine = itemView.findViewById(R.id.routine);
rv_detail = itemView.findViewById(R.id.detail_routine);
addSet = itemView.findViewById(R.id.add_set);
deleteSet = itemView.findViewById(R.id.delete_set);
}
}
update method called from onBindViewHolder
private void updateRoutineViews(RoutineViewHolder holder, RoutineModel routineItem, position){
holder.routine.setText(routineItem.getRoutine());
holder.rv_detail.setText(routineItem.getWhateverYouWant());
holder.deleteSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onItemClickListener.onClick(v, position); //or holder.getAdapterPosition()
}
});
}
COMPLETE CODE
Create complete new app to test it and understand (copy/paste this code), then integrate in your app...
There are more improvements but I don't have time, this is a quick fix
Activity
public class RoutineActivity extends AppCompatActivity {
private MultipleViewAdapter adapter;
private List<RoutineModel> routineList;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_routine);
RecyclerView contentList = findViewById(R.id.list);
contentList.setLayoutManager(new LinearLayoutManager(this));
adapter = new MultipleViewAdapter(this, new ArrayList<>());
contentList.setAdapter(adapter);
adapter.setOnItemClickListener(new MultipleViewAdapter.OnItemClickListener() {
#Override
public void onClick(View view, int position) {
Object item = (Object) adapter.getItem(position);
if(item instanceof RoutineModel) {
RoutineModel routineModel = (RoutineModel) item;
if (view.getId() == R.id.add_set) {
int weight = randomInt(99);
Toast.makeText(RoutineActivity.this, "New item with weight: " + weight + " kg", Toast.LENGTH_SHORT).show();
routineModel.addDetails(new RoutineDetailsModel(routineModel.getDetailsSize() + 1, weight));
adapter.swapData(getMixedList()); // OR add item to adapter and notify item inserted
//TODO implement SnappingLinearLayoutManager and set list.smoothScroleto... routineModel
} else if (view.getId() == R.id.delete_set) {
//TODO doSomething()
boolean deleted = routineModel.removeDetails(routineModel.getDetailsSize() - 1); // -1 !!! to delete last item
Toast.makeText(RoutineActivity.this, deleted ? "Last item is deleted": "No more items", Toast.LENGTH_SHORT).show();
adapter.swapData(getMixedList()); // OR remove item from adapter and notify item removed
//TODO implement SnappingLinearLayoutManager and set list.smoothScroleto.... routineModel
}
}else if(item instanceof RoutineDetailsModel) {
RoutineDetailsModel routineModel = (RoutineDetailsModel) item;
Toast.makeText(RoutineActivity.this, "Weight: " + routineModel.getWeight() + " kg", Toast.LENGTH_SHORT).show();
//TODO EDITED 4 (copy/paste this) random new Weight
routineModel.setWeight(randomInt(99));
adapter.notifyItemChanged(position);
Toast.makeText(RoutineActivity.this, "New Weight: " + routineModel.getWeight() + " kg", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onLongClick(View view, int position) {
}
});
initFakeData();
adapter.swapData(getMixedList());
}
private List<Object> getMixedList() {
List<Object> mixedList = new ArrayList<>();
for(RoutineModel rm: routineList){
mixedList.add(rm);
if(rm.getRoutineDetailsModel() != null && rm.getRoutineDetailsModel().size() > 0){
for(RoutineDetailsModel rmdetilas: rm.getRoutineDetailsModel()){
mixedList.add(rmdetilas);
}
}
}
return mixedList;
}
private void initFakeData() {
routineList = new ArrayList<>();
for(int i = 0; i < 5; i++){
RoutineModel routineModel = new RoutineModel(String.valueOf(i + 1));
for(int j = 0; j < 4; j++){
routineModel.addDetails(new RoutineDetailsModel(j+1, randomInt(99)));
}
routineList.add(routineModel);
}
}
private int randomInt(int max) {
return (int) Math.floor(Math.random() * max);
}
}
Activity Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Adapter
public class MultipleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Context context; // TODO only if you need conetxt
private List<Object> items;
private OnItemClickListener onItemClickListener;
public MultipleViewAdapter(Context context, List<Object> items) {
this.context = context;
this.items = items;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if(viewType == 1){
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.routine_item, parent, false);
return new RoutineViewHolder(itemView);
}/*else{*/
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.routine_detail_item, parent, false);
return new RoutineDetailsViewHolder(itemView);
//}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
Object object = items.get(position); //Object or Generic class or abstarct base Model class parent of Routine and RoutineDetails>
if(object instanceof RoutineModel) {
updateRoutineViews((RoutineViewHolder) holder, (RoutineModel) object, position);
}else if(object instanceof RoutineDetailsModel) {
updateRoutineDetailsViewHolder((RoutineDetailsViewHolder) holder, (RoutineDetailsModel) object, position);
}
}
private void updateRoutineViews(RoutineViewHolder holder, RoutineModel routineItem, int position){
holder.routine.setText("Routine " + routineItem.getRoutine());
holder.addSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(onItemClickListener != null) onItemClickListener.onClick(v, position);
}
});
holder.deleteSet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(onItemClickListener != null) onItemClickListener.onClick(v, position);
}
});
}
private void updateRoutineDetailsViewHolder(RoutineDetailsViewHolder holder, RoutineDetailsModel routineDetailsModel, int position){
holder.set.setText(routineDetailsModel.getSet() + " set");
holder.weight.setText(routineDetailsModel.getWeight() + " kg");
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(onItemClickListener != null) onItemClickListener.onClick(v, position);
}
});
}
#Override
public int getItemViewType(int position) {
Object object = items.get(position);
if(object instanceof RoutineModel){
return 1;
}
//else if instanceOf RoutineDetailModel return 0
return 0;
}
#Override
public int getItemCount() {
if(items == null) return 0;
return items.size();
}
public Object getItem(int position) {
if(this.items == null || position < 0 || position >= this.items.size()) return null;
return this.items.get(position);
}
public void swapData(List<Object> newItems) {
if (newItems != null) {
this.items = newItems;
notifyDataSetChanged();
}
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
public interface OnItemClickListener{
void onClick(View view, int position);
void onLongClick(View view, int position);
}
public static class RoutineViewHolder extends RecyclerView.ViewHolder {
public TextView routine;
public Button addSet;
public Button deleteSet;
public RoutineViewHolder(#NonNull View itemView) {
super(itemView);
//initViews(); in constructor
routine = itemView.findViewById(R.id.routine);
addSet = itemView.findViewById(R.id.add_set);
deleteSet = itemView.findViewById(R.id.delete_set);
}
}
public static class RoutineDetailsViewHolder extends RecyclerView.ViewHolder {
public TextView set;
public TextView weight;
public RoutineDetailsViewHolder(#NonNull View itemView) {
super(itemView);
set = itemView.findViewById(R.id.set);
weight = itemView.findViewById(R.id.weight);
}
}
}
RoutineModel
public class RoutineModel {
private List<RoutineDetailsModel> routineDetailsList;
private String routine;
public RoutineModel(String routine) {
this.routine = routine;
}
public List<RoutineDetailsModel> getRoutineDetailsModel() {
return routineDetailsList;
}
public void setRoutineDetailsModel(List<RoutineDetailsModel> routineDetailsModel) {
this.routineDetailsList = routineDetailsModel;
}
public void addDetails(RoutineDetailsModel item) {
if(routineDetailsList == null) {
routineDetailsList = new ArrayList<>();
}
this.routineDetailsList.add(item);
}
public boolean removeDetails(int index) {
if(routineDetailsList == null || index >= routineDetailsList.size() || index < 0) return false;
this.routineDetailsList.remove(index);
return true;
}
public String getRoutine() {
return routine;
}
public void setRoutine(String routine) {
this.routine = routine;
}
public int getDetailsSize() {
if(routineDetailsList == null) return 0;
return routineDetailsList.size();
}
}
RoutineDetailsModel
public class RoutineDetailsModel {
private int set;
private int weight;
public RoutineDetailsModel(int set, int weight) {
this.set = set;
this.weight = weight;
}
public int getSet() {
return set;
}
public void setSet(int set) {
this.set = set;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
Item layouts
Find in question: routine_item.xml and routine_detail_item.xml
I have two nested RecyclerViews - basically a list of horizontal RecyclerViews and I am struggling to make the items in the horizontal RecyclerView clickable.
This is the main layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/overview_list"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<components.emptyView.EmptyView
android:id="#+id/empty_state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"/>
</LinearLayout>
Then RecyclerView has the following item layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView android:id="#+id/tables"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:clipToPadding="false"
android:paddingEnd="18dp"
android:paddingStart="18dp"/>
And I'm trying to get the onclick setup in the adapter for the inner RecyclerView as follows:
#Override
public void onBindViewHolder(#NonNull LeagueTableViewHolder holder, int position) {
Pair<Long, String> table = tables.get(position);
holder.bindTable(table.first, table.second);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// click event here
}
});
}
I've tried making the ViewHolder's itemview as clickable programatically and in XML and it doesn't seem to make a difference. Same for the inner RecyclerView.
Any idea what I'm doing wrong?
Edit: Added parent RecyclerView adapter
public class OverviewListAdapter extends RecyclerView.Adapter<BaseViewHolder> {
public final static int TEAM_PICTURE = 0;
public final static int FIXTURES = 1;
public final static int LEAGUE_TABLES = 2;
public final static int LATEST_NEWS = 3;
public final static int LATEST_EVENTS = 4;
private final RecyclerView.RecycledViewPool viewPool;
private List<BaseOverviewItemVM> viewModels = new ArrayList<>();
private int tint;
private OverviewLatestNewsVM newsVM;
private OverviewLatestEventsVM eventsVM;
private OverviewTablesVM tablesVM;
private OverviewFixturesVM fixturesVM;
private OverviewTeamPicVM teamPicVM;
OverviewListAdapter(int tint) {
this.tint = tint;
viewPool = new RecyclerView.RecycledViewPool();
}
void addViewModel(OverviewLatestNewsVM viewModel){
if(newsVM == null){
newsVM = viewModel;
viewModels.add(newsVM);
sort();
int viewModelIndex = viewModels.indexOf(newsVM);
notifyItemInsertedIfValid(viewModelIndex);
} else {
int currentNewsIndex = viewModels.indexOf(newsVM);
newsVM = viewModel;
if(currentNewsIndex >= 0) {
this.viewModels.set(currentNewsIndex, newsVM);
}
notifyItemChangedIfValid(currentNewsIndex);
}
}
void addViewModel(OverviewLatestEventsVM viewModel){
if(eventsVM == null){
eventsVM = viewModel;
viewModels.add(eventsVM);
sort();
int viewModelIndex = viewModels.indexOf(eventsVM);
notifyItemChangedIfValid(viewModelIndex);
} else {
int currentIndex = viewModels.indexOf(eventsVM);
eventsVM = viewModel;
if(currentIndex >= 0) {
this.viewModels.set(currentIndex, eventsVM);
}
notifyItemChangedIfValid(currentIndex);
}
}
void addViewModel(OverviewFixturesVM viewModel){
if(fixturesVM == null){
fixturesVM = viewModel;
viewModels.add(fixturesVM);
sort();
int viewModelIndex = viewModels.indexOf(fixturesVM);
notifyItemChangedIfValid(viewModelIndex);
} else {
int currentIndex = viewModels.indexOf(fixturesVM);
fixturesVM = viewModel;
if(currentIndex >= 0) {
this.viewModels.set(currentIndex, fixturesVM);
}
notifyItemChangedIfValid(currentIndex);
}
}
void addViewModel(OverviewTablesVM viewModel){
if(tablesVM == null){
tablesVM = viewModel;
viewModels.add(tablesVM);
sort();
int viewModelIndex = viewModels.indexOf(tablesVM);
notifyItemChangedIfValid(viewModelIndex);
} else {
int currentIndex = viewModels.indexOf(tablesVM);
tablesVM = viewModel;
if(currentIndex >= 0) {
this.viewModels.set(currentIndex, tablesVM);
}
notifyItemChangedIfValid(currentIndex);
}
}
void addViewModel(OverviewTeamPicVM viewModel){
if(teamPicVM == null){
teamPicVM = viewModel;
viewModels.add(teamPicVM);
sort();
int viewModelIndex = viewModels.indexOf(teamPicVM);
notifyItemChangedIfValid(viewModelIndex);
} else {
int currentIndex = viewModels.indexOf(teamPicVM);
teamPicVM = viewModel;
if(currentIndex >= 0) {
this.viewModels.set(currentIndex, teamPicVM);
}
notifyItemChangedIfValid(currentIndex);
}
}
private void sort(){
// sort by the item type defined at the top of this class
Collections.sort(viewModels, (o1, o2) -> {
if(o1.getType() > o2.getType()){
return 1;
} else if(o1.getType() < o2.getType()){
return -1;
}
return 0;
});
}
private void notifyItemInsertedIfValid(int index){
if(index >= 0 && index < getItemCount()){
notifyItemInserted(index);
} else {
notifyDataSetChanged();
}
}
private void notifyItemChangedIfValid(int index){
if(index >= 0 && index < getItemCount()){
notifyItemChanged(index);
} else {
notifyDataSetChanged();
}
}
#NonNull
#Override
public BaseViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
switch (viewType){
case FIXTURES:
View fixturesView = inflater.inflate(R.layout.row_overview_fixtures, parent, false);
OverviewFixturesVH fixturesHolder = new OverviewFixturesVH(fixturesView);
fixturesHolder.fixturesOverview.setRecycledViewPool(viewPool);
return fixturesHolder;
case LEAGUE_TABLES:
View tablesView = inflater.inflate(R.layout.row_overview_league_tables, parent, false);
OverviewTablesVH tablesHolder = new OverviewTablesVH(tablesView);
tablesHolder.tablesList.setRecycledViewPool(viewPool);
return tablesHolder;
case LATEST_EVENTS:
View upcomingEvents = inflater.inflate(R.layout.row_latest_list_view, parent, false);
return new OverviewLatestEventsVH(upcomingEvents, tint);
case TEAM_PICTURE:
View teamPicView = inflater.inflate(R.layout.row_overview_team_pic, parent, false);
return new OverviewTeamPicVH(teamPicView);
default:
View latestNewsView = inflater.inflate(R.layout.row_latest_list_view, parent, false);
return new OverviewLatestNewsVH(latestNewsView, tint);
}
}
#Override
public void onBindViewHolder(#NonNull BaseViewHolder holder, int position) {
if(holder instanceof OverviewFixturesVH) {
Optional<BaseOverviewItemVM> fixturesVM = getViewModelOfType(FIXTURES);
fixturesVM.ifPresent(viewModel -> ((OverviewFixturesVH) holder).setFixtures(((OverviewFixturesVM)viewModel).getFixtures()));
} else if (holder instanceof OverviewTablesVH) {
Optional<BaseOverviewItemVM> tablesVM = getViewModelOfType(LEAGUE_TABLES);
tablesVM.ifPresent(vm -> ((OverviewTablesVH) holder).setTableIds(((OverviewTablesVM) vm).getTableIds()));
} else if (holder instanceof OverviewLatestEventsVH){
Optional<BaseOverviewItemVM> eventsVM = getViewModelOfType(LATEST_EVENTS);
eventsVM.ifPresent(vm -> ((OverviewLatestEventsVH) holder).setEvents(((OverviewLatestEventsVM) vm).getEvents()));
} else if (holder instanceof OverviewLatestNewsVH){
Optional<BaseOverviewItemVM> newsVM = getViewModelOfType(LATEST_NEWS);
newsVM.ifPresent(vm -> ((OverviewLatestNewsVH) holder).setNews(((OverviewLatestNewsVM) vm).getNews()));
} else if (holder instanceof OverviewTeamPicVH){
Optional<BaseOverviewItemVM> teamPicVM = getViewModelOfType(TEAM_PICTURE);
teamPicVM.ifPresent(vm -> ((OverviewTeamPicVH) holder).setPictureUrl(((OverviewTeamPicVM) vm).getTeamPicUrl()));
}
}
#Override
public int getItemViewType(int position) {
return viewModels.get(position).getType();
}
#Override
public int getItemCount() {
return viewModels.size();
}
private Optional<BaseOverviewItemVM> getViewModelOfType(int type){
if(viewModels != null ) {
for (BaseOverviewItemVM viewModel : viewModels) {
if(viewModel.getType() == type){
return Optional.of(viewModel);
}
}
}
return Optional.empty();
}
}
I think you will be able to handle it creating a listener and handle the call with it:
public interface clickListenerItem {
void onItemClick(Item element);
}
add this interface in your adapter and in your main.
Adding this function to your *nameclass*ViewHolder extends RecyclerView.ViewHolder :
public void bind(final ItemName item, final clickListenerItem listener) {
itemView.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
listener.onItemClick(item);
}
});
}
then add to your Adapter variables: private final clickListenerItem clickItem;
in your constructor: this.clickItem = clickItem;
and in onBindViewHolder of your adapter call:
public void onBindViewHolder(final #NonNull ItemViewHolder holder, int position) {
holder.bind(list.get(position), clickItem);
}
now when initiliazing your adapter in the main you just add your listener adding it with a call like this:
adapter = new ItemAdapter(this, list, (new ItemAdapter.clickListenerItem(){
#Override public void onItemClick(Item thisItem) {
Toast.makeText(getApplicationContext(), "Loading Item...", Toast.LENGTH_LONG).show();
Intent viewElem = new Intent(getApplicationContext(), ViewItem.class);
startActivityForResult(viewElem ,codeForResult);
}
}));
I hope this will help you, obviusly you can change the last part for not calling a new activity but changing it with what you want to see or do with your item.
I'm just one step to getting this dynamic layout work for me but I just cant figure out what I'm doing wrong.
I have the following string:
AAA__AB/
_AAAAAA/
_AAABBB/
I'm supposed to convert the above strings to a rooms layout whereby:
A - Represents available rooms
B - Represents booked rooms
_ - Represents a space
/ - Represents a new line
I have been following this tutorial Dynamic bus seats layout to try and make an app that can create Apartment rooms layout dynamically in RecyclerView.
I have a horizontally oriented recyclerview as the parent recyclerView. I have an adapter and a data source from a webserver.
Everything is working fine apart from the NEW_LINE feature which is supposed to force the recyclerview add a new line/row so that the rest of the views may align themselves in the new row/line
Please help
The following is what I get
The way my layout looks like as at now
The following is what I want
The intended layout
The Main Activity.java
...rest of the code
//Displaying the rooms and trying to make the layout
private void display_rooms_details(){
ArrayList house_rooms;
house_rooms=new ArrayList<>();
for (int i = 0; i < rooms_list.size(); i++)
{
if(rooms_list.get(i).getType()==0){
house_rooms.add(new Decode_rooms(Decode_rooms.AVAILABLE_ROOMS, rooms_list.get(i).getRoom_type(), rooms_list.get(i).getRoom_number(), rooms_list.get(i).getRoom_name(), rooms_list.get(i).getRoom_symbol(), rooms_list.get(i).getRoom_tenant()));
}
if(rooms_list.get(i).getType()==1){
house_rooms.add(new Decode_rooms(Decode_rooms.BOOKED_ROOMS, rooms_list.get(i).getRoom_type(), rooms_list.get(i).getRoom_number(), rooms_list.get(i).getRoom_name(), rooms_list.get(i).getRoom_symbol(), rooms_list.get(i).getRoom_tenant()));
}
if(rooms_list.get(i).getType()==2){
house_rooms.add(new Decode_rooms(Decode_rooms.NEW_LINE, rooms_list.get(i).getRoom_type(), rooms_list.get(i).getRoom_number(), rooms_list.get(i).getRoom_name(), rooms_list.get(i).getRoom_symbol(), rooms_list.get(i).getRoom_tenant()));
}
if(rooms_list.get(i).getType()==3){
house_rooms.add(new Decode_rooms(Decode_rooms.SPACE, rooms_list.get(i).getRoom_type(), rooms_list.get(i).getRoom_number(), rooms_list.get(i).getRoom_name(), rooms_list.get(i).getRoom_symbol(), rooms_list.get(i).getRoom_tenant()));
}
}
RecyclerView myrv= findViewById(R.id.recycleview_item_list);
Adapter_display_rooms myAdapter= new Adapter_display_rooms(this,house_rooms);
myrv.setLayoutManager(new LinearLayoutManager(this, OrientationHelper.HORIZONTAL,false));
myrv.setItemAnimator(new DefaultItemAnimator());
myrv.setAdapter(myAdapter);
}
Adapter_display_rooms
public class Adapter_display_rooms extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
//Declare context and the class with our list
private static Context mContext;
int total_types;
private static ArrayList<Decode_rooms> rooms_list; //Our data class... this one contains the get and set methods to decode the data for us
//Available rooms layout class
public static class ActualRoomsViewHolder extends RecyclerView.ViewHolder {
CardView cardview;
TextView txtType;
ImageView image;
public ActualRoomsViewHolder(View itemView) {
super(itemView);
this.cardview= itemView.findViewById(R.id.layout_available_rooms);
this.image = itemView.findViewById(R.id.room_icon);
this.txtType = itemView.findViewById(R.id.room_name);
}
}
//New Line layout class
public static class NewLineViewHolder extends RecyclerView.ViewHolder {
LinearLayout linearlayout;
public NewLineViewHolder(View itemView) {
super(itemView);
this.linearlayout = itemView.findViewById(R.id.layout_newline);
}
}
//New Line layout class
public static class AddSpaceViewHolder extends RecyclerView.ViewHolder {
TextView txtType;
public AddSpaceViewHolder(View itemView) {
super(itemView);
this.txtType = itemView.findViewById(R.id.add_space);
}
}
//END OF VIEWHOLDER CLASSES
//The Class Constructor
public Adapter_display_rooms(Context mContext, ArrayList<Decode_rooms> rooms_list) {
this.mContext = mContext;
this.rooms_list = rooms_list;
total_types=rooms_list.size();
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
switch (viewType) {
case Decode_rooms.AVAILABLE_ROOMS:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_rooms_layout_listview_rooms, parent, false);
return new ActualRoomsViewHolder(view);
case Decode_rooms.BOOKED_ROOMS:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_rooms_layout_listview_rooms, parent, false);
return new ActualRoomsViewHolder(view);
case Decode_rooms.NEW_LINE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_rooms_layout_listview_newline, parent, false);
return new NewLineViewHolder(view);
case Decode_rooms.SPACE:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_rooms_layout_listview_space, parent, false);
return new AddSpaceViewHolder(view);
}
return null;
}
#Override
public int getItemViewType(int position) {
switch (rooms_list.get(position).type) {
case 0:
return Decode_rooms.AVAILABLE_ROOMS;
case 1:
return Decode_rooms.BOOKED_ROOMS;
case 2:
return Decode_rooms.NEW_LINE;
case 3:
return Decode_rooms.SPACE;
default:
return -1;
}
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
final Decode_rooms object = rooms_list.get(position);
if (object != null) {
switch (object.type) {
case Decode_rooms.AVAILABLE_ROOMS:
((ActualRoomsViewHolder) holder).image.setImageResource(R.drawable.ic_seats_book);
((ActualRoomsViewHolder) holder).txtType.setText(object.getRoom_number());
((ActualRoomsViewHolder) holder).cardview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(mContext, Available_room_details.class);
intent.putExtra("room_number",object.getRoom_number());
intent.putExtra("room_name",object.getRoom_name());
intent.putExtra("room_type",object.getRoom_type());
mContext.startActivity(intent);
}
});
((ActualRoomsViewHolder) holder).cardview.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(mContext,v);
//Inflating the Popup using xml file
popup.getMenuInflater()
.inflate(R.menu.menu_available_room_options, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(
mContext,
"You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT
).show();
return true;
}
});
popup.show(); //showing popup menu
return false;
}
});
break;
case Decode_rooms.BOOKED_ROOMS:
((ActualRoomsViewHolder) holder).image.setImageResource(R.drawable.ic_seats_booked);
((ActualRoomsViewHolder) holder).txtType.setText(object.getRoom_number());
((ActualRoomsViewHolder) holder).cardview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(mContext, Booked_room_details.class);
intent.putExtra("room_number",object.getRoom_number());
intent.putExtra("room_name",object.getRoom_name());
intent.putExtra("room_type",object.getRoom_type());
mContext.startActivity(intent);
}
});
((ActualRoomsViewHolder) holder).cardview.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(mContext,v);
//Inflating the Popup using xml file
popup.getMenuInflater()
.inflate(R.menu.menu_booked_room_options, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(
mContext,
"You Clicked : " + item.getTitle(),
Toast.LENGTH_SHORT
).show();
return true;
}
});
popup.show(); //showing popup menu
return false;
}
});
break;
case Decode_rooms.NEW_LINE:
break;
case Decode_rooms.SPACE:
((AddSpaceViewHolder) holder).txtType.setText("Space "+object.getRoom_number());
break;
default:
((ActualRoomsViewHolder) holder).txtType.setText("Default "+object.getRoom_number());
break;
}
}
}
#Override
public int getItemCount() {
return rooms_list.size();
}
Decode rooms java
public class Decode_rooms {
//Aid the custom adapter in inflating different types of layouts
public static final int AVAILABLE_ROOMS = 0;
public static final int BOOKED_ROOMS = 1;
public static final int NEW_LINE = 2;
public static final int SPACE = 3;
String room_type,room_number,room_name,room_symbol,room_tenant;
public int type; //This variable will hold the type of data i.e THE ROOM SYMBOL .. It'll tell the adapter the layout to inflate
public Decode_rooms(int type,String room_type, String room_number, String room_name, String room_symbol, String room_tenant) {
this.type = type;
this.room_type = room_type;
this.room_number = room_number;
this.room_name = room_name;
this.room_symbol = room_symbol;
this.room_tenant = room_tenant;
}
//The setter methods
public void setType(int type) {
this.type = type;
}
public void setRoom_type(String room_type) {
this.room_type = room_type;
}
public void setRoom_number(String room_number) {
this.room_number = room_number;
}
public void setRoom_name(String room_name) {
this.room_name = room_name;
}
public void setRoom_symbol(String room_symbol) {
this.room_symbol = room_symbol;
}
public void setRoom_tenant(String room_tenant) {
this.room_tenant = room_tenant;
}
//The getter methods
public int getType() {
return type;
}
public String getRoom_type() {
return room_type;
}
public String getRoom_number() {
return room_number;
}
public String getRoom_name() {
return room_name;
}
public String getRoom_symbol() {
return room_symbol;
}
public String getRoom_tenant() {
return room_tenant;
}
}
Parent xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/recyclerview" />
</android.support.design.widget.CoordinatorLayout>
The recyclerView
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycleview_item_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:background="#color/blue"
android:elevation="2dp"
android:layout_margin="10dp"/>
</android.support.constraint.ConstraintLayout>
The Card view / Individual row item for rooms
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_available_rooms"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:layout_width="#dimen/room_width"
android:layout_height="#dimen/room_height"
android:layout_margin="#dimen/roomGaping"
android:orientation="horizontal"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/room_icon"
android:background="#color/black"
android:layout_width="match_parent"
android:layout_height="#dimen/room_width"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/room_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#2d2d2d"
android:textSize="13sp"
android:text="Room Name"/>
</LinearLayout>
</android.support.v7.widget.CardView>
The new line layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_newline"
android:layout_width="match_parent"
android:layout_height="#dimen/room_height"
android:background="#color/grey_background"
android:orientation="horizontal">
</LinearLayout>
What you want is GridLayoutManager with column count of maximum of length of all rows. (You can set it as lcm of length of all inputs)
Iterate through each input and set column span as required. Let me know if anything is not clear.
Edit: Adding sample code
TestActivity.java
public class TestActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
mRecyclerView = findViewById(R.id.recycler_view);
final String[] inputs = new String[3];
inputs[0] = "AAA__AB/";
inputs[1] = "_AAAAAA/";
inputs[2] = "_ABB/";
final StringBuilder sb = new StringBuilder();
for(int i =0; i < inputs.length; i++) {
sb.append(inputs[i]);
}
String[] modifiedInput = sb.toString().split("/");
final int lcm = getLcm(modifiedInput);
GridLayoutManager manager = new GridLayoutManager(getApplicationContext(), lcm);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
#Override
public int getSpanSize(int i) {
if(sb.toString().charAt(i) == '/') {
return lcm;
}
else {
int index = 0;
for(int j = 0; j< inputs.length; j++) {
if(i < index+inputs[j].length()) {
if(inputs[j].contains("/")) return lcm / (inputs[j].length()-1);
else return lcm / inputs[j].length();
}
else {
index += inputs[j].length();
}
}
throw new IllegalStateException("Wrong index");
}
}
});
List<String> inputDataList = new ArrayList<>();
String flatInputData = sb.toString();
for(int i = 0; i < flatInputData.length(); i++) {
inputDataList.add(String.valueOf(flatInputData.charAt(i)));
}
Adapter adapter = new Adapter(inputDataList);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(manager);
}
private int getLcm(String[] modifiedInputs) {
int lcm = modifiedInputs[0].length();
for(int i = 1; i< modifiedInputs.length; i++) {
lcm = lcm(lcm, modifiedInputs[i].length());
}
return lcm;
}
public static int lcm(int a, int b) {
return a/gcd(a,b) * b;
}
public static int gcd(int a, int b){
if (a<b) return gcd(b,a);
if (a%b==0) return b;
else return gcd(b, a%b);
}
}
Adapter.java
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
public List<String> data;
public Adapter(List<String> data) {
this.data = data;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(android.R.layout.simple_list_item_1, viewGroup, false));
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
String text = data.get(i);
if(!text.equals("/"))
viewHolder.textView.setText(text);
}
#Override
public int getItemCount() {
if(data == null) {
return 0;
}
else {
return data.size();
}
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(android.R.id.text1);
}
}
}
I am implementing sticky header with RecyclerView. I have three sections (i.e 3 sticky headers) and they are working fine.Now I have taken three arraylists inside each section, I have initialized these list in my adapter and I am trying to get data of these lists on basis of header id inside onBindViewHolder. But it is not giving me the full list,just one string from each list (i.e under first section--data on first position of mylist,,,under second section-- data on second position of mylist1 ---under third section-- data on third position of mylist2)
Please Help !!
Code in Context:
StickyTestAdapter
public class StickyTestAdapter extends RecyclerView.Adapter<StickyTestAdapter.ViewHolder> implements
StickyHeaderAdapter<StickyTestAdapter.HeaderHolder> {
private Context mContext;
private ArrayList<String> mylist;
private ArrayList<String> mylist1;
private ArrayList<String> mylist2;
private static int countposition;
private String HEADER_FIRIST="HEADER_FIRIST";
private String HEADER_SECOND="HEADER_SECOND";
private String HEADER_THIRD="HEADER_THIRD";
public StickyTestAdapter(Context context) {
prepareData();
prepareData1();
prepareData2();
this.mContext=context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
final View view = LayoutInflater.from(mContext).inflate(R.layout.item_test, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
long rowType= getHeaderId(position);
Log.e("getHeaderId----",""+rowType);
if (rowType==0){
if(!mylist.equals(""))
{
Log.e("list_data----", "" + mylist.get(position));
viewHolder.item.setText(mylist.get(position));
}
else
{
Log.e("Error--0--", "" + "error");
}
} else if (rowType==1){
if(!mylist1.equals(""))
{
Log.e("list_data1----", "" + mylist1.get(position));
viewHolder.item.setText(mylist1.get(position));
}
else
{
Log.e("Error---1-", "" + "error");
}
} else if (rowType==2){
if(!mylist2.equals(""))
{
Log.e("list_data2----", "" + mylist2.get(position));
viewHolder.item.setText(mylist2.get(position));
}
else
{
Log.e("Error----2", "" + "error");
}
}
}
#Override
public int getItemCount() {
if (getHeaderId(countposition)==0){
Log.e("mylist",""+mylist.size());
return mylist.size();
}else if (getHeaderId(countposition)==1){
Log.e("mylist1",""+mylist1.size());
return mylist1.size();
}else if (getHeaderId(countposition)==2){
Log.e("mylist2",""+mylist2.size());
return mylist2.size();
}
return 0;
}
#Override
public long getHeaderId(int pos) {
return pos;
}
#Override
public HeaderHolder onCreateHeaderViewHolder(ViewGroup parent) {
final View view = LayoutInflater.from(mContext).inflate(R.layout.header_test, parent, false);
return new HeaderHolder(view);
}
#Override
public void onBindHeaderViewHolder(HeaderHolder viewholder, int count) {
countposition=count;
if (getItemViewType(count)==0){
viewholder.headertext.setText(HEADER_FIRIST);
}else if (getItemViewType(count)==1){
viewholder.headertext.setText(HEADER_SECOND);
}else if (getItemViewType(count)==2){
viewholder.headertext.setText(HEADER_THIRD);
}
}
static class ViewHolder extends RecyclerView.ViewHolder {
public TextView item;
public ViewHolder(View itemView) {
super(itemView);
item = (TextView)itemView.findViewById(R.id.text_item);
}
}
static class HeaderHolder extends RecyclerView.ViewHolder {
public TextView headertext;
public HeaderHolder(View itemView) {
super(itemView);
headertext = (TextView)itemView.findViewById(R.id.header_text);
}
}
#Override
public int getItemViewType(int position) {
return position;
}
public void prepareData()
{
mylist=new ArrayList<>();
mylist.add("rajendra");
mylist.add("rani");
mylist.add("rahul");
}
public void prepareData1()
{
mylist1=new ArrayList<>();
mylist1.add("ravi");
mylist1.add("vikram");
mylist1.add("rakesh");
}
public void prepareData2()
{
mylist2=new ArrayList<>();
mylist2.add("apple");
mylist2.add("ashok");
mylist2.add("vikash");
}
}
Question is quite old. But that code looks complicated to read, personally I try to not reinvent what others did quite well by creating libraries.
GitHub is full of source that you can import. Some examples: FlexibleAdapter, FastAdapter, Epoxy and others.
I have build the first one, but you can try the others as well.
With mine, having multiple views and headers with sections is quite easy, I point here the wiki page where I define the section and how to initialize it.
Along with this feature, you have a lot more functionalities that makes your life easier.
I divide arraylist into 3 section based on alphabet like contactlist.
For that i use SectionedRecyclerViewAdapter
MainActivity.java
public class MainActivity extends AppCompactActivity {
private SectionedRecyclerViewAdapter sectionAdapter;
#Override
public View onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
sectionAdapter = new SectionedRecyclerViewAdapter();
for(char alphabet = 'a'; alphabet <= 'z';alphabet++) {
List<String> contacts = getContactsWithLetter(alphabet);
if (contacts.size() > 0) {
sectionAdapter.addSection(new ContactsSection(String.valueOf(alphabet), contacts));
}
}
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
return view;
}
#Override
public void onResume() {
super.onResume();
if (getActivity() instanceof AppCompatActivity) {
AppCompatActivity activity = ((AppCompatActivity) getActivity());
if (activity.getSupportActionBar() != null)
activity.getSupportActionBar().setTitle(R.string.nav_example1);
}
}
private List<String> getContactsWithLetter(char letter) {
List<String> contacts = new ArrayList<>();
for (String contact : getResources().getStringArray(R.array.names_)) {
if (contact.charAt(0) == letter) {
contacts.add(contact);
}
}
return contacts;
}
private class ContactsSection extends StatelessSection {
String title;
List<String> list;
ContactsSection(String title, List<String> list) {
super(new SectionParameters.Builder(R.layout.section_ex1_item)
.headerResourceId(R.layout.section_ex1_header)
.build());
this.title = title;
this.list = list;
}
#Override
public int getContentItemsTotal() {
return list.size();
}
#Override
public RecyclerView.ViewHolder getItemViewHolder(View view) {
return new ItemViewHolder(view);
}
#Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
final ItemViewHolder itemHolder = (ItemViewHolder) holder;
String name = list.get(position);
itemHolder.tvItem.setText(name);
itemHolder.imgItem.setImageResource(name.hashCode() % 2 == 0 ? R.drawable.ic_face_black_48dp : R.drawable.ic_tag_faces_black_48dp);
itemHolder.rootView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), String.format("Clicked on position #%s of Section %s", sectionAdapter.getPositionInSection(itemHolder.getAdapterPosition()), title), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
return new HeaderViewHolder(view);
}
#Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
headerHolder.tvTitle.setText(title);
}
}
private class HeaderViewHolder extends RecyclerView.ViewHolder {
private final TextView tvTitle;
HeaderViewHolder(View view) {
super(view);
tvTitle = (TextView) view.findViewById(R.id.tvTitle);
}
}
private class ItemViewHolder extends RecyclerView.ViewHolder {
private final View rootView;
private final ImageView imgItem;
private final TextView tvItem;
ItemViewHolder(View view) {
super(view);
rootView = view;
imgItem = (ImageView) view.findViewById(R.id.imgItem);
tvItem = (TextView) view.findViewById(R.id.tvItem);
}
}
}
use structure similar to
public class MultiArray<T> {
List<ItemGroup> lists = new ArrayList<>();
public void addList(String headerText, List<T> list) {
lists.add(new ItemGroup(headerText, list));
}
public int itemCount() {
int count = 0;
for (ItemGroup group : lists) {
count += group.count();
}
return count;
}
public T getItem(int position) {
int count = 0;
for (ItemGroup group : lists) {
if (count + group.count() >= position) {
return group.item(position - count);
}
count += group.count();
}
return null;
}
public int getGroupIndex(int position) {
int count = 0;
int groupIndex = 0;
for (ItemGroup group : lists) {
if (count + group.count() >= position) {
return groupIndex;
}
count += group.count();
groupIndex++;
}
return -1;
}
public String getHeaderText(int position){
int count = 0;
for (ItemGroup group : lists) {
if (count + group.count() >= position) {
return group.headerText;
}
count += group.count();
}
return "";
}
class ItemGroup {
public final String headerText;
public final List<T> list;
public ItemGroup(String headerText, List<T> list) {
this.headerText = headerText;
this.list = list;
}
public int count() {
return list.size();
}
public T item(int position) {
return list.get(position);
}
}
}
you can optimize it for faster performance
This question already has answers here:
How to create RecyclerView with multiple view types
(23 answers)
Closed 6 years ago.
I am getting data in jSON. Data contains images url. I am trying to display images in below layouts.
1Image
2Image
3Image 4Image 5Image 6Image 7image
8Image 9Image 10Image 11Image 12image
13image 14image 15image 16image 17image
.... .... ..... ...... .....
So First two image will be in vertically. Other 5 images in horizontal scroll view and so on.
So i am using RecyclerView with vertical orientation. but i am not able to creating the logic so images will be display above format.
please anyone guide me.
update:-
//Horizontal images view.
public class MoviesAdapterHorizontal extends RecyclerView.Adapter<MoviesAdapterHorizontal.MyViewHolder> {
private List<String> moviesList;
// private ThumbnailListener thumbnailListener;
public class MyViewHolder extends RecyclerView.ViewHolder {
// YouTubeThumbnailView thumbnail;
ImageView imageView2;
public MyViewHolder(View view) {
super(view);
// thumbnail = (YouTubeThumbnailView) view.findViewById(R.id.thumbnail);
imageView2 = (ImageView) view.findViewById(R.id.imageView2);
}
}
public MoviesAdapterHorizontal(List<String> moviesList) {
this.moviesList = moviesList;
// thumbnailListener = new ThumbnailListener();
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.hotrow2, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final String url = moviesList.get(position);
System.out.println("movie url = " + url);
final String m = url.substring(url.lastIndexOf('/') + 1);
System.out.println("url = "+m);
int w = BasicDeviceInfo.getWidth(getActivity());
// int h = BasicDeviceInfo.getHeight(getActivity());
LinearLayout.LayoutParams lParams =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
w/3);
AQuery aq = new AQuery(holder.imageView2);
aq.id(holder.imageView2).image("http://x.x.x.231/news-flicks/uploads/home/tulips_e84aa48cee1a2a25aef001cd74152663.jpg", true, true, 0,
R.drawable.no_thumbnail);
holder.imageView2.setLayoutParams(lParams);
// holder.thumbnail.setLayoutParams(lParams);
/* holder.thumbnail.setTag(m);
holder.thumbnail.initialize("AIzaSyDW-sxPUqy2rD6ZWs3vTNb0jKEKA21RjrY", thumbnailListener);
holder.thumbnail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), FullscreenDemoActivity.class);
intent.putExtra("url",m);
intent.putExtra("txt", "");
startActivity(intent);
}
});*/
}
#Override
public int getItemCount() {
return moviesList.size();
}
private final class ThumbnailListener implements
YouTubeThumbnailView.OnInitializedListener,
YouTubeThumbnailLoader.OnThumbnailLoadedListener {
#Override
public void onInitializationSuccess(
YouTubeThumbnailView view, YouTubeThumbnailLoader loader) {
loader.setOnThumbnailLoadedListener(this);
String videoId = (String) view.getTag();
loader.setVideo(videoId);
}
#Override
public void onInitializationFailure(
YouTubeThumbnailView view, YouTubeInitializationResult loader) {
view.setImageResource(R.drawable.no_thumbnail);
}
#Override
public void onThumbnailLoaded(YouTubeThumbnailView view, String videoId) {
}
#Override
public void onThumbnailError(YouTubeThumbnailView view, YouTubeThumbnailLoader.ErrorReason errorReason) {
view.setImageResource(R.drawable.no_thumbnail);
}
}
}
horizontal xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#drawable/no_thumbnail"/>
</LinearLayout>
i am calling it parent recycle view onBindViewHolder()
#Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
Videos movie = moviesList.get(position);
System.out.println("Image = " + movie.getImage());
int w = BasicDeviceInfo.getWidth(getActivity());
// int h = BasicDeviceInfo.getHeight(getActivity());
if(position < 2) {
final List<String> lst = new ArrayList<String>();
lst.add(movie.getVideo_url());
lst.add(movie.getVideo_url());
lst.add(movie.getVideo_url());
lst.add(movie.getVideo_url());
lst.add(movie.getVideo_url());
final MoviesAdapterHorizontal adapter = new MoviesAdapterHorizontal(lst);
holder.recyclerView2.setAdapter(adapter);
adapter.notifyDataSetChanged();
Now my horizontal onBindViewHolder is calling and able to see log messages. But ImageView is not displaying. i think it is hiding or overlapping
See onto this Answer for how to make different layout item in RecyclerView
For first two element layout file containing ImageView
For rest item view I will suggest take Horizontal RecyclerView as item
My Custom view for Horizontal RecyclerView
public class DocumentViewDynamic extends LinearLayout {
private static final String LOG_TAG = DocumentViewDynamic.class.getSimpleName();
private final String titleStr;
private OnDocumentItemClicked callBack;
public void updateImage(Bitmap bitmap, int postionOfView) {
Log.e(LOG_TAG, "updateImage: ");
documentAdapter.setImageFromCamera(bitmap, postionOfView);
}
public interface OnDocumentItemClicked {
void onDocumentItemClicked(View v, int position);
}
public void setItemClickedListener(OnDocumentItemClicked callBack) {
this.callBack = callBack;
}
#Bind(R.id.title)
TextView title;
#Bind(R.id.img_add)
FloatingActionButton imgAdd;
private DocumentAdapter documentAdapter;
private final Context context;
#Bind(R.id.rv_document)
RecyclerView rvDocument;
#OnClick(R.id.img_add)
public void addDummyImage() {
documentAdapter.addSingelDummyImage();
rvDocument.scrollToPosition(documentAdapter.getItemCount() - 1);
}
public DocumentViewDynamic(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.DocumentViewDynamic, 0, 0);
try {
titleStr = ta.getString(R.styleable.DocumentViewDynamic_titleDoc);
} finally {
ta.recycle();
}
init();
}
public ArrayList<DocumentModel> getAllImageData() {
return documentAdapter.getImageData();
}
private void init() {
setOrientation(VERTICAL);
View view = LayoutInflater.from(context).inflate(R.layout.compund_view_document, this);
ButterKnife.bind(this, view);
if (!TextUtils.isEmpty(titleStr))
title.setText(titleStr);
initRecyclerView();
}
private void initRecyclerView() {
rvDocument.setLayoutManager(new LinearLayoutManager(context, HORIZONTAL, false));
ArrayList<DocumentModel> documentModels = new ArrayList<>();
documentAdapter = new DocumentAdapter(documentModels);
rvDocument.setAdapter(documentAdapter);
}
public class DocumentAdapter extends RecyclerView.Adapter<DocumentAdapter.DocViewHolder> {
public ArrayList<DocumentModel> getImageData() {
return documentModels;
}
private ArrayList<DocumentModel> documentModels = new ArrayList<>();
public DocumentAdapter(ArrayList<DocumentModel> documentModels) {
this.documentModels = documentModels;
if (documentModels.size() == 0)
documentModels.add(new DocumentModel());
}
#Override
public DocViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_compund_document, parent, false);
return new DocViewHolder(view);
}
#Override
public void onBindViewHolder(final DocViewHolder holder, int position) {
if (documentModels.get(position).getBitmap() == null) {
holder.imgDocument.setImageResource(R.drawable.camera);
holder.imgBtnDelete.setVisibility(GONE);
} else {
holder.imgDocument.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
holder.imgDocument.setScaleType(ImageView.ScaleType.FIT_XY);
holder.imgDocument.setImageBitmap(documentModels.get(position).getBitmap());
holder.imgBtnDelete.setVisibility(VISIBLE);
}
holder.itemView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
callBack.onDocumentItemClicked(DocumentViewDynamic.this, holder.getAdapterPosition());
}
});
holder.imgBtnDelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (holder.getAdapterPosition() == 0 && documentModels.size() == 1) {
documentModels.get(holder.getAdapterPosition()).setBitmap(null);
} else {
documentModels.remove(holder.getAdapterPosition());
}
notifyDataSetChanged();
}
});
}
#Override
public int getItemCount() {
return (documentModels != null && documentModels.size() > 0) ? documentModels.size() : 0;
}
public void setImageFromCamera(Bitmap imageFromCamera, int postionOfView) {
documentModels.get(postionOfView).setBitmap(imageFromCamera);
notifyDataSetChanged();
}
public class DocViewHolder extends RecyclerView.ViewHolder {
#Bind(R.id.img_document)
ImageView imgDocument;
#Bind(R.id.img_btn_delete)
ImageView imgBtnDelete;
public DocViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
public void addSingelDummyImage() {
if (documentModels.get(documentModels.size() - 1).getBitmap() != null) {
documentModels.add(new DocumentModel());
notifyDataSetChanged();
} else {
Snackbar.make(rvDocument, "Please Add image ", Snackbar.LENGTH_LONG).show();
}
}
}
}
layout file for compound view
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textColor="#3399ff"
android:textSize="15dp"
android:textStyle="normal" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_document"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/img_add" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/img_add"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:src="#drawable/fab_plus_bg" />
</RelativeLayout>
Inflate this compound view as item in Vertical RecyclerView