Hello I am developing an app which uses recyclerview to show items to select categories. I have added textview and checkbox on recyclerview item. The problem is if I select one checkbox, it also selects multiple checkbox in the list. For example if I select 1st checkbox in the list it automatically selects every 10th checkbox in the list. Thanks
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="5dp"
android:background="#FFF"
app:cardUseCompatPadding="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/categoryName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="14dp"
android:text="Category"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="#+id/categoryCount"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/categoryCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:text="Count"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<CheckBox
android:id="#+id/categoryCheck"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="50dp"
android:layout_marginBottom="36dp"
android:buttonTint="#color/red"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Adapter.java
public class CategorySelector extends RecyclerView.Adapter<CategorySelector.ViewHolder> {
LayoutInflater inflater;
List<Category> categoryList;
List<String> selectedItems = new ArrayList<>();
public CategorySelector(Context ctx, List<Category> categoryList){
inflater = LayoutInflater.from(ctx);
this.categoryList = categoryList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.category_select, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.categoryName.setText(categoryList.get(position).getCategoryName());
holder.categoryCount.setText(String.valueOf(categoryList.get(position).getCategoryCount()));
holder.categoryCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
selectedItems.add(categoryList.get(position).getCategoryName());
} else{
selectedItems.remove(categoryList.get(position).getCategoryName());
}
}
});
}
#Override
public int getItemCount() {
return categoryList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView categoryName;
TextView categoryCount;
CheckBox categoryCheck;
public ViewHolder(#NonNull View itemView) {
super(itemView);
categoryName = itemView.findViewById(R.id.categoryName);
categoryCount = itemView.findViewById(R.id.categoryCount);
categoryCheck = itemView.findViewById(R.id.categoryCheck);
}
}
public List<String> getSelectedItems(){
return selectedItems;
}
}
In your checkBox changeListener use instead of using position use :
holder.getAdapterPosition()
then set your checkBox select with your item :
holder.categoryCheck.setChecked(//set your boolean is selected before);
Related
PLEASE READ THE EDIT SECTION BELLOW
I have a Spinners Recyclerview (list) I want to iterate after clicking a button(finishBtn).
in order to get the selected results entered by the user, I try to iterate the list using list.findViewHolderForAdapterPosition.
The size of the list is supposed to be 6 (by viewing Recyclerview I can see all 6) but when I try to iterate, the size is smaller than what actually there is. (instead of 6 the list.getChildCount() is only 3).
Fragment:
public class FragmentUserDetails extends Fragment {
Button finishBtn;
LinkedHashMap<String, ArrayList<String>> detailsQuestions;
DetailsViewModel detailsViewModel;
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
detailsViewModel = new ViewModelProvider(this).get(DetailsViewModel.class);
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_user_details, container, false);
detailsQuestions =getQuestions();
Log.d("TAG", "size: "+detailsQuestions.size());
RecyclerView list = view.findViewById(R.id.details_list_rv);
list.setHasFixedSize(true);
list.setLayoutManager(new LinearLayoutManager(getContext()));
DetailsAdapter detailsAdapter = new DetailsAdapter(this);
list.setAdapter(detailsAdapter);
finishBtn = view.findViewById(R.id.userDetails_next_btn);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
finishBtn.setOnClickListener(v -> {
Log.d("TAG", ""+list.getChildCount());
for (int i = 0; i < list.getChildCount(); i++) {
DetailsHolder holder = (DetailsHolder) list.findViewHolderForAdapterPosition(i);
if (holder!=null) {
String question = (String) detailsQuestions.keySet().toArray()[i];
Log.d("TAG", "The answer for " +question+"is :"+ holder.questionSp.getSelectedItem().toString());
}
}
Intent intent = new Intent(getContext(), MainActivity.class);
startActivity(intent);
getActivity().finish();
});
return view;
}
interface OnItemClickListener {
void onItemClick(int position);
}
public LinkedHashMap<String, ArrayList<String>> getQuestions() {
ArrayList<Detail> arrayList = detailsViewModel.getDetails();
detailsQuestions = new LinkedHashMap<>();
for (Detail detail : arrayList) {
detailsQuestions.put(detail.getQuestion(), detail.getAnswers());
}
return detailsQuestions;
}
}
Adapter:
class DetailsAdapter extends RecyclerView.Adapter<DetailsHolder> {
private final FragmentUserDetails fragmentUserDetails;
FragmentUserDetails.OnItemClickListener listener;
public DetailsAdapter(FragmentUserDetails fragmentUserDetails) {
this.fragmentUserDetails = fragmentUserDetails;
}
public void setOnItemClickListener(FragmentUserDetails.OnItemClickListener listener) {
this.listener = listener;
}
#NonNull
#Override
public DetailsHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = fragmentUserDetails.getLayoutInflater().inflate(R.layout.details_list_row, parent, false);
DetailsHolder detailsHolder = new DetailsHolder(view, listener);
return detailsHolder;
}
#Override
public void onBindViewHolder(#NonNull DetailsHolder detailsHolder, int position) {
if(fragmentUserDetails.detailsQuestions.size()!=0) {
String detail = (String) fragmentUserDetails.detailsQuestions.keySet().toArray()[position];
fragmentUserDetails.detailsQuestions.get(detail).add(0,detail);
String[] array = fragmentUserDetails.detailsQuestions.get(detail).toArray(new String[0]);
ArrayAdapter adapter = new ArrayAdapter<String>(fragmentUserDetails.getContext(), R.layout.spinner_item, array){
#Override
public boolean isEnabled(int position) {
if (position == 0) {
return false;
} else {
return true;
}
}
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
TextView tv = (TextView) view;
if (position == 0) {
// Set the hint text color gray
tv.setTextColor(Color.GRAY);
} else {
tv.setTextColor(Color.WHITE);
}
return view;
}};
detailsHolder.questionSp.setAdapter(adapter);
adapter.setDropDownViewResource(R.layout.spinner_item);
}
}
#Override
public int getItemCount() {
return fragmentUserDetails.detailsQuestions.size();
}
}
Holder:
class DetailsHolder extends RecyclerView.ViewHolder {
Spinner questionSp;
public DetailsHolder(#NonNull View itemView, FragmentUserDetails.OnItemClickListener listener) {
super(itemView);
questionSp = itemView.findViewById(R.id.listrow_question_sp);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = getAdapterPosition();
listener.onItemClick(pos);
}
});
}
}
details_list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true">
<Spinner
android:id="#+id/listrow_question_sp"
android:layout_width="409dp"
android:layout_height="90dp"
android:layout_weight="1"
android:clickable="true"
android:drawablePadding="14dp"
android:focusable="true"
android:maxLines="1"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:spinnerMode="dialog"
android:textSize="18sp"
android:theme="#style/mySpinnerItemStyle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_user_details.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="#color/black"
tools:context=".details.FragmentUserDetails">
<ImageView
android:id="#+id/userDetails_img_main"
android:layout_width="306dp"
android:layout_height="160dp"
android:layout_marginTop="30dp"
android:src="#drawable/logo__nobg_2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/homeScr_text_title"
android:layout_width="350dp"
android:layout_height="90dp"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif-light"
android:gravity="center"
android:text="Please enter the following details:"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/userDetails_img_main" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/details_list_rv"
android:layout_width="402dp"
android:layout_height="211dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/homeScr_text_title" />
<com.google.android.material.button.MaterialButton
android:id="#+id/userDetails_next_btn"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginTop="3dp"
android:fontFamily="sans-serif-black"
android:text="NEXT"
android:textSize="20sp"
app:cornerRadius="35dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/details_ti" />
<com.google.android.material.button.MaterialButton
android:id="#+id/userDetails_btn_back"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="88dp"
android:layout_height="50dp"
android:layout_marginTop="3dp"
android:backgroundTint="#color/black"
android:fontFamily="sans-serif"
android:text="BACK"
android:textSize="20sp"
app:cornerRadius="35dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/details_ti"
app:strokeColor="#color/primeOrng"
app:strokeWidth="1dp" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/details_ti"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif-light"
android:hint="map.."
android:textColorHint="#color/white"
app:layout_constraintTop_toBottomOf="#+id/details_list_rv"
tools:layout_editor_absoluteX="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/listrow_question_et"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/custom_input"
android:drawablePadding="14dp"
android:fontFamily="sans-serif-light"
android:inputType="text"
android:maxLines="1"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/details_ti"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:layout_editor_absoluteX="0dp" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
How can I solve this?
EDIT:
By replacing :
list.getChildCount();
with:
list.getAdapter().getItemCount()
I get the exact size (6) and now I understand it is related to the fact RecycleView recycles the views and not keeping them all, so it will always be a maximum of 3 items(in my example).
now, my question is still how could I achieve my target of saving the selected answers in the spinners by clicking finishBtn(I understand that iterating them is not an option because of the same thing, or maybe there is a way?)
I've made an adapter with a layout like the one I share below. it went well but, the appearance of each item became untidy.
Here is my code :
public class TableRecyclerAdapter extends RecyclerView.Adapter<TableRecyclerAdapter.ViewHolder> {
private ArrayList<TableModel> data;
private Context context;
private Fragment fragment;
private Dialog dialog;
public TableRecyclerAdapter(Context context, ArrayList<TableModel> data, Fragment fragment, Dialog dialog) {
this.context = context;
this.data = data;
this.fragment = fragment;
this.dialog = dialog;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.item_table, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.tvTable.setText(data.get(position).getName());
holder.tvState.setText(data.get(position).getStateString());
holder.bg.setBackgroundColor(Color.parseColor(data.get(position).getWarna()));
holder.tvHtml.setText(Html.fromHtml(data.get(position).getShortInfoHtml()));
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
public int getItemCount() {
return data.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private ImageView paid;
private TextView tvTable, tvHtml, tvState;
private ConstraintLayout bg;
public ViewHolder(View itemView) {
super(itemView);
tvTable = itemView.findViewById(R.id.tvTable);
tvHtml = itemView.findViewById(R.id.tvHtml);
bg = itemView.findViewById(R.id.bg);
tvState = itemView.findViewById(R.id.tvState);
paid = itemView.findViewById(R.id.ivPaid);
}
}
}
This is for layout item
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:cardCornerRadius="10dp"
app:cardElevation="3dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/bg"
android:layout_width="match_parent"
android:background="#drawable/border_button_green"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginStart="12dp"
android:background="#drawable/border_transparent_table"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/tvState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="This text"
android:textColor="#color/color_primary"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="#+id/tvTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="25dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:text="7"
android:textColor="#color/color_primary"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvHtml"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:paddingBottom="12dp"
android:text="Example Text"
android:textColor="#color/color_primary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tvTable" />
<ImageView
android:id="#+id/ivPaid"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="4dp"
android:src="#drawable/paid"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
It makes my recyclerview have different heights depending on the text.
I want the items to be the same height per row (maximum height in the row)
Sorry for my english. I hope someone can help me. Thank you
I am making an android chat app. I have a recyclerview to show messages. i have two different layouts for messages, one for sent and one for received. my recyclerview doesnt throw any error but is doesnt show anything on the screen.
This is my adapter class :
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<Chat> items;
private final int USER = 1, CONTACT = 2;
// Provide a suitable constructor (depends on the kind of dataset)
public ChatAdapter(List<Chat> items) {
this.items = items;
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return this.items.size();
}
#Override
public int getItemViewType(int position) {
if(items.get(position).getType()==1)
return USER;
else
return CONTACT;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
switch (viewType){
case USER:
View v1 = inflater.inflate(R.layout.chat_item_user, viewGroup, false);
viewHolder = new ViewHolder1(v1);
break;
default:
View v2 = inflater.inflate(R.layout.chat_item_contact, viewGroup, false);
viewHolder = new ViewHolder1(v2);
}
return viewHolder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
switch (viewHolder.getItemViewType()) {
case USER:
ViewHolder1 vh1 = (ViewHolder1) viewHolder;
configureViewHolder1(vh1, position);
break;
default:
ViewHolder2 vh2 = (ViewHolder2) viewHolder;
configureViewHolder2(vh2, position);
break;
}
}
public void configureViewHolder1(ViewHolder1 viewHolder, int position){
Chat chat = items.get(position);
viewHolder.getTv().setText(chat.getMsg());
}
public void configureViewHolder2(ViewHolder2 viewHolder, int position){
Chat chat = items.get(position);
viewHolder.getTv().setText(chat.getMsg());
}
}
This is my Chat class:
public class Chat {
private int type;
private String msg;
private long time;
public Chat(int type,long time,String msg){this.type = type; this.msg = msg; this.time = time;}
public int getType(){return type;}
public void setType(int type){this.type = type;}
public String getMsg(){return msg;}
public void setMsg(String msg){this.msg = msg;}
public long getTime(){return time;}
public void setTime(long time){this.time = time;}
}
This my layout containing the recylcerview :
<?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"
tools:context=".ChatActivity">
<TextView
android:id="#+id/textView3"
android:layout_width="368dp"
android:layout_height="59dp"
android:layout_marginEnd="9dp"
android:layout_marginStart="7dp"
android:layout_marginTop="16dp"
android:background="#android:color/holo_green_light"
android:text="TextView"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="368dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_height="394dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<EditText
android:id="#+id/editText8"
android:layout_width="325dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginStart="8dp"
android:layout_marginTop="1dp"
android:ems="10"
android:inputType="textMultiLine"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/imageButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/recyclerView" />
<ImageButton
android:id="#+id/imageButton"
android:layout_width="53dp"
android:layout_height="54dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/recyclerView"
app:srcCompat="#drawable/logo_arrow" />
These are my two layouts:
a. chat_item_contact
<?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="wrap_content">
<TextView
android:id="#+id/textView5"
android:layout_width="323dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="45dp"
android:layout_marginTop="16dp"
android:background="#android:color/holo_green_light"
android:paddingLeft="10dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
b. chat_item_user
<?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="wrap_content">
<TextView
android:id="#+id/textView4"
android:layout_width="329dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginEnd="48dp"
android:layout_marginStart="7dp"
android:layout_marginTop="7dp"
android:background="#android:color/holo_green_light"
android:paddingLeft="10dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I've done something similar, you should focus mostly on getItemViewType
Here is how I do (also I suggest you to use RecyclerView instead of ListView)
#Override
public int getItemViewType(int position){
if (/*your condition based on message received or sent*/) {
return VIEW_MESSAGE_RECEIVED;
}
else {
return VIEW_MESSAGE_SENT;
}
}
After, you just check what getItemViewType has returned
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_MESSAGE_RECEIVED) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.chat_message_item_received, parent, false);
return new ChatAdapter.MyViewHolder(view);
}else {
View eventDataView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.chat_message_item_sent, parent, false);
return new ChatAdapter.MyViewHolder(view);
}
}
Try this code..
remove listitem null check because once value is assign after that it not getting null.
add also add one more things if one layout is show than other is gone at time two layout not show to good..
if (currentChat.getType() == 1) {
listItem1 = LayoutInflater.from(context).inflate(R.layout.chat_item_user, parent, false);
listItem1.setVisibility(View.VISIBLE);
listItem2.setVisibility(View.GONE);
} else {
listItem2 = LayoutInflater.from(context).inflate(R.layout.chat_item_contact, parent, false);
listItem2.setVisibility(View.VISIBLE);
listItem1.setVisibility(View.GONE);
}
I have a Fragment Navigation Drawer with RecyclerView and some other view in it. Whenever I put my finger on a item on the recyclerview it doesn't show the selected state color. Although I've put every code needed.
RecyclerView Item Row:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingEnd="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingStart="10dp"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"
android:id="#+id/layout"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="26dp"
android:layout_height="26dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_gravity="center_vertical"
android:id="#+id/icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:textSize="16sp"
android:layout_gravity="center_vertical"
android:id="#+id/title"/>
</LinearLayout>
RecyclerView Adapter:
List<NavDrawerItems> data = Collections.emptyList();
private LayoutInflater inflater;
private Context context;
int selectedPosition = 0;
public NavigationDrawerAdapter(Context context, List<NavDrawerItems> data) {
this.context = context;
inflater = LayoutInflater.from(context);
this.data = data;
}
public void delete(int position) {
data.remove(position);
notifyItemRemoved(position);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.nav_drawer_row, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
if(selectedPosition == position){
holder.layout.setBackgroundColor(context.getResources().getColor(R.color.grey_efef));
holder.title.setTextColor(context.getResources().getColor(R.color.dark_red));
holder.icon.setColorFilter(context.getResources().getColor(R.color.dark_red));
} else {
holder.layout.setBackgroundColor(context.getResources().getColor(R.color.white));
holder.title.setTextColor(context.getResources().getColor(R.color.black));
holder.icon.setColorFilter(context.getResources().getColor(R.color.black));
}
NavDrawerItems current = data.get(position);
holder.title.setText(current.getTitle());
holder.icon.setImageResource(current.getIcon());
}
#Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView title;
ImageView icon;
LinearLayout layout;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
icon = (ImageView) itemView.findViewById(R.id.icon);
layout = (LinearLayout) itemView.findViewById(R.id.layout);
}
}
If I remove this code from the RecyclerView adapter, it works fine :
if(selectedPosition == position){
holder.layout.setBackgroundColor(context.getResources().getColor(R.color.grey_efef));
holder.title.setTextColor(context.getResources().getColor(R.color.dark_red));
holder.icon.setColorFilter(context.getResources().getColor(R.color.dark_red));
} else {
holder.layout.setBackgroundColor(context.getResources().getColor(R.color.white));
holder.title.setTextColor(context.getResources().getColor(R.color.black));
holder.icon.setColorFilter(context.getResources().getColor(R.color.black));
}
But this code is used to show the current selected item in recyclerview. How can I implement both of them.
I solved this issue by modifying the recyclerview item's code.
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/layout"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingEnd="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingStart="10dp"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_gravity="center_vertical"
android:id="#+id/icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:textSize="16sp"
android:layout_gravity="center_vertical"
android:id="#+id/title"/>
</LinearLayout>
</LinearLayout>
It seems, I was changing the background color of the Layout who was set with background :
android:background="?android:attr/selectableItemBackground"
So I added a new Layout and transfered the content inside it.
I am getting a Json output from the server.
This is my Json look like
{
"feed": [
{
"order_no": "70000004",
"quotation_no": "abc004a"
},
{
"order_no": "70000003",
"quotation_no": "abc003a"
},
{
"order_no": "70000001",
"quotation_no": "abc001a"
},
{
"order_no": "70000001",
"quotation_no": "abc001b"
}
]
}
I know how to populate this in a recyclerview. But what I want is grouping this json using order_no and populate. For example there are two 70000001. So i only need to show one 70000001 order no.
This is my custom row .xml file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#color/white">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginTop="2dp"
android:background="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dbdbda"
android:orientation="vertical">
<TextView
android:id="#+id/parent_list_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:text="Parent Title"
android:textColor="#color/primaryColorText"
android:textStyle="bold" />
<TextView
android:id="#+id/parent_q_item_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="2dp"
android:text="Q Title"
android:textColor="#color/secondaryColorText"
android:textSize="12sp" />
<TextView
android:id="#+id/parent_q_item_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="2dp"
android:text="Q Title"
android:textColor="#color/secondaryColorText"
android:textSize="12sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
After grouping, I need to show only one order_no in parent_list_item (70000001) and under that I need to show one quotation_no in the first text view and second quotation_no in the second text view under same order_no
My adapter class
public class OrderQuatationHistory extends RecyclerView.Adapter<OrderQuatationHistory.ItemViewHolder> {
private LayoutInflater layoutInflater;
private ArrayList<OrderHistoryData> orderHistoryDataArrayList;
public OrderQuatationHistory() {
}
public OrderQuatationHistory(Context context) {
orderHistoryDataArrayList = new ArrayList<>();
layoutInflater = LayoutInflater.from(context);
}
public void setOrderHistoryList(ArrayList<OrderHistoryData> orderHistoryDataArrayList) {
this.orderHistoryDataArrayList = orderHistoryDataArrayList;
notifyItemRangeChanged(0, orderHistoryDataArrayList.size());
}
#Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.custom_order_history_p_row, parent, false);
ItemViewHolder itemViewHolder = new ItemViewHolder(view);
return itemViewHolder;
}
#Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
OrderHistoryData orderHistoryData = orderHistoryDataArrayList.get(position);
holder.orderNo.setText(orderHistoryData.getOrderNo());
holder.quotationNo.setText(orderHistoryData.getQuotationNo());
}
#Override
public int getItemCount() {
return orderHistoryDataArrayList.size();
}
public static class ItemViewHolder extends RecyclerView.ViewHolder {
private TextView orderNo,quotationNo;
public ItemViewHolder(View itemView) {
super(itemView);
orderNo = (TextView) itemView.findViewById(R.id.parent_list_item);
quotationNo = (TextView) itemView.findViewById(R.id.parent_q_list);
}
}
}
Try this... For avoiding of duplicate value,you can use this Method.
ArrayList<Integer> jSonOrder=new ArrayList();
int order_no= assigned values that you have retrieved from JSon.
if (!jSonOrder.contains(order_no)) {
jSonOrder.add(order_no);
}