如何更改Recycler Item Color?

时间:2016-11-16 04:10:44

标签: android android-recyclerview

OUTPUT...

我有RecyclerView而且我在膨胀数据。我的要求是我必须以黑色显示第一个项目文本,其余项目应为灰色。当用户滚动时,那个灰色的颜色项应为黑色,其跟随者将再次变为灰色。我正在考虑使用这个位置,但我很困惑。

    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);
        }
    }

}

我更新的onScrollIng和动态更改代码

  @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);
    }
}
}

我的活动,我在呼叫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);
        }
    });

2 个答案:

答案 0 :(得分:4)

首先在您的主Activity中使用此代码查找第一个可见项:

        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));
    }

我只是以文字为例,但您也可以更改cardview的背景

答案 1 :(得分:3)

使用holder.itemView.setBackgroundColor(...);

实施例

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

}

更新

@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));

}