How to get each view from addView() method dynamically?

202 Views Asked by At

So I have a layout with adapterViewFlipper inside. And I set progress bar at the top of main layout view with addView() method, so when layout of adapterViewFlipper is ex: 4 then my progress bar is also 4. But instead every layout changes according to each progress bar, my progress bar run 4 of them at the same time. Here is the result of my code look like.

Here is my MainActivity:

public class MainActivity extends AppCompatActivity {

private AdapterViewFlipper mViewFlipper;
private MyAdapter mAdapter;
private LinearLayout mLayout;
private ProgressBar progressBar;

private int childCount;

private String[] values = {"satu", "dua", "tiga", "empat"};
private int[] colors =  {Color.BLUE, Color.GRAY, Color.GREEN, Color.RED};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mViewFlipper = findViewById(R.id.viewflipper);
    mLayout = findViewById(R.id.layoutParent);

    mAdapter = new MyAdapter(values, colors);

    mViewFlipper.setAdapter(mAdapter);
    mViewFlipper.setFlipInterval(2500);
    childCount = mViewFlipper.getCount();
    mViewFlipper.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
            if (mViewFlipper.getDisplayedChild() == childCount - 1) {
                mViewFlipper.stopFlipping();
            }
        }
    });
    mViewFlipper.startFlipping();

    for (int i = 0; i < childCount; i++) {
        View view = LayoutInflater.from(this).inflate(R.layout.progress, null);
        view.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
        progressBar = view.findViewById(R.id.bar);
        setMargins(progressBar, 0, 0, 8, 0);
        setProgressMax(progressBar, 100);
        setProgressAnimate(progressBar, 100);
        mLayout.addView(progressBar);
    }

}

private void setProgressMax(ProgressBar pb, int max) {
    pb.setMax(max * 1000);
}

private void setProgressAnimate(ProgressBar pb, int progressTo)
{
    ObjectAnimator animation = ObjectAnimator.ofInt(pb, "progress", pb.getProgress(), progressTo * 1000);
    animation.setDuration(2500);
    animation.setInterpolator(new LinearInterpolator());
    animation.start();
}

private void setMargins (View view, int left, int top, int right, int bottom) {
    if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        p.setMargins(left, top, right, bottom);
        view.requestLayout();
    }
}

}

What I really want is each progreess bar for each layout. For example, when first progress bar is done then second progress bar started and so on just like my adapterViewFlipper. How can I fix that? thanks.

2

There are 2 best solutions below

1
DinhNguyen On

You should show Progreess Bar in addOnLayoutChangeListener

mViewFlipper.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
        if (mViewFlipper.getDisplayedChild() == childCount - 1) {
            mViewFlipper.stopFlipping();
        } else {
           View view = LayoutInflater.from(this).inflate(R.layout.progress, null);
           view.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
           progressBar = view.findViewById(R.id.bar);
           setMargins(progressBar, 0, 0, 8, 0);
           setProgressMax(progressBar, 100);
           setProgressAnimate(progressBar, 100);
           mLayout.addView(progressBar);
        }
    }
});
0
Iceka On

I've finally solve this problem. Here's my solution:

private ProgressBar[] progressBar1;

progressBar1 = new ProgressBar[childCount];
    for (int e = 0; e < childCount; e++) {
        progressBar1[e] = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
        progressBar1[e].setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
        setMargins(progressBar1[e], 0, 0, 8, 0);
        setProgressMax(progressBar1[e], 100);
        viewGroup = findViewById(R.id.layoutParent);
        viewGroup.addView(progressBar1[e]);
    }

    mViewFlipper.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
            if (mViewFlipper.getDisplayedChild() == childCount - 1) {
                mViewFlipper.stopFlipping();
            }
            setProgressAnimate(progressBar1[mViewFlipper.getDisplayedChild()], 100);
        }
    });

    mViewFlipper.startFlipping();