如何重新运行适配器

时间:2018-10-20 16:15:41

标签: android firebase android-activity firebase-realtime-database firebaseui

代码

    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>

1 个答案:

答案 0 :(得分:0)

无需再次设置适配器增益,只需在值更改后通知适配器即可。

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