How to change the color of a Recycler item?

OUTPUT ...

I have RecyclerView, and I am inflating the data. My requirement is that I have to show the text of the first element in black, the rest of the elements should be gray. And when the user scrolls, this gray color should be black, and his follower will again be gray. I thought about it, but I'm confused.

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>
    implements RecyclerViewFastScroller.BubbleTextGetter {

    private List<String> mDataArray;

    public RecyclerViewAdapter(List<String> dataset) {
        mDataArray = dataset;
    }

    @Override
    public int getItemCount() {
        if (mDataArray == null)
            return 0;
        return mDataArray.size();
    }

    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_view_layout, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTextView.setText(mDataArray.get(position));

    }

    @Override
    public String getTextToShowInBubble(int pos) {
        if (pos < 0 || pos >= mDataArray.size())
            return null;

        String name = mDataArray.get(pos);
        if (name == null || name.length() < 1)
            return null;
        return mDataArray.get(pos).substring(0, 1);

    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_alphabet)
        public TextView mTextView;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}

My updated code for onScrollIng and dynamic change

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

    /*Tuesday, April 10, 2001 3:51 PM*/

    msgModels = msgList.get(position);
    if (msgList.get(position) != null) {

            holder.msgTitle.setText(msgModels.getMessageTitle());
            holder.msgDescription.setText(msgModels.getMessageDescription());
            holder.msgDate.setText(msgModels.getMessageDate());
    }

    if (position == firstVisible) {
        holder.msgTitle.setTextColor(Color.RED);
        holder.msgDescription.setTextColor(Color.RED);
        holder.msgDate.setTextColor(Color.RED);
    }else{
        holder.msgTitle.setTextColor(Color.BLUE);
        holder.msgDescription.setTextColor(Color.BLUE);
        holder.msgDate.setTextColor(Color.BLUE);
    }


}

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

private int firstVisible = 0;

public void changeItem(int position){
    firstVisible = position;
    notifyItemChanged(firstVisible);
    notifyDataSetChanged();
}


public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView msgTitle, msgDescription,msgDate;

    public ViewHolder(View view) {
        super(view);
        msgTitle  = (TextView)view.findViewById(R.id.msgTitle);
        msgDescription  = (TextView)view.findViewById(R.id.msgDescription);
        msgDate  = (TextView)view.findViewById(R.id.msgDate);
    }
}
}

My activity when I call Recycler onScroll

 private ProgressDialog mProgressDialog = null;
private static final String API_MSG_CALL = "API_MSG_CALL";
private Map<String, String> mapobject;
private LinearLayoutManager linearLayoutManager;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_message, null);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_viewLaws);
    mRecyclerView.setAdapter(msgAdapter);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    msgAdapter = new MessageAdapter(msgList, getActivity(), this);

    linearLayoutManager = new LinearLayoutManager(getActivity());

    //msgAdapter.notifyDataSetChanged();
    mProgressDialog = new ProgressDialog(getActivity());

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int firstVisible = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
            msgAdapter.changeItem(firstVisible);
        }
    });
+4
source share
2 answers

use holder.itemView.setBackgroundColor(...);

Example

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.itemView.setBackgroundColor(yourcolor);
    holder.mTextView.setText(mDataArray.get(position));

}

Update:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    if(position==0){
        holder.itemView.setBackgroundColor(Color.BLACK);
    }else{
        holder.itemView.setBackgroundColor(Color.GREY);

    }
    holder.mTextView.setText(mDataArray.get(position));

}
+3
source

first in your main Office use this code to find the first visible element:

        recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int firstVisible = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
            RecyclerViewAdapter.changeItem(firstVisible);
        }
    });

:

    private int firstVisible = 0;

    public void changeItem(int position){
    firstVisible = position;
    notifyItemChanged(firstVisible);
    notifyDataSetChanged();
    }

onBindViewHolder :

   if (position == firstVisible) {
        holder.txtNumber.setBackgroundColor(mContext.getResources().getColor(R.color.black));
    }

, .

+3

Source: https://habr.com/ru/post/1660883/


All Articles