交易期间的片段动画 - 只有左侧幻灯片在工作中而右侧幻灯片不在

时间:2013-07-01 06:44:35

标签: android animation fragment

我在交易期间进行片段动画,但我只能进行左侧滑动。右侧幻灯片动画不会影响任何内容。 请帮助:我已经尝试了所有内容并搜索了谷歌和其他stackoverflow问题,并做了他们建议的所有事情。动画中的右侧幻灯片没有做任何事情。

以下是片段活动:

public class MainActivity extends FragmentActivity {
    static FragmentManager manager;

    String arr1[] = { "One", "Two", "Three", "Four", "Five", "Six", "Seven" };
    String arr2[] = { "One2", "Two2", "Three2", "Four2", "Five2", "Six2",
            "Seven2" };
    String arr3[] = { "One23", "Two23", "Three23", "Four23", "Five23", "Six23",
            "Seven23" };

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

        manager = getSupportFragmentManager();
        StartTransaction(arr1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public static void StartTransaction(String arr[]) {
        FragmentOne one = new FragmentOne();
        Bundle b = new Bundle();
        b.putStringArray("array", arr);
        one.setArguments(b);
        FragmentTransaction transaction = manager.beginTransaction();

        // PROblem is here

        transaction.setCustomAnimations(R.anim.slide_right, R.anim.slide_left);// ,R.anim.slide_right,R.anim.slide_left);
        transaction.replace(R.id.container, one);
        // transaction.addToBackStack(null);
        transaction.commit();
    }

    private void MoveOneLevelUp() {
        FragmentOne one = new FragmentOne();
        Bundle b = new Bundle();
        b.putInt("moveup", 1);
        one.setArguments(b);
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.setCustomAnimations(R.anim.slide_left, R.anim.slide_right,
                R.anim.slide_right, R.anim.slide_left);
        transaction.replace(R.id.container, one);
        transaction.addToBackStack(null);
        transaction.commit();
    }
}

这是片段代码:

public class FragmentOne extends Fragment implements OnItemClickListener {

    Activity activity;
    ListView list;
    View v;
    String arr1[] = {"One","Two","Three","Four","Five","Six","Seven"};
    String arr2[] = { "One2", "Two2", "Three2", "Four2", "Five2", "Six2",
    "Seven2" };
    String arr3[] = { "One23", "Two23", "Three23", "Four23", "Five23", "Six23",
    "Seven23" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        activity = getActivity();

        String array[] = getArguments().getStringArray("array");

        v = LayoutInflater.from(activity).inflate(R.layout.first_view, null);

        list = (ListView)v.findViewById(R.id.listview);
        list.setOnItemClickListener(this);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array);
        list.setAdapter(adapter);
        return v;
        // return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        if(position ==2) {
            MainActivity.StartTransaction(arr2);
        } else {
            MainActivity.StartTransaction(arr3);
        }
    }
}

向左滑动动画xml:

        <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="700"
        android:fromXDelta="-100%"
        android:toXDelta="0%" >
    </translate>
</set>

向右滑动xml:

<?xml version="1.0" encoding="utf-8"?>`enter code here`
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="700"
        android:fromXDelta="0%"
        android:toXDelta="100%" >
    </translate>
</set>

1 个答案:

答案 0 :(得分:2)

您需要一个片段交易。

尝试这样的事情:

if (mLastMenuWeight > mMenuWeight) {
    transaction.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
}
else {
    transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
}

请记住,我使用mLastMenuWeight和mMenuWeight来跟踪我应该使用哪个动画。您需要用其他东西替换这个条件。

由于android仅包含从左到右的动画,因此您必须使用原始动画代码自行定义从右到左。像这样:

slide_in_right.xml:

<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/res/anim/slide_in_left.xml
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/
-->

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="50%p" android:toXDelta="0"
            android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

slide_out_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/res/anim/slide_out_right.xml
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/
-->

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-50%p"
            android:duration="@android:integer/config_mediumAnimTime"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

此外,您可以尝试添加/附加新视图并分离旧视图而不是替换。也许它会产生相同的行为...