Adding a view makes the ListView inside the snippet to update.

It can be a little difficult to explain, so the best way I can think of it is to provide you with a video showing the problem.

In the video, I show a scroll of the list, and after 5 seconds a view is created and added inside this holder at the bottom. At this point, the listview is being updated.

http://tinypic.com/player.php?v=vpz0k8%3E&s=8#.U0VrIvl_t8E

The problem is this:

I have an Activity with a layout that consists of:

  • Snippet (above RelativeLayout), match parent, match parent.
  • RelativeLayout, as the contents of the shell.

The snippet displays a ListView with animation for each row.

If I add the view to "RelativeLayout", it will force the fragment to reconfigure the new size, since it is set above this RelativeLayout, so each row will be restored again.

Do you guys think to avoid this?

EDIT: Source code: https://bitbucket.org/sergicast/listview-animated-buggy

+4
source share
3 answers

Do not start the animation if the layout process is in progress to add a footer. The end of the layout process can be determined using ViewTreeObserver (the beginning, obviously, begins with the addition of a footer):

    hand.postDelayed(new Runnable() {

        @Override
        public void run() {
            ViewTreeObserver viewTreeObserver = holder.getViewTreeObserver();
            viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @SuppressWarnings("deprecation")
                @Override
                public void onGlobalLayout() {
                    holder.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    mIgnoreAnimation = false;
                }
            });

            mIgnoreAnimation = true;
            holder.addView(viewToAdd);
        }
    }, 5000);

Add this method to your activity:

public boolean ignoreAnimation() {
    return mIgnoreAnimation;
}

:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Context context = FragmentTest.this.getActivity();

    TextView tv = new TextView(context);
    tv.setText("Pos: " + position);
    tv.setTextSize(35f);

    if (runAnimation()) {
        Animation anim = AnimationUtils.loadAnimation(context, R.anim.animation);
        tv.startAnimation(anim);
    }

    return tv;
}

private boolean runAnimation() {
    Activity activity = getActivity();
    if (activity != null && activity instanceof MainActivity) {
        return ! ((MainActivity)activity).ignoreAnimation();
    }
    return true;
}

, " - " , , .

, ListView, . , , :

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Context context = FragmentTest.this.getActivity();
    TextView tv = null;
    if (convertView != null && convertView instanceof TextView) {
        tv = (TextView) convertView;
    }
    else {
        tv = new TextView(context);
    }
+5

, .

:

  • holder wrap_content. , , " " - ( Android, sic!)
  • holder, , holder . - root RelativeLayout, , , - <fragment>.
  • , , . .

, holder. :

<RelativeLayout
    android:id="@+id/holder"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" >
</RelativeLayout>

. holder .

0

. RelativeLayout

ListView TextView RelativeLayout, , , RelativeLayout.

So, when you add a new TexView, another child fragment is affected, even if its height matches_parent.

You can fix this only by changing the parent layout to LinearLayout .

-1
source

Source: https://habr.com/ru/post/1535798/


All Articles