-7

如何在 android 应用程序中创建与 facebook 应用程序相同的幻灯片抽屉。

4

1 回答 1

3

创建 XML 布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
    android:id="@+id/menu"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="#51d7ff"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/ListView01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#51d7ff"
        android:cacheColorHint="#51d7ff" >
    </ListView>
</LinearLayout>
<LinearLayout
    android:id="@+id/app"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="#033333"
    android:orientation="vertical" >
    <Button
        android:id="@+id/BtnSlide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/menu" />
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#758fb1"
        android:cacheColorHint="#758fb1" >
    </ListView>
</LinearLayout>

主要活动

public class MainActivity extends Activity implements AnimationListener
{

Button Slide_Button;
View menu;
View app;
boolean menuOut = false;
AnimParams animParams = new AnimParams();
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    menu = findViewById(R.id.menu);
    app = findViewById(R.id.app);

    Slide_Button=(Button)findViewById(R.id.BtnSlide);
    Slide_Button.setOnClickListener(OnClick_slide);

    ListView listView = (ListView) app.findViewById(R.id.list);
    ViewUtils.initListView(this, listView, "Itemwa ", 30, android.R.layout.simple_list_item_1);

    ListView listView1 = (ListView) menu.findViewById(R.id.ListView01);
    ViewUtils.initListView(this, listView1, "Itemwana ", 10, android.R.layout.simple_list_item_1);
}

Button.OnClickListener OnClick_slide=new Button.OnClickListener()
{

    @Override
    public void onClick(View arg0)
    {
        // TODO Auto-generated method stub
        System.out.println("onClick " + new Date());
        MainActivity me = MainActivity.this;

        Animation anim;

        int w = app.getMeasuredWidth();
        int h = app.getMeasuredHeight();
        int left = (int) (app.getMeasuredWidth() * 0.8);

        if (!menuOut)
        {

            anim = new TranslateAnimation(0, left, 0, 0);
            menu.setVisibility(View.VISIBLE);
            animParams.init(left, 0, left + w, h);
        } 
        else 
        {

            anim = new TranslateAnimation(0, -left, 0, 0);
            animParams.init(0, 0, w, h);
        }

        anim.setDuration(500);
        anim.setAnimationListener(me);

        anim.setFillAfter(true);

        app.startAnimation(anim);
    }

};

void layoutApp(boolean menuOut) 
{
    app.layout(animParams.left, animParams.top, animParams.right, animParams.bottom);

    app.clearAnimation();

}

@Override
public void onAnimationEnd(Animation animation) 
{
    menuOut = !menuOut;
    if (!menuOut) 
    {
        menu.setVisibility(View.INVISIBLE);
    }
    layoutApp(menuOut);
}

@Override
public void onAnimationRepeat(Animation animation) 
{

}

@Override
public void onAnimationStart(Animation animation)
{

}


static class AnimParams 
{
    int left, right, top, bottom;

    void init(int left, int top, int right, int bottom) 
    {
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    }
}

ViewUtils.java

 public class ViewUtils
 {
 public static void setViewWidths(View view, View[] views) 
 {
    int w = view.getWidth();
    int h = view.getHeight();
    for (int i = 0; i < views.length; i++) 
    {
        View v = views[i];
        v.layout((i + 1) * w, 0, (i + 2) * w, h);
    }
 }


public static void initListView(Context context, ListView listView, String prefix, int numItems, int layout) 
{
    // By using setAdpater method in listview we an add string array in list.
    String[] arr = new String[numItems];
    for (int i = 0; i < arr.length; i++)
    {
        arr[i] = prefix + (i + 1);
    }
    listView.setAdapter(new ArrayAdapter<String>(context, layout, arr));
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {

        }
    });
    } 
 }

我希望你得到你的答案。

于 2013-02-18T07:23:18.580 回答