1

我正在尝试用两个片段实现 Youtube 风格的滑动菜单。我创建了一个自定义动画,该动画将在按下操作栏上的应用程序图标时调用切换功能来更改左侧片段的宽度。Expand.java 将增加宽度,而 Collapse.java 将减小宽度。当我从 MainActivity.java 的 onCreate() 调用 startAnimation() 函数时,它工作正常。但是当我从切换()中调用它时,它不起作用,尽管 Expand.java 和 Collapse.java 的构造函数是

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout 
 android:id="@+id/f1"     
     android:layout_height="match_parent"
    android:layout_width="0dip"  
    >

 <fragment
    android:id="@+id/listFragment"     
     android:layout_height="match_parent"
    android:layout_width="match_parent"   
    class="com.surv.ui123.FragLeft" >

 </fragment>
 </LinearLayout>
 <LinearLayout
    android:id="@+id/f2"     
     android:layout_height="match_parent"
    android:layout_width="match_parent"  
    >

<fragment
    android:id="@+id/detailFragment"
    android:layout_width="match_parent"     
    android:layout_height="match_parent"        
    class="com.surv.ui123.FragRight" >
      </fragment>

</LinearLayout>
</LinearLayout>

FragLeft.java

package com.surv.ui123;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class FragLeft extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
  Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragleft,
    container, false);


return view;
} 
}

FragRight.java

package com.surv.ui123;


import android.support.v4.app.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class FragRight extends Fragment {

 @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragright,
        container, false);      
return view;
}
}

展开.java

package com.surv.ui123;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.widget.Toast;


public class Expand extends Animation implements Animation.AnimationListener {
private View view;
private static long ANIMATION_DURATION;
private int FromWidth;
private int ToWidth;

public Expand(View v, int FromWidth, int ToWidth) {

    this.view = v;
    ANIMATION_DURATION = 1;
    this.FromWidth = FromWidth;
    this.ToWidth = ToWidth;
    setDuration(ANIMATION_DURATION);
    setRepeatCount(10);
    setFillAfter(false);
    setInterpolator(new AccelerateInterpolator());
    Toast.makeText(view.getContext(),"Expand",Toast.LENGTH_SHORT).show();
    setAnimationListener(this);

}

@Override
public void onAnimationEnd(Animation arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onAnimationRepeat(Animation animation) {
    // TODO Auto-generated method stub

    LayoutParams lyp =  view.getLayoutParams();     
    lyp.width = lyp.width+(int)(ToWidth/10);
    view.setLayoutParams(lyp);

}

@Override
public void onAnimationStart(Animation animation) {
    // TODO Auto-generated method stub

    Toast.makeText(view.getContext(),"Strarted     Expand",Toast.LENGTH_SHORT).show();
}

}

折叠.java

package com.surv.ui123;

import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.widget.Toast;

public class Collapse extends Animation implements Animation.AnimationListener {

private View view;
private static long ANIMATION_DURATION;

private int FromWidth;
private int ToWidth;
public Collapse(View v, int ToWidth, int FromWidth) {

    Toast.makeText(view.getContext(),"Collapse",Toast.LENGTH_SHORT).show();
    this.view = v;      
    ANIMATION_DURATION = 1;
    this.FromWidth = FromWidth;
    this.ToWidth = ToWidth;
    setDuration(ANIMATION_DURATION);
    setRepeatCount(10);
    setFillAfter(false);
    setInterpolator(new AccelerateInterpolator());
    setAnimationListener(this);
}


@Override
public void onAnimationEnd(Animation animation) {
    // TODO Auto-generated method stub

}

@Override
public void onAnimationRepeat(Animation animation) {
    LayoutParams lyp =  view.getLayoutParams();     
    lyp.width = lyp.width-(int)(ToWidth/10);
    view.setLayoutParams(lyp);  
}

@Override
public void onAnimationStart(Animation animation) {
    // TODO Auto-generated method stub



}

}

MainActivity.java

package com.surv.ui123;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import com.actionbarsherlock.app.SherlockFragmentActivity;



 public class MainActivity extends SherlockFragmentActivity {

 LinearLayout MenuList,RFrg;
 boolean isExpanded=true;    
 int screenWidth;

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

    MenuList = (LinearLayout) findViewById(R.id.f1);
    RFrg=(LinearLayout) findViewById(R.id.f2);
    LayoutParams lyp =  RFrg.getLayoutParams();     

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        screenWidth = metrics.widthPixels;
        lyp.width = screenWidth;

        RFrg.setLayoutParams(lyp);
        getSupportActionBar().setHomeButtonEnabled(true);               

}

@Override
    public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)           {
        switch (item.getItemId()) {
            case android.R.id.home:
                // app icon in action bar clicked; go home
                toggle();

                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


    public void toggle()
    {       

    isExpanded=!isExpanded;

        if (isExpanded) {
            Collapse x= new Collapse(MenuList, 0,(int)(screenWidth*0.75));
            MenuList.startAnimation(x);
        }else {
            Expand d=new Expand(MenuList, 0,(int)(screenWidth*0.75));

            MenuList.startAnimation(d);
        }
    }                                                   


 }
4

0 回答 0