2012-02-01 11 views
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

और क्या मैं विधि पर अपने कस्टम सेट की बजाय मेजबान गतिविधि में श्रोता पर सेट करने के लिए खंड का ऑनएट() का उपयोग कर सकता हूं? –

+1

ठीक है, एंड्रॉइड एपीआई में सबसे परिष्कृत अधिसूचना प्रणाली ब्रॉडकास्ट रिसीवर का उपयोग कर रही है। आप इस कक्षा का उपयोग कर पारंपरिक जावा इंटरफ़ेस/श्रोता को प्रतिस्थापित कर सकते हैं। अपनी मेजबान गतिविधि में एक रिसीवर पंजीकृत करें और अपने पांच या अधिक टुकड़ों से आप बस प्रसारण भेज सकते हैं। अपने टुकड़ा आप आशय जो sendBroadcast विधि में साथ भेजा जाता है का उपयोग कर सकते के बीच अंतर करने के लिए। इस की जाँच करें: [BroadcastReceiver] (http://developer.android.com/reference/android/content/BroadcastReceiver.html) और शायद कुछ उदाहरण के लिए गूगल, मैं टिप्पणी में मेरी उदाहरण पोस्ट नहीं कर सकते, खुश होती है। –

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> 

-------------------------------------------- 
संबंधित मुद्दे