Last Updated
Viewed 49 Times

I am trying to add an image view as a banner on top of recycler view. so in some condition I can hide (View.GONE) or to show that banner image view (View.VISIBLE). but the problem is, the banner image view will never show when I run the app, even though I have set android:visibility="visible" on the image view xml.

as you can see, I have image view with red background, but that red background image view will not displaying

how to solve this ?

the layout in my fragment is like this

enter image description here

and here is the xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".Fragments.Search.SearchKeywordResultFragment"
                                                   android:id="@+id/constraintLayout_search_keyword_fragment">


    <androidx.recyclerview.widget.RecyclerView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:scrollbars="vertical"
            tools:listitem="@layout/item_general_event"
            android:id="@+id/recyclerView_keyword_search_result"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>

    <ImageView
            android:id="@+id/imageView_banner_search_keyword"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:background="#E91E63"
            android:visibility="visible"
            app:layout_constraintBottom_toBottomOf="@+id/recyclerView_keyword_search_result"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            tools:src="@tools:sample/avatars[3]" />


</androidx.constraintlayout.widget.ConstraintLayout>

I have created one list. In that I am showing expense amount, expense type, expense category and expense time.

I have used RecyclerView with multiple view type, for view type 1 I am showing a small circle and view type two it's a big circle.

Expense type is to show two different circles, one is small for time and another is big for date. Both I can see.

But now I want to give different colors on small circle i.e time depending on the time. The problem is I can see circle of only yellow color filled with yellow color which is for 12:30 and shows time and other circles are blank, no color no time only a border.

    public class IAdapter extends RecyclerView.Adapter<IAdapter.ItemViewHolder> {

    ArrayList<Expense> item;
    public static final int TYPE1=1;
    Context conext;

    IAdapter(Context context,ArrayList<Expense> item) {
        this.conext=context;
       this.item=item;
    }

    @Override
    public int getItemCount() {
        return item.size();

    }

    @Override
        public int getItemViewType(int position) {
            return item.get(position).getExpenseType();// Assume that this return 1 0r 2
        }

    @Override
    public void onBindViewHolder(ItemViewHolder itemViewHolder, int i) {

        itemViewHolder.amount.setText(item.get(i).amount);
        itemViewHolder.expense.setText(item.get(i).expense);

        Expense expense = new Expense();
        expense = item.get(i);

        if(TYPE1==i) {

            if(expense.getTime().equals("10:30")) {
                TextDrawable drawable = TextDrawable.builder()
                        .beginConfig()
                        .withBorder(4)
                        .textColor(Color.BLACK)
                        .useFont(Typeface.DEFAULT)
                        .fontSize(25)
                        .bold()
                        .toUpperCase()
                        .endConfig()
                        .buildRound(expense.getTime(), Color.GREEN);

                itemViewHolder.small.setImageDrawable(drawable);
            }else if(expense.getTime()
                    .equals("12:30")) {
                TextDrawable drawable = TextDrawable.builder()
                        .beginConfig()
                        .withBorder(4)
                        .textColor(Color.BLACK)
                        .useFont(Typeface.DEFAULT)
                        .fontSize(25)
                        .bold()
                        .toUpperCase()
                        .endConfig()
                        .buildRound(expense.getTime(), Color.YELLOW);
                itemViewHolder.small.setImageDrawable(drawable);
            }else  if(expense.getTime().equals("4:30")) {
                TextDrawable drawable = TextDrawable.builder()
                        .beginConfig()
                        .withBorder(4)
                        .textColor(Color.BLACK)
                        .useFont(Typeface.DEFAULT)
                        .fontSize(25)
                        .bold()
                        .toUpperCase()
                        .endConfig()
                        .buildRound(expense.getTime(), Color.BLUE);
                itemViewHolder.small.setImageDrawable(drawable);
            }else if(item.get(i).time.equals("7:30")){
                TextDrawable drawable = TextDrawable.builder()
                        .beginConfig()
                        .withBorder(4)
                        .textColor(Color.BLACK)
                        .useFont(Typeface.DEFAULT)
                        .fontSize(25)
                        .bold()
                        .toUpperCase()
                        .endConfig()
                        .buildRound(expense.getTime(), Color.GRAY);
                itemViewHolder.small.setImageDrawable(drawable);
            }
        }

    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup,int viewType) {

        if (viewType==TYPE1) {
            View itemView = LayoutInflater.
                    from(viewGroup.getContext()).
                    inflate(R.layout.cardlayout, viewGroup, false);
            return new ItemViewHolder(itemView,viewType);
        } else {
            View itemView = LayoutInflater.from(viewGroup.getContext()).
                    inflate(R.layout.bigcircle, viewGroup, false);
            ItemViewHolder ivh = new ItemViewHolder(itemView, viewType);
            return ivh;
        }
    }


    public static class ItemViewHolder extends RecyclerView.ViewHolder {
        CardView cv;
        TextView amount;
        TextView expense;
        ImageView small;
        ImageView big;

        ItemViewHolder(View itemView, int viewType) {
            super(itemView);
            amount = (TextView) itemView.findViewById(R.id.txtAmount);
            expense = (TextView) itemView.findViewById(R.id.txtexpense);
            cv = (CardView) itemView.findViewById(R.id.card_view);

            if(viewType==TYPE1) {small = (ImageView) itemView.findViewById(R.id.small);}
            else{  big=(ImageView)itemView.findViewById(R.id.big);
                TextDrawable drawable5 = TextDrawable.builder()
                        .beginConfig()
                        .withBorder(4)
                        .textColor(Color.BLACK)
                        .useFont(Typeface.DEFAULT)
                        .fontSize(25)
                        .bold()
                        .toUpperCase()
                        .endConfig()
                        .buildRound("10Jan", Color.CYAN);
                big.setImageDrawable(drawable5);}

        }
    }
        @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

  }

Fragment :

 public class ItemFragment extends Fragment {

    RecyclerView recyclerView;

    IAdapter adapter;
    ArrayList<Expense> items;

    public ItemFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_item_list, container, false);
        setHasOptionsMenu(true);
        Expense e = new Expense();

        recyclerView = (RecyclerView) view.findViewById(R.id.RecyclerView);
        ImageButton imageButton = (ImageButton) view.findViewById(R.id.imgbtn_fab);
        LinearLayoutManager llm = new LinearLayoutManager(this.getActivity());


        items=new ArrayList<>();
        recyclerView.setLayoutManager(llm);

        recyclerView.setHasFixedSize(true);

        initializeDataType1();

        initRecyclerAdapter();

        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), Main2Activity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("list", items);
                intent.putExtras(bundle);
                startActivityForResult(intent, 101);
            }
        });
        return view;
    }

    private void initRecyclerAdapter() {
        adapter = new IAdapter(getActivity(),items);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 101 && resultCode == 102) {
            Bundle bundle = data.getExtras();
            Expense ex = (Expense) bundle.getSerializable("ex");
            items.add(ex);
            initRecyclerAdapter();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    private void initializeDataType1() {

        items.add(new Expense("1000", "2000", 1,"10:30"));
        items.add(new Expense("2000", "5000", 1,"12:30"));
        items.add(new Expense("3000", "400", 2));
        items.add(new Expense("1000", "4000", 1,"4:30"));
        items.add(new Expense("3000", "3000", 2));
        items.add(new Expense("2000", "100", 1,"4:30"));
        items.add(new Expense("2000", "3333", 2));
        items.add(new Expense("3000", "shopping", 1,"7:30"));
        items.add(new Expense("1000", "food", 1,"7:30"));
        items.add(new Expense("1000", "food", 2));
        items.add(new Expense("2000", "drink", 1,"7:30"));
        items.add(new Expense("3000", "shopping", 2,"10:30"));
        items.add(new Expense("2000", "3333", 1,"7:30"));
        items.add(new Expense("3000", "shopping", 1,"7:30"));
        items.add(new Expense("1000", "food", 1,"12:30"));
        items.add(new Expense("1000", "food", 1,"7:30"));
        items.add(new Expense("2000", "drink", 1,"10:30"));
        items.add(new Expense("3000", "shopping", 1,"10:30"));
    }
}

It's comparing for only 12:30 time and others are skipped. Why is this happening?

I am added a background image highlight in RecyclerView Image:

but when i click the image previous selected image highlight is also shown. how to fix this. my code is

holder.colorImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            holder.colorImage.setBackgroundResource(0);

            for (int i = 0; i < colorId.size(); i++) {

                holder.colorImage.setBackgroundResource(R.drawable.image_border);
            }
        }

    });

This is the project I am trying to run. Here is my code for the onBindViewHolder from RecyclerView.Adapter class

@Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        TextView title = (TextView) holder.view.findViewById(R.id.title);
        final TextView desc = (TextView) holder.view.findViewById(R.id.desc);
        final ImageView imageView = (ImageView) holder.view.findViewById(R.id.imageView);

        title.setText(pojos.get(position).getTitle());
        desc.setText(pojos.get(position).getDesc());

        imageView.setImageResource(pojos.get(position).getImage());

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                desc.setText("clicked");
                desc.setBackgroundColor(Color.BLUE);
                imageView.setImageResource(R.drawable.heart_red);
            }
        });

    }

The list loads fine, the problem happens when the imageView's onclicklistener is called.

desc.setText("clicked");

The line above makes the change in the list item on which it was clicked on. but

 desc.setBackgroundColor(Color.BLUE);

when this line is executed, the change reflects in multiple items on the list. What is going wrong? In the pictures shown below, I clicked on item 0, the text changes to "clicked" and color is set. But when I scroll down, item 12 has also been affected from my click on item 0. Only the background color change has reflected, not the text change. How do I stop this?

enter image description here

enter image description here

I have been trying to solve this for a long time, kindly download the project and try executing the code to understand what I exactly mean, if my question is not clear.

Similar Question 6 (2 solutions) : RecyclerView setting a image in the wrong place

Similar Question 7 (2 solutions) : How to handle variable height image in RecyclerView?

Similar Question 8 (3 solutions) : UI freezing while showing image into ImageView

cc