Code
public class MessagesAdapter extends RecyclerView.Adapter<MessagesAdapter.MessageViewHolder>{
private List<MessagesHelper> mMessagesHelperList;
private DatabaseReference mDatabaseReference;
private FirebaseAuth mAuth;
private Handler handler = new Handler();
private Runnable runnable;
public MessagesAdapter(List<MessagesHelper> mMessagesHelperList)
{
this.mMessagesHelperList = mMessagesHelperList;
}
public class MessageViewHolder extends RecyclerView.ViewHolder{
public TextView messageText;
public MessageViewHolder(View view)
{
super(view);
messageText = (TextView)view.findViewById(R.id.message_text_layout);
}
}
@Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_activity_chat,parent,false);
mAuth = FirebaseAuth.getInstance();
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
return new MessageViewHolder(V);
}
@Override
public void onBindViewHolder(final MessageViewHolder holder, int position) {
String current_user_id = mAuth.getInstance().getCurrentUser().getUid();
MessagesHelper messagesHelper = mMessagesHelperList.get(position);
String from_user = messagesHelper.getFrom();
final boolean Seen = messagesHelper.isSeen();
if (from_user != null && from_user.equals(current_user_id)) {
holder.messageText.setBackgroundColor(Color.BLUE);
holder.messageText.setTextColor(Color.WHITE);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.messageText.getLayoutParams();
params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.messageText.setLayoutParams(params);
if (Seen==false){
holder.messageText.setBackgroundColor(Color.BLUE);
}else{
holder.messageText.setBackgroundColor(Color.CYAN);
}
}else {
holder.messageText.setBackgroundColor(Color.GREEN);
holder.messageText.setTextColor(Color.BLACK);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.messageText.getLayoutParams();
params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
holder.messageText.setLayoutParams(params);
}
holder.messageText.setText(messagesHelper.getMessage());
}
@Override
public int getItemCount() {
return mMessagesHelperList.size();
}
}
如您所见,背景颜色可能会根据数据而变化
但是在我的活动中,颜色不会改变,直到我重新开始我的活动......我猜是因为颜色是在活动的onCreate运行时设置的。
所以我尝试了这样的事情
messageList = (RecyclerView) findViewById(R.id.message_list);
mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
linearLayoutManager = new LinearLayoutManager(getApplicationContext());
mAdapter = new MessagesAdapter(messagesList);
messageList.setHasFixedSize(true);
messageList.setLayoutManager(linearLayoutManager);
messageList.setAdapter(mAdapter);
final Handler handlerr = new Handler();
int delay = 1000; //milliseconds
final int finalDelay1 = delay;
handlerr.postDelayed(new Runnable() {
public void run() {
messageList.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
handlerr.postDelayed(this, finalDelay1);
}
}, delay);
但是没有用...如何每秒重置适配器或最新的适配器?或者我应该每次调用oncreate让它工作
我的代码目前的问题是它可以工作,但如果看到的= false,然后我运行活动,然后看到变成真的,它的颜色一直保持到我重新启动活动......所以我希望它无需重新启动即可更改,以便有人帮助我......此外,数据位于 Firebase 中,因此适配器会获取该数据
自定义 XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/message_single_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp">
<TextView
android:id="@+id/message_text_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@drawable/text_background2"
android:padding="10dp"
android:text="Textview"
android:textSize="15dp"
android:textColor="@color/black"/>
</RelativeLayout>
不需要设置适配器增益,只需在值更改时通知适配器即可。
if(messagesList != null && messagesList.size() > 0){
mAdapter = new MessagesAdapter(messagesList);
mRListView.setAdapter(mAdapter);
}
如果手动更新适配器
if(messagesList != null && messagesList.size() > 0){
new Handler().postDelayed(new Runnable() {
public void run() {
messagesList.get(0).Seen = true;
mAdapter.notifyDataSetChanged();
}
}, 5000);
}