如何在RecycleView中迭代进度栏项

时间:2019-03-31 18:41:54

标签: java android progress-bar android-progressbar

我正在使用RecyclerView来查看许多进度条,如幻灯片和适配器。

这是我的适配器类

public class RecAdapter extends RecyclerView.Adapter<RecAdapter.ViewHolder> {

private static final String TAG = "RecAdapter";
private ArrayList progressbars = new ArrayList<>();
private Context pcontex;
public RecAdapter(Context pcontex ,ArrayList progressbars) {
    this.progressbars = progressbars;
    this.pcontex = pcontex;
}
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.progress,viewGroup,false);
    ViewHolder holder = new ViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
    Log.d(TAG, "onBindViewHolder: called");
}

@Override
public int getItemCount() {
    return progressbars.size() ;
}
public class ViewHolder extends RecyclerView.ViewHolder {

    ProgressBar b ;
    ConstraintLayout parent;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
       b = itemView.findViewById(R.id.progressBar);
        parent= itemView.findViewById(R.id.parent);
    } 
}
}

我的MainActivity代码-

Prog = new ArrayList<>();  b2 = findViewById(R.id.progressBar) ;
b = findViewById(R.id.progressBar);  
probitmap();

private void probitmap(){

Log.d(TAG, "probitmap: preparing bit maps");
Prog.add(b); 
Prog.add(b2);

Recycle();// this method is out of on create
}

private void  Recycle(){

Log.d(TAG, "Recycle: init recycleview");
recyclerView =findViewById(R.id.Rec);
RecAdapter Adapter = new RecAdapter(this,Prog);
recyclerView.setAdapter(Adapter);
        LinearLayoutManager layoutManager
        = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
}

代码运行正常,但是我需要检索回收视图中的每个进度条,并为每个进度条设置进度。

我尝试了多种方法,但是没有用。 这是我尝试过的-

for (int i = 0; i < recyclerView.getChildCount(); i++) {
child = recyclerView.getChildAt(i);


//In case you need to access ViewHolder:
RecAdapter.ViewHolder v = (RecAdapter.ViewHolder) recyclerView.getChildViewHolder(child);   v.b.setProgress(100);
}

0 个答案:

没有答案