2013-04-27 5 views
3

나는 단편 1에서 단편 2로 이동하기 위해 슬라이딩 애니메이션을 구현하려고합니다. setCustomAnimations 메소드를 사용하고 있습니다. 그리고 파편을 대체하기 위해 프레임 방법을 사용해야한다는 것을 이해합니다.안드로이드 - 슬라이딩 단편

내 코드 :

package com.example.fragmentss; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 

public class MainActivity extends Activity implements MyListFragment.OnItemSelectedListener{ 

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

    // if the wizard generated an onCreateOptionsMenu you can delete 
    // it, not needed for this tutorial 

    @Override 
    public void onRssItemSelected(String link) { 

    } 

} 

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" > 

    <fragment 
     android:id="@+id/listFragment" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     class="com.example.fragmentss.MyListFragment" ></fragment> 

    <FrameLayout android:id="@+id/details" android:layout_weight="1" 
      android:layout_width="0px" android:layout_height="match_parent" 
      android:background="?android:attr/detailsElementBackground" /> 

</LinearLayout> 

MyListFragment 클래스

package com.example.fragmentss; 
import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 

public class MyListFragment extends Fragment { 

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

    public void onClick2(View view) { 
     switch (view.getId()) { 
     case R.id.button2: 
      FragmentTransaction ft = getFragmentManager().beginTransaction(); 
      ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right); 

      DetailFragment newFragment = DetailFragment.newInstance(); 

      ft.replace(R.id.details, newFragment, "mylistFragment"); 

      // Start the animated transition. 
      ft.commit(); 
      break; 
     } 
    } 
} ~ 

fragment_rssitem_detail.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Goto 2" 
     android:onClick="onClick1"/> 

    <TextView 
     android:id="@+id/detailsText" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="center_horizontal|center_vertical" 
     android:layout_marginTop="20dip" 
     android:text="Frag 1" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="30dip" /> 

</LinearLayout> 

fragment_rsslist_overview.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Goto 1" 
     android:onClick="onClick2" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="256dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.18" 
     android:text="Frag 2" 
     android:textSize="30dip"/> 

</LinearLayout> 

답변

5

나는 당신이 아주 쉽게 ViewPager

ViewPager에 의해 구현 될 수있다 구현 하려는지 짐작이 조각 사이 쉽고 부드러운 스위칭을 구현하기 위해 도입 된 사용자 정의 구성 요소입니다. 안드로이드 문서는 우선은 또한 Example/Tutorial for ViewPager and FragmentsHow to use Android ViewPager?

대답 한 비슷한 질문을 볼 수 있습니다 http://developer.android.com/training/animation/screen-slide.html에서 봐 소용 이해하는 아주 좋은 이해하기 쉬운 예제가 실려있다
관련 문제