Question

I have a Button, a View and a ListView in my LinearLayout. On button click event I want view to disappear and the listview is take its place. I tried to set LayoutParams to match_parent dinamically after the animation but it doesn't work. Any ideas?

Here is my activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/container"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="com.example.linearlayouttry.MainActivity" >

 <Button android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start"/>

 <View android:id="@+id/view"
    android:layout_width="fill_parent"
    android:background="@android:color/black"
    android:layout_height="300dp"/>

 <ListView android:id="@+id/listv"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

 </ListView>

</LinearLayout>

Here is my MainActivity.java:

public class MainActivity extends Activity implements OnClickListener {

protected View v;
protected ListView lv;

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

    ArrayList<String> list = new ArrayList<String>(Arrays.asList("1","2","3","4","5","6","7","8","9","10"));
    lv = (ListView) findViewById(R.id.listv);
    lv.setAdapter(new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, list));

    Button mButton = (Button) findViewById(R.id.button);
    mButton.setOnClickListener(this);

    v = findViewById(R.id.view);
}

@Override
public void onClick(View v) {
    this.v.setAlpha(0);
    int height = this.v.getLayoutParams().height;
    lv.animate().translationY(-height).setListener(new AnimatorListener() {
        public void onAnimationStart(Animator animation) {}
        public void onAnimationRepeat(Animator animation) {}

        @Override
        public void onAnimationEnd(Animator animation) {
            lv.getLayoutParams().height=LayoutParams.MATCH_PARENT;
            lv.requestLayout();
        }

        public void onAnimationCancel(Animator animation) {}
    });
}
}

It's working when I set a specific integer to height, but how can I do it with match_parent?

SOLVED:

I only had to remove the view from the linearlayout like this:

public void onAnimationEnd(Animator animation) {
            parent.removeView(this.v);
            lv.setTranslationY(0);
}
Était-ce utile?

La solution 2

Thanks for the answer, I solved the problem with remove the view from the linearlayout like this:

public void onAnimationEnd(Animator animation) {
            parent.removeView(this.v);
            lv.setTranslationY(0);
}

Autres conseils

Try

lv.setLayoutParams(new LayoutParams(widthValue, LayoutParams.MATCH_PARENT));

Instead of

lv.getLayoutParams().height=LayoutParams.MATCH_PARENT;
lv.requestLayout();

More info about the LayoutParams class can be found here.

alternatively,

public void onAnimationEnd(Animator animation) {
     this.v.setVisibility(View.GONE);
     lv.setTranslationY(0);
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top