作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
他
我想知道,是否可以在 RecyclerView 中动态加载 Items。
我想做一个像这个应用程序的列表 Lifeline example aplication
我试过用
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() 的末尾放置一些延迟机制
方法。无论如何 - 祝你好运!
关于android - 如何更新 RecyclerView ,让每个项目每 N 秒出现一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33225040/
我是一名优秀的程序员,十分优秀!