6

我有一个LinearLayout, I am dynamically adding TextView, ImageView to that LinearLayout,现在我已将 OnClickListener 添加到这些 TextViews 和 ImageViews。

我有一个数组列表,其中有 10 个项目,使用 for 循环我获取每个值并显示,因为它是测验应用程序,我有下一个和上一个按钮,当我单击它时,我正在从数组列表中一个一个地显示项目。Now I need slide In and slide Out animation to apply to this Linear Layout.

我见过这个:

链接 1

链接 2

我也试过这个

slideIn = new TranslateAnimation(
    TranslateAnimation.RELATIVE_TO_PARENT, 1.0f,
    TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
    TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
    TranslateAnimation.RELATIVE_TO_PARENT, 0.0f);

slideIn.setDuration(500);
slideOut = new TranslateAnimation(
    TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
    TranslateAnimation.RELATIVE_TO_PARENT, -1.0f,
    TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
    TranslateAnimation.RELATIVE_TO_PARENT, 0.0f);

slideOut.setDuration(500);

但不工作请帮助解决这个问题。

编辑:

问题:我已将此应用于线性布局,但发生的情况是,当我单击下一个按钮时,当前问题将向左滑动并出现一个空白屏幕,然后它将滑入并显示下一个问题,但我希望在当前问题滑动时立即出来后应该是下一个问题。

4

2 回答 2

4

在 res/anim/ 中使用这个 xml

leftright.xml
这是从左到右的动画:

  <set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
    <translate android:fromXDelta="-100%" android:toXDelta="0%"
      android:fromYDelta="0%" android:toYDelta="0%"
     android:duration="700"/>
   </set>

在你的java代码中使用它

Handler handler = new Handler();
Runnable runnable = new Runnable(){
{
   public void run()
 {
   item[i].setInAnimation(AnimationUtils.loadAnimation(this,R.anim.leftright.xml));
   i=i+1;
   handler.postDelayed(this,5000);
 }
};handler.postDelayed(runnable,5000);
于 2012-12-25T05:24:18.037 回答
4

如果您的布局具有相同的内容,请在视图翻转器内创建两个布局。用数据加载第一个视图并显示它。当用户单击下一个或上一个时,使用数据加载下一个视图并保留一个标志以显示第二个视图现在可见并用动画显示它。

现在开始根据标志值加载适当的视图和数据并调用showext()。

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

    mainFlipper = (ViewFlipper) findViewById(R.id.flipper);
    firstLayout = (LinearLayout) findViewById(R.id.layout1);
    secondLayout = (LinearLayout) findViewById(R.id.layout2);


    findViewById(R.id.btnPrevious).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            showPrevious();
        }
    });

    findViewById(R.id.btnNext).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            showNext();
        }
    });

}

private void showNext() {
    mainFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_left));
    mainFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_out_right));
    flip();
}

private void showPrevious() {
    mainFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_right));
    mainFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_out_left));
    flip();
}

private void flip() {
    if(isFirstVisible) {
        isFirstVisible = false;
        secondLayout.removeAllViews();
        secondLayout.addView(getTextView("Second"));
    } else {
        isFirstVisible = true;
        firstLayout.removeAllViews();
        firstLayout.addView(getTextView("First"));
    }
    mainFlipper.showNext();
}

private TextView getTextView(String txt) {
    TextView txtView = new TextView(this);
    txtView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    txtView.setText(txt);
    return txtView;
}
于 2012-12-25T05:53:35.910 回答