7

여러 조각을 보유하고 조각 사이를 탐색하는 하나의 활동을 사용하고 싶습니다. 예를 들어, 액티비티에는 조각 인 목록 뷰가 있습니다. 사용자가 목록에서 항목 하나를 선택하면 뷰가 다른 조각으로 이동합니다. 어떻게 구현할 수 있습니까?하나의 활동 내에서 조각 간 이동

개발자 사이트에는 tutorial이라는 멋진 정보가 있지만, 하나의 목록 조각과 하나의 세부적인 조각이 한 화면에 표시되는 두 개의 창 레이아웃이있는 태블릿 화면을 처리합니다. 한 화면에 두 개의 조각이 표시되지 않고 조각 사이를 탐색하기를 원합니다.

튜토리얼은 저에게 방법을 가르쳐 줄 수 있습니까?

+0

하지만 자습서는 인물 사진 레이아웃 만 보는 경우 완벽하게 유효합니다. – Warpzit

+0

@ Warpzit,하지만 자습서에서는 세로 레이아웃으로 두 개의 액티비티를 만들 것을 권합니다. 각 액티비티마다 하나씩 있습니다. –

답변

11

간단히 말해서 질문에 대한 대답은 호스트 활동을 알리고 FragmentManager를 사용하여 현재의 조각 컨테이너를 호스트 활동으로 대체하는 것입니다.

첫 번째 단편에 인터페이스를 만들고 호스트 활동을이 인터페이스에 등록/수신 (구현) 한 다음 FragmentManager를 사용하여 컨테이너 내용을 수신기 콜백의 두 번째 조각으로 바꿉니다.

내가 여기에 튜토리얼에 대한 모르겠지만 내 조각입니다 : 첫 번째 조각

public class First extends Fragment{ 
private static onMySignalListener listener; 

//call this function from whatever you like i.e button onClickListener 
public void switchWindow() { 
    if(listener != null){ 
     listener.onMySignal(); 
    } 
} 

public interface onMySignalListener { 
    //customize this to your liking 

    //plain without argument 
    void onMySignal(); 

    //with argument 
    void onMySignalWithNum(int mNum); 
} 

public static void setOnMySignalListener(onMySignalListener listener) { 
    First.listener = listener; 
}} 

호스트 활동

public class HostActivity extends FragmentActivity implements onMySignalListener{ 
private final String ADD_TAG_IF_NECESSARY = "mTag"; 

@Override 
public void onCreate(Bundle ssi) { 
    setContentLayout(R.layout.main); 

    FirstFragment.setOnMySignalListener(this); 
} 

@Override 
public void onMySignal() { 
    //if you're using compat library 
    FragmentManager manager = getSupportFragmentManager(); 
    FragmentTransaction transaction = manager.beginTransaction(); 

    //initialize your second fragment 
    sfragment = SecondFragment.newInstance(null); 
    //replace your current container being most of the time as FrameLayout 
    transaction.replace(R.id.container, fragment, ADD_TAG_IF_NECESSARY); 
    transaction.commit(); 
} 

@Override 
public void onMySignalWithNum(int mNum) { 
    //you can do the same like the above probably with your own customization 
}} 

이, 친절하게 당신이 인터페이스를 구현하는 것입니다 방법에 대한 예에 불과 너 자신에 의해 그것을 정돈해라. 그리고 호스트 활동에 대해 알리고 자하는 많은 프래그먼트가있는 경우이 방법은 효과가 없습니다. 그렇게하면 호스트 활동에 다양한 청취자를 구현하게됩니다.

+0

안녕하세요, 많은 조각 (약 5)이 내 호스트 활동을 알리고 싶다면 어떻게 구현해야합니까? –

+0

그리고 프래그먼트의 onAttach()를 사용하여 메소드의 사용자 정의 세트 대신 호스트 활동의 리스너를 설정할 수 있습니까? –

+1

글쎄, 안드로이드 API에서 가장 정교한 알림 시스템은 BroadcastReceiver를 사용하고 있습니다. 이 클래스를 사용하여 기존 Java 인터페이스/수신기를 바꿀 수 있습니다. 귀하의 호스트 활동에 수신기를 등록하십시오. 즉, 단순히 방송을 보낼 수있는 5 개 이상의 조각을 등록하십시오. 조각 사이를 구별하기 위해 sendBroadcast 메서드에서 전송되는 Intent를 사용할 수 있습니다. 이것을 확인해보십시오 : [BroadcastReceiver] (http://developer.android.com/reference/android/content/BroadcastReceiver.html) 그리고 아마도 몇 가지 예를 들어 google을 올려주세요. 코멘트에 코멘트를 올릴 수 없습니다. –

5
I think this will be useful for you. 

example of two fragments in one screen works independently 
=========================================================== 

public class MainActivity extends Activity { 

    enter code here 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_activity); 

     Fragment newFragment = new Test(); 
     FragmentTransaction transaction = getFragmentManager() 
       .beginTransaction(); 
     transaction.add(R.id.UprLayout, newFragment); 
//  transaction.addToBackStack(null); 
     transaction.commit(); 

     Fragment newFragment2 = new TestRight(); 
     FragmentTransaction transaction2 = getFragmentManager() 
       .beginTransaction(); 
     transaction2.add(R.id.dwnLayout, newFragment2); 
//  transaction.addToBackStack(null); 
     transaction2.commit(); 


    } 
} 

----------------------------- 
main_activity layout 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout2" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center|center_horizontal" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/UprLayout" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/dwnLayout" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

    </LinearLayout> 

</LinearLayout> 

------------------------------------ 
public class Test extends Fragment { 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
    } 

TextView tv; 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.test, container, false); 
     return view;   
    } 
} 
--------------------------- 

public class TestRight extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.test_right, container, false); 
     return view; 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     Button button = (Button)getActivity().findViewById(R.id.button1); 
     button.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       Fragment newFragment = new Right2nd(); 
       FragmentTransaction transaction = getFragmentManager() 
         .beginTransaction(); 
       transaction.replace(R.id.dwnLayout, newFragment); 
       transaction.addToBackStack("aa"); 
       transaction.commit(); 

//    transaction.add(R.id.frag, newFragment).commit(); 
      } 
     }); 
    } 

} 
------------------------------- 
test layout 

<?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:gravity="center" 
    android:orientation="vertical" > 
    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="test" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="50sp" /> 

</LinearLayout> 
------------------------------------ 
test_right layout 

<?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:gravity="center" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Click" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Test right" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="45sp" /> 

</LinearLayout> 
------------------------------------- 
public class Right2nd extends Fragment{ 

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

<?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:gravity="center" 
    android:orientation="vertical" > 


    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Right 2nd" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="50sp" /> 

</LinearLayout> 

--------------------------------------------