2014-04-18 69 views
0

我在我的LinearLayout中有一个Button,一个View和一个ListView。在按钮单击事件我想要视图消失,并且listview被取代它。我试图在动画之后将LayoutParams设置为match_parent dinamically,但它不起作用。有任何想法吗?android listview更改动画后的高度

这里是我的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> 

这里是我的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) {} 
    }); 
} 
} 

它的工作时,我设置一个特定的整数高度,但我怎么可以这样做match_parent?

解决:

我只有从LinearLayout中这样除去观点:

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

回答

0

感谢您的回答,我解决了删除的问题从LinearLayout中像这样的观点:

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

尝试

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

而不是

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

有关LayoutParams类的更多信息可以参见here

+0

谢谢,我试了一下,但它与ClassCastException异常退出,如果我写的'新LinearLayout.LayoutParams( )'它不能再工作:(。 – bendaf