I have a recyclerview . I click on a item. It is highlighted ( sets background - color) . And then, I click another item , It doesn't remove old highlighted view. My class is very leak . Also could you give me an advice to make more effective ?
public class DrawerMenuShelfListAdapter extends RecyclerView.Adapter<DrawerMenuShelfListAdapter.ViewHolder> implements View.OnClickListener {
private int selectedRow = 0;
public Fragment fragment;
public ViewHolder holder;
public RecyclerView recyclerView;
private ArrayList<ShelfModel> shelfList;
public DrawerMenuShelfListAdapter(Fragment fragment, ArrayList<ShelfModel> shelfList, RecyclerView recyclerView) {
this.shelfList = shelfList;
this.fragment = fragment;
this.recyclerView = recyclerView;
}
public void setItemSelected(int position){
recyclerView.getChildAt(selectedRow).setBackgroundColor(fragment.getActivity().getResources().getColor(android.R.color.transparent));
notifyItemChanged(selectedRow);
selectedRow = position;
recyclerView.getChildAt(selectedRow).setBackgroundColor(fragment.getActivity().getResources().getColor(android.R.color.black));
notifyItemChanged(selectedRow);
}
public void updateList(ArrayList<ShelfModel> list){
shelfList.clear();
shelfList.addAll(list);
notifyDataSetChanged();
notifyItemChanged(selectedRow);
}
#Override
public DrawerMenuShelfListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext())
.inflate(R.layout.navigation_drawer_list_row, parent, false);
holder = new ViewHolder(v);
holder.text = (TextView) v.findViewById(R.id.definitionText);
holder.total = (TextView) v.findViewById(R.id.countText);
holder.editButton = (ImageButton)v.findViewById(R.id.editButton);
holder.text.setTypeface(App.MUSEO_100);
holder.total.setTypeface(App.MUSEO_300);
return holder;
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final ShelfModel shelfModel = shelfList.get(position);
holder.text.setText(shelfModel.getName());
holder.total.setText(String.valueOf(shelfModel.getTotal()));
holder.shelfId = shelfModel.getShelfId();
holder.itemView.setOnClickListener(this);
}
#Override
public int getItemCount() {
return shelfList.size();
}
#Override
public void onClick(View v) {
int position = recyclerView.getChildLayoutPosition(v);
((DrawerMenuShelfListListener)fragment).onShelfItemClick(shelfList.get(position));
setItemSelected(position);
}
public interface DrawerMenuShelfListListener extends BaseRecyclerViewListener {
void onShelfItemClick(ShelfModel shelfModel);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView text;
public TextView total;
public ImageView editButton;
public int shelfId;
public ViewHolder(View view) {
super(view);
}
}
}
Ok. I found the solution.If I don't call notifyItemChanged , it works
Related
when i click on one button others should be deselected how to achieve this
I have seen other answers in stack but none solved my problem or i was not able to undrestand properly
public class LocationAreaAdapter extends RecyclerView.Adapter<LocationAreaAdapter.ViewHolder> {
private int mSelectedItem = -1;
private ArrayList<String> mNames = new ArrayList<>();
private Context mContext;
private ArrayList<Boolean> isChecked=new ArrayList<>();
private AdapterView.OnItemClickListener onItemClickListener;
public LocationAreaAdapter(Context context,ArrayList<String> mNames ){
this.mContext=context;
this.mNames=mNames;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.locatin_area_cardview, viewGroup, false);
return new ViewHolder(view,this);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int i) {
holder.name.setText(mNames.get(i));
holder.name.setChecked(i == mSelectedItem);
//what to do here
}
#Override
public int getItemCount() {
return mNames.size();
}
public void onItemHolderClick(ViewHolder holder) {
if (onItemClickListener != null)
onItemClickListener.onItemClick(null, holder.itemView, holder.getAdapterPosition(), holder.getItemId());
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
RadioButton name;
LocationAreaAdapter mAdapter;
public ViewHolder(View itemView, final LocationAreaAdapter mAdapter) {
super(itemView);
this.mAdapter = mAdapter;
name = itemView.findViewById(R.id.location_area_button);
itemView.setOnClickListener(this);
name.setOnClickListener(this);
}
#Override
public void onClick(View v) {
mSelectedItem = getAdapterPosition();
notifyItemRangeChanged(0, mNames.size());
mAdapter.onItemHolderClick(ViewHolder.this);
}
}
}
when i click on the button the button is not clicked rather recyclerview flashes
When you click to viewholder item select position of current and deselect other viewholder position:
holder.name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mSelectedItem = position;
notifyDataSetChanged();
}
});
if (mSelectedItem==position ) {
//viewHolder.item.setVisibility(View.VISIBLE);
show itew here.
} else {
// viewHolder.item.setVisibility(View.GONE);
hideitew here.
}
Everything works well and my onClick in my recyclerView is working in getting the positions of my items, but what my design calls for is to be able to click an item of the recyclerView and open up a new activity (as a popover or pop up). I can achieve this but my problems comes with the information I need to display on the popover. The information comes like this inside the activity (inside a Firebase value call)
attributeList.removeAll(attributeList);
for (DataSnapshot child : dataSnapshot.child("Attribute").getChildren()){
Attribute attribute = child.getValue(Attribute.class);
attribute_list newAttributeList = new attribute_list( attribute.Name + ": " + attribute.Value);
attributeList.add(newAttributeList);
}
attributeAdapter = new attribute_list_adapter(attributeList, getContext());
recyclerAttribute.setAdapter(attributeAdapter);
This works perfectly for displaying the information, but there's more then just a "value" and a "name" associated with the click.
Basically when I select an item, I need to get the position of the item clicked (which I have) and compare it to the position inside attributeList so I can call a Firebase call (or pass the data somehow) to the popover to display values from the "Attribute" class (such as Name, Value, Description, and another list (recyclerView).
My recyclerView:
public class attribute_list_adapter extends RecyclerView.Adapter<attribute_list_adapter.ViewHolder> {
private List<attribute_list> listItems;
private Context context;
public attribute_list_adapter(List<attribute_list> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.attribute_list, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
attribute_list listItem = listItems.get(position);
holder.txtTitle.setText(listItem.getTxtTitle());
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txtTitle;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
}
#Override
public void onClick(View v) {
}
}
}
This is example:
public class attribute_list_adapter extends RecyclerView.Adapter<attribute_list_adapter.ViewHolder> {
private List<attribute_list> listItems;
private Context context;
public attribute_list_adapter(List<attribute_list> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.attribute_list, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.itemView.setOnClickListener(new View.onClickListener() {
#Override
public void onClick(View v) {
onItemClickListener.onItemClick(position);
}
});
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txtberita;
ImageView imgberita;
TextView txtnama;
public ViewHolder(View itemView) {
super(itemView);
txtnama = (TextView) itemView.findViewById(R.id.txtnama);
txtberita = (TextView) itemView.findViewById(R.id.txtberita);
imgberita = (ImageView) itemView.findViewById(R.id.imgberita);
}
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
OnItemClickListener onItemClickListener;
public interface OnItemClickListener{
void onItemClick(int position);
}
}
your Activity. in Oncreate()
public class TestActivity extends AppCompatActivity implements attribute_list_adapter.OnItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
attribute_list_adapter adapter = new attribute_list_adapter(listItems, this);
adapter.setOnItemClickListener(this);
}
#Override
public void onItemClick(int position) {
// code here
}
}
Create an interface something like
public interface OnSingleItemClickListener{
void onSingleItemClick(int position);
}
Then implement it on your ViewHolder like this
public class ViewHolder extends RecyclerView.ViewHolder implements OnSingleItemClickListener {
public ViewHolder(View itemView) {
super(itemView);
}
#Override
void onSingleItemClick(int position){
if(listItems.get(position) == listItems.get(getAdapterPosition)){
// TODO do something here
}
}
now on your OnBindViewHolder inside your adapter you must do this.
holder.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
holder.onSingleItemClick(position);
}
}):
I have horizontalRecycleView inside verticalRecycleview. and I have made horizontatRecycleView selectable using SparseBooleanArray. So every user clicks on an item from Horizontal list I change the background of that position.
The problem is SparseBooleanArray initialized for each view/row uses same physical address I guess. Because every position I select reflects on all horizontal list.
here is my code:
RecycleView-Vertical:
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 3:
return new HorizontalListHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_adapter_recycleview_horizontal, parent, false));
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (type) {
int type = chatAdapterModels.get(position).getVIEWTYPE();
case HORIZONTAL_LIST:
HorizontalListHolder viewHolder = (HorizontalListHolder) holder;
viewHolder.chatHorizontalAdapter.updateData(chatAdapterModels.get(position).getChatHorizontalModels());
break;
}
}
public final class HorizontalListHolder extends RecyclerView.ViewHolder {
ChatHorizontalAdapter chatHorizontalAdapter = null;
private SparseBooleanArray sparseBooleanArray;
#BindView(R.id.recycle_view)
RecyclerView recyclerView;
public HorizontalListHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
sparseBooleanArray = new SparseBooleanArray();
chatHorizontalAdapter = new ChatHorizontalAdapter(context, sparseBooleanArray);
chatHorizontalAdapter.onItemClickListener(this);
recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
recyclerView.setAdapter(chatHorizontalAdapter);
}
}
}
Horizontal-RecycleView
public class ChatHorizontalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private ChatHorizontalAdapter.ClickListner clickListner;
ArrayList<ChatHorizontalModel> horizontalModels;
private SparseBooleanArray sparseBooleanArray;
private int lastPosition;
public ChatHorizontalAdapter(Context activity, SparseBooleanArray sparseBooleanArray) {
context=activity;
this.sparseBooleanArray=sparseBooleanArray;
}
public void updateData(ArrayList<ChatHorizontalModel> horizontalModels,int viewType){
this.horizontalModels=horizontalModels;
notifyDataSetChanged();
// sparseBooleanArray.clear();
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType){
case 1: return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_recycle_adapter_view, parent, false));
// case 2: return new something
}
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ViewHolder viewHolder = (ViewHolder) holder;
String strings=horizontalModels.get(position).getStr();
viewHolder.textView.setText(strings);
viewHolder.imageView.setImageResource(horizontalModels.get(position).getImages());
changeBackgroundColor(sparseBooleanArray.get(position),viewHolder.cardView,viewHolder.imageView,viewHolder.textView,horizontalModels.get(position).getImages());
}
#Override
public int getItemViewType(int position) {
return viewType;
}
#Override
public int getItemCount() {
return horizontalModels.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
#BindView(R.id.mainCardView)CardView cardView;
#BindView(R.id.image)ImageView imageView;
#BindView(R.id.textView)TextView textView;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
cardView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if(clickListner!=null){
clickListner.onItemClick(getAdapterPosition());
handleSelection(getAdapterPosition());
notifyDataSetChanged();
}
}
}
public interface ClickListner {
void onItemClick(int position);
}
public void onItemClickListener(ChatHorizontalAdapter.ClickListner clickListner){
this.clickListner=clickListner;
}
public void handleSelection(int position){
sparseBooleanArray.clear();
for (int i = 0; i <=horizontalModels.size() ; i++) {
if (i==position) {
sparseBooleanArray.put(i, true);
} else {
sparseBooleanArray.put(i,false);
}
}
}
public void changeBackgroundColor(boolean set, View cardView, ImageView imageView, TextView textView, int images){
// change bck color of selected item
}
}
RecyclerView doesn't create a view to each row in your list, if you have 100 rows, it will create maybe about 7 or 8 " just the number visible on your screen" so you will have about 7 or 8 SparseBooleanArray and it will reuse them with all rows, so a change in one row will affect another if it uses the same view reference.
You should create an ArrayList<SparseBooleanArray> with a number that matches your rows count in the VerticalAdapter and update the SparseBooleanArray inside HorizontalAdapter when onBindViewHolder() is called
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
HorizontalListHolder viewHolder = (HorizontalListHolder) holder;
// Where mSparseList is ArrayList<SparseBooleanArray>
viewHolder.updateSparseArray(mSparseList.get(position));
}
and in the horizontal adapter add the method updateSparseArray() that updates the SparseBooleanArray data.
I want to do different job for different RecycleView item. I created custom OnClickListener for RecycleView and using this from my activity. But problem is when I want to get RecycleView Item Id form OnClick it's return -1. I can't able to check view Id.
RecycleView Adapter:
public class AddFoodAdapter extends RecyclerView.Adapter<AddFoodAdapter.AddFoodApapterVH>{
private ArrayList<FoodReviewItem> foodList = new ArrayList<>();
OnItemClickListener onItemClickListener;
public AddFoodAdapter(){
}
#Override
public AddFoodApapterVH onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycleview_add_food_item, parent, false);
return new AddFoodApapterVH(itemView);
}
#Override
public void onBindViewHolder(AddFoodApapterVH holder, int position) {
holder.tvFoodTitle.setText(foodList.get(position).getFoodName());
holder.tvFoodPrice.setText(foodList.get(position).getFoodPrice());
}
public void add(int position,FoodReviewItem foodItem){
foodList.add(position,foodItem);
notifyItemInserted(position);
}
public void delete(int position){
foodList.remove(position);
notifyItemRemoved(position);
}
#Override
public int getItemCount() {
return foodList.size();
}
public class AddFoodApapterVH extends RecyclerView.ViewHolder implements View.OnClickListener{
public ImageView ivEditFoodItem;
public TextView tvFoodTitle;
public RatingBar rbFoodRating;
public ImageView ivDeleteFoodItem;
public TextView tvFoodPrice;
public AddFoodApapterVH(View itemView) {
super(itemView);
ivEditFoodItem = (ImageView) itemView.findViewById(R.id.ivEditFoodItem);
tvFoodTitle = (TextView) itemView.findViewById(R.id.tvAddFoodItemTitle);
rbFoodRating = (RatingBar) itemView.findViewById(R.id.rbAddFoodItemRating);
ivDeleteFoodItem = (ImageView) itemView.findViewById(R.id.ivDeleteFoodItem);
tvFoodPrice = (TextView) itemView.findViewById(R.id.tvAddFoodItemPrice);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(onItemClickListener!=null){
onItemClickListener.onItemClick(v, getAdapterPosition());
}
}
}
public interface OnItemClickListener {
public void onItemClick(View view , int position);
}
public void SetOnItemClickListener(final OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
}
Form Activity:
private void setUpFoodItemApapter() {
foodItemRecyclerView = (RecyclerView) findViewById(R.id.rvFoodItems);
addFoodAdapter = new AddFoodAdapter();
final LinearLayoutManager layoutManager = new org.solovyev.android.views.llm.LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
foodItemRecyclerView.setLayoutManager(layoutManager);
foodItemRecyclerView.addItemDecoration(new DividerItemDecoration(this, null));
foodItemRecyclerView.setAdapter(addFoodAdapter);
addFoodAdapter.SetOnItemClickListener(new AddFoodAdapter.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Log.i("getid",""+view.getId()); //return -1
Log.i("getid",""+R.id.ivDeleteFoodItem); //return id
// not working
if(view.getId() == R.id.ivDeleteFoodItem){
addFoodAdapter.delete(position);
}
}
});
}
addFoodAdapter.SetOnItemClickListener(new AddFoodAdapter.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
ImageView iv = (ImageView)view;// get imageView
int id = iv.getId();
if(id == R.id.ivDeleteFoodItem){
addFoodAdapter.delete(position);
}
}
});
I've a RecyclerView and CardView inside it. Now what i want to do is load a simple Fragment on clicking the CardView. I'm not able to do that with FragmentManager or SupportFragmentManager. Please give me some directions.
My RecyclerViewAdapder and ViewHolder are ..
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private String[] eventName;
private String[] eventBrief;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName, textViewBrief;
public CardView cardView;
public ViewHolder(View v) {
super(v);
textViewName = (TextView)v.findViewById(R.id.textViewName);
textViewBrief = (TextView)v.findViewById(R.id.textViewBrief);
final Context context = v.getContext();
cardView = (CardView)v.findViewById(R.id.card_view);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EventFragment eventFragment = EventFragment.newInstance();
// Load and view eventFragment here??
}
});
}
}
public RecyclerViewAdapter(String[] eventName, String[] eventBrief) {
this.eventName = eventName;
this.eventBrief = eventBrief;
}
#Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_card_view, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textViewName.setText(eventName[position]);
holder.textViewBrief.setText(eventBrief[position]);
}
#Override
public int getItemCount() {
return eventName.length;
}
}
A simple implementation is to add a onClickListener() to the ViewHolder 's itemView in the constructor of the Adapter.
public MyViewHolder(View view) {
super(view);
itemView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
AppCompatActivity activity = (AppCompatActivity) view.getContext();
Fragment myFragment = new MyFragment();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, myFragment).addToBackStack(null).commit();
}
});
}
I'm not able to do that with FragmentManager or
SupportFragmentManager
That's because RecyclerViewAdapter doesn't hold a reference to the FragmentManager. You should communicate to the Activity/Fragment hosting the RecyclerView, that the event occurred and from there you start the fragment. To do so check my answer here
There are many ways to go at this, really. I feel that the best implementation is a callback to the activity notifying that an item has been clicked. I'll give you a hand with some code, see below:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private String[] eventName;
private String[] eventBrief;
private OnItemClickListener listener;
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView textViewName, textViewBrief;
public CardView cardView;
private OnItemClickListener listener;
public ViewHolder(View v, OnItemClickListener listener) {
super(v);
textViewName = (TextView)v.findViewById(R.id.textViewName);
textViewBrief = (TextView)v.findViewById(R.id.textViewBrief);
final Context context = v.getContext();
cardView = (CardView)v.findViewById(R.id.card_view);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(listener!=null)
listener.onItemClick(textViewName.getText().toString(),
textViewBrief.getText().toString();
}
});
}
}
public RecyclerViewAdapter(String[] eventName, String[] eventBrief) {
this.eventName = eventName;
this.eventBrief = eventBrief;
}
#Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_card_view, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textViewName.setText(eventName[position]);
holder.textViewBrief.setText(eventBrief[position]);
holder.setOnItemClickListener(listener);
}
#Override
public int getItemCount() {
return eventName.length;
}
public void setOnItemClickListener(OnItemClickListener listener){
this.listener = listener
}
public interface OnItemClickListener {
public void onItemClick(String textName, String textViewBrief);
}
}
Then in your activity just instantiate it like this:
RecyclerViewAdapter adapter = new RecyclerViewAdapter(eventName, eventBrief);
adapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener(){
public void onItemClick(String textName, String textViewBrief){
EventFragment eventFragment = EventFragment.newInstance();
//replace content frame with your own view.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.content_frame, eventFragment).commit()
}
});