[英]How to update RecyclerView , so each item appears every N seconds?
他
我想知道,是否可以動態地在RecyclerView中加載Items。
我想列出一個類似於此應用程序生命線示例應用程序的列表
我嘗試使用
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
Log.i("tag", "This'll run 300 milliseconds later");
}
},300);
但這對我不起作用,因為等待了那么長時間,然后立即顯示所有內容
這是我的代碼
public class TextoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private static final int TYPE_BUTTON = 0;
private static final int TYPE_TEXT = 1;
private Context context;
private int lastPosition = -1;
@Override
public int getItemViewType(int position) {
// Just as an example, return 0 or 2 depending on position
int viewType;
if ( position % 2 * 2 == 0 )
{
viewType = TYPE_TEXT;
}else
{
viewType = TYPE_BUTTON;
}
return TYPE_TEXT;
}
public static class TextoViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView texto;
TextoViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv_texto);
texto = (TextView)itemView.findViewById(R.id.texto);
}
}
public static class ButtonViewHolder extends RecyclerView.ViewHolder {
CardView cv_btn;
Button izq, der;
ButtonViewHolder(View itemView) {
super(itemView);
cv_btn = (CardView)itemView.findViewById(R.id.cv_botones);
izq = (Button)itemView.findViewById(R.id.btn_izquierdo);
der = (Button)itemView.findViewById(R.id.btn_derecho);
}
}
List<Texto> textos;
LinearLayoutManager llm;
RecyclerView rv;
public TextoAdapter(List<Texto> textos, LinearLayoutManager llm,RecyclerView rv,Context context){
this.textos = textos;
this.llm = llm;
this.rv = rv;
this.context = context;
}
public void addItemsToList (Texto texto){
textos.add(texto);
this.notifyDataSetChanged();
rv.smoothScrollToPosition(getItemCount());
}
@Override
public int getItemCount() {
return textos.size();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater mInflater = LayoutInflater.from ( viewGroup.getContext () );
switch ( viewType ) {
case TYPE_TEXT:
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.texto_card, viewGroup, false);
TextoViewHolder pvh = new TextoViewHolder(v);
return pvh;
case TYPE_BUTTON:
ViewGroup v2 = ( ViewGroup ) mInflater.inflate (R.layout.botones_card, viewGroup, false);
ButtonViewHolder vhGroup = new ButtonViewHolder(v2);
return vhGroup;
default:
View v3 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.texto_card, viewGroup, false);
TextoViewHolder pvh3 = new TextoViewHolder(v3);
return pvh3;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int i) {
switch ( holder.getItemViewType () ) {
case TYPE_TEXT:
TextoViewHolder textoViewHolder = ( TextoViewHolder ) holder;
textoViewHolder.texto.setText(textos.get(i).texto);
setAnimation(textoViewHolder.cv, i);
textoViewHolder.cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addItemsToList(new Texto("otroooooo"));
}
});
break;
case TYPE_BUTTON:
ButtonViewHolder buttonViewHolder = ( ButtonViewHolder ) holder;
setAnimation(buttonViewHolder.cv_btn, i);
buttonViewHolder.izq.setText("SI");
buttonViewHolder.der.setText("NO");
break;
}
}
/**
* Here is the key method to apply the animation
*/
private void setAnimation(View viewToAnimate, int position)
{
// If the bound view wasn't previously displayed on screen, it's animated
if (position > lastPosition)
{
//Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.fade_in);
animation.setDuration(1000);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
主要
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
List<Texto> textos;
textos = new ArrayList<>();
textos.add(new Texto("Hola y bienvenido al juego"));
TextoAdapter adapterTextos = new TextoAdapter(textos,llm,rv,this );
rv.setAdapter(adapterTextos);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
謝謝
我認為解決方案之一是制作兩個ArrayList,並將文本從一個復制到另一個,並不斷通知適配器-像這樣:
制作兩個數組:
ArrayList<String> textsQueue; // put all the texts here
ArrayList<String> textos; // pass it to the adapter
使用ScheduledExecutorService或其他計時器。 我會做這樣的事情:
ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
int currentTextNo = 0;
Runnable nextText = new Runnable {
@Override
public void run() {
textos.add(textsQueue.get(currentTextNo));
currentTextNo++;
adapter.notifyDataSetChanged();
if (currentTextNo == textsQueue.size()) {
ses.shutdownNow();
}
}
};
// the parameters are: runnable, initial delay, period, time unit
ses.scheduleAtFixedRate(nextText, 0, 300, TimeUnit.MILLISECONDS);
我不知道這是否是最佳解決方案,也沒有驗證代碼,但我認為它可以工作。 我認為您甚至可以使文本在隨機時間后出現。 為此,您可以-但這只是我的猜測-而不是使用scheduleAtFixedRate
而是使用scheduleWithFixedDelay
並將一些延遲機制放在run()
方法的末尾。 無論如何-祝你好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.