Code
@Override
public void onBindViewHolder(final MessageViewHolder holder, int position) {
String current_user_id = mAuth.getInstance().getCurrentUser().getUid();
Messages messages = mMessagesList.get(position);
String from_user = messages.getFrom();
if (from_user != null && from_user.equals(current_user_id)) {
holder.messageText.setTextColor(Color.BLACK);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.messageText.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.messageText.setLayoutParams(params);
final Handler handler = new Handler();
final int delay = 1000; //milliseconds
final long Seen = messages.isSeen();
handler.postDelayed(new Runnable(){
public void run(){
if (Seen==1){
holder.messageText.setBackgroundResource(R.drawable.text_background2);
}else{
holder.messageText.setBackgroundResource(R.drawable.text_background1);
}
}
}, delay);
}else {
holder.messageText.setBackgroundResource(R.drawable.text_background2);
holder.messageText.setTextColor(Color.BLACK);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.messageText.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
holder.messageText.setLayoutParams(params);
}
holder.messageText.setText(messages.getMessage());
}
Currently im trying to achieve the same like this... but just doesn't seem to happen... i get the desired result only after i refresh the activity... i want the code which is inside the runnable to execute every one second... is my approach correct? please help... thanks in advance
Tried Solution
messageList = (RecyclerView) findViewById(R.id.message_list);
linearLayoutManager = new LinearLayoutManager(getApplicationContext());
mAdapter = new AdapterMessages(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() {
mAdapter.notifyDataSetChanged();
handlerr.postDelayed(this, finalDelay1);
}
}, delay);
Try this
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
@Override
public void run() {
yourfunction();
handler.postDelayed(this, 1000);
}
};
//Start
handler.postDelayed(runnable, 1000);
Or try Timer
//Declare the timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
//Called each time when 1000 milliseconds (1 second) (the period
parameter)
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
1000);
Inside your Activity or Fragment you need to create Handler like below.
Note: Remove Handler related code from the "onBindViewHolder" adapter you just need to Notify your adapter.
(1)Create Handler and Runnable like
public static final int TIME=1000;
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
@Override
public void run() {
//Notify your recyclerview adapter here
handler.postDelayed(this, TIME);
}
};
(2)Start Handler like
handler.postDelayed(runnable, TIME);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.