2013-03-20 19 views
5

तो मैं कुछ दिनों के लिए Android Developer Site about Fragments पर तीसरे ट्यूटोरियल पर फंस गया हूं। जब मैं टैबलेट (ऐप स्क्रीन लेआउट) पर ऐप चलाता हूं तो मैं समझ नहीं पा रहा हूं कि ऐप डेटा को कैसे पॉप्युलेट करता है। मैं समझ सकता हूं कि एक छोटी स्क्रीन (डेटा स्क्रीन) पर डेटा कैसे पॉप्युलेट किया जा रहा है।एंड्रॉइड फ्रैगमेंट बेसिक्स ट्यूटोरियल

बड़ी स्क्रीन सूची डेटा के साथ कैसे पॉप्युलेट करती है?

यहां Android.com ट्यूटोरियल से पूरी परियोजना का एक लिंक है।

MainActivity कक्षा

public class MainActivity extends FragmentActivity 
    implements HeadlinesFragment.OnHeadlineSelectedListener { 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //Here, the system will decide which news_article layout it will use based on the screen size. Will use layout if small or layout-large if it's big. 
     setContentView(R.layout.news_articles); 

     // Check whether the activity is using the layout version with 
     // the fragment_container FrameLayout. If so, we must add the first fragment 
     //This check is to determine which layout to be used, either small screen or big screen. 
     //fragment_container used FrameLayout for small screens. 
     //fragment_container is the id of FrameLayout in news_article for small screen. 
     if (findViewById(R.id.fragment_container) != null) { 

      // However, if we're being restored from a previous state, 
      // then we don't need to do anything and should return or else 
      // we could end up with overlapping fragments. 
      if (savedInstanceState != null) { 
       return; 
      } 

      // Create an instance of ExampleFragment 
      HeadlinesFragment firstFragment = new HeadlinesFragment(); 

      // In case this activity was started with special instructions from an Intent, 
      // pass the Intent's extras to the fragment as arguments 
      firstFragment.setArguments(getIntent().getExtras()); 

      // Add the fragment to the 'fragment_container' FrameLayout 
      getSupportFragmentManager().beginTransaction() 
        .add(R.id.fragment_container, firstFragment).commit(); 
     } 
    } 

    public void onArticleSelected(int position) { 
     // The user selected the headline of an article from the HeadlinesFragment 

     // Capture the article fragment from the activity layout 
     ArticleFragment articleFrag = (ArticleFragment) 
      getSupportFragmentManager().findFragmentById(R.id.article_fragment); 

     if (articleFrag != null) { 
      // If article frag is available, we're in two-pane layout... 

      // Call a method in the ArticleFragment to update its content 
      articleFrag.updateArticleView(position); 

     } else { 
      // If the frag is not available, we're in the one-pane layout and must swap frags... 

      // Create fragment and give it an argument for the selected article 
      ArticleFragment newFragment = new ArticleFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(ArticleFragment.ARG_POSITION, position); 
      newFragment.setArguments(args); 
      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 

      // Replace whatever is in the fragment_container view with this fragment, 
      // and add the transaction to the back stack so the user can navigate back 
      transaction.replace(R.id.fragment_container, newFragment); 
      transaction.addToBackStack(null); 

      // Commit the transaction 
      transaction.commit(); 
     } 
    } 
} 

HeadLineFragment

public class HeadlinesFragment extends ListFragment { 

// The container Activity must implement this interface so the frag can deliver messages 
    public interface OnHeadlineSelectedListener { 
     /** Called by HeadlinesFragment when a list item is selected */ 
     public void onArticleSelected(int position); 
    } 

    OnHeadlineSelectedListener mCallback; 

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

     // We need to use a different list item layout for devices older than Honeycomb 
     int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? 
      android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1; 

     // Create an array adapter for the list view, using the Ipsum headlines array 
     setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines)); 

    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     // When in two-pane layout, set the listview to highlight the selected list item 
     // (We do this during onStart because at the point the listview is available.) 
     if (getFragmentManager().findFragmentById(R.id.article_fragment) != null) { 
      getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     } 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

     // This makes sure that the container activity has implemented 
     // the callback interface. If not, it throws an exception. 
     try { 
      mCallback = (OnHeadlineSelectedListener) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
        + " must implement OnHeadlineSelectedListener"); 
     } 
    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     // Notify the parent activity of selected item 
     mCallback.onArticleSelected(position); 

     // Set the item as checked to be highlighted when in two-pane layout 
     getListView().setItemChecked(position, true); 
    } 
} 

छोटे परदे के लिए लेआउट news_article.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

के लिए bigscreen news_article.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <fragment android:name="com.example.android.fragments.HeadlinesFragment" 
       android:id="@+id/headlines_fragment" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" /> 

    <fragment android:name="com.example.android.fragments.ArticleFragment" 
       android:id="@+id/article_fragment" 
       android:layout_weight="2" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" /> 

</LinearLayout> 
+0

आप इस लिंक उपयोगी http://www.vogella.com/articles/AndroidFragments/article.html –

+0

क्या है HeadLinesFragment है कि आप प्रयोग कर रहे हैं मिल सकता है? – Arqam

उत्तर

5

सूचना दो लेआउट की नियुक्ति लेआउट।

बड़ी स्क्रीन एक गोली बिन (फोल्डर) में res/layout-large/main.xml है, जबकि छोटे परदे लेआउट सामान्य res/layout/main.xml

में है के बाद से जावा पूछता है कि findViewById रिक्त है कि हम जानते हैं इस उपकरण में एक बड़ी स्क्रीन या सामान्य लेआउट है अगर।

ArticleFragment articleFrag = (ArticleFragment) getSupportFragmentManager().findFragmentById(R.id.article_fragment); 
    if (articleFrag != null) { 
     /* not null because we are in res/layout-large */ 
    } else { 
     /* we are in single pain view /res/layout/... */ 
    } 

जब आप फोन setContentView(int); वे प्रणाली सबसे अच्छा लेआउट आप डिवाइस प्रदान की डीपीआई डिब्बे के आधार पर के लिए प्रदान की लोड हो रहा है संभालती है।

+0

उत्तर देने के लिए धन्यवाद। मेरी समस्या यह है कि मैं समझ नहीं पा रहा हूं कि डेटा के साथ बड़ी स्क्रीन कैसे आबादी की जा रही है। मैं मेनएक्टिविटी में एक संदर्भ का पता नहीं लगा सकता हूं कि सेट सेटलिस्ट एडाप्टर (नया ऐरे एडाप्टर (getActivity(), लेआउट, आईपीएसम। हेडलाइंस); क्या मैं कुछ याद कर रहा हूँ? –

+0

'ArticleFragment' लेआउट केवल' TextView' है जो 'ArticalFragment' लाइन 64 से सेट है। सभी दूसरा खंड संबंधित संदेश प्रदर्शित करता है। सरणी एक ही लंबाई दोनों हैं, इसलिए यह संबंधित स्थिति में दूसरी सरणी में संदेश प्रदर्शित करता है – JBirdVegas

+0

हेडलाइंसफ्रैगमेंट के बारे में क्या, यह 2-फलक लेआउट (बड़े लेआउट में कब से डेटा प्राप्त करता है? मेरा मतलब है कि कौन सा कोड पॉप्युलेट करता है –

0

मुझे लगता है, इस सवाल का इस पोस्ट से संबंधित है: http://developer.android.com/training/multiscreen/screensizes.html

नोट: बड़े क्वालीफायर का मतलब है कि लेआउट बड़े (उदाहरण के लिए, 7 "गोलियाँ और ऊपर) के रूप में वर्गीकृत स्क्रीन के साथ उपकरणों पर चयन किया जाएगा। अन्य लेआउट (क्वालिफायर के बिना) छोटे उपकरणों के लिए चयन किया जाएगा;

इसके अलावा आप छोटी स्क्रीन दो फलक लेआउट के लिए उपयोग करने के लिए, निर्देशिका बनाने चाहते हैं:/लेआउट-sw600dp/main.xml रेस;

डेटा जनसंख्या के बारे में: मुख्य गतिविधि में आपने इंटरफ़ेस ऑनहेडलाइन चयन किए गए लिस्टर को कार्यान्वित किया है, जब आप सूची से आइटम (हेडलाइंसफ्रैगमेंट क्लास)
इंटरफ़ेस mCallback कॉल फ़ंक्शन मुख्य एक्टिविटी ऑन आर्टिकल से चयनित (स्थिति) पर क्लिक करते हैं;

इस समारोह में

आप articleFlag

ArticleFragment articleFlag = (ArticleFragment) getSupportFragmentManager() की है। findFragmentById (R.id।article_fragment);

if(articleFlag != null){ 
     //we have 2 panes (big screen) 
articleFlag.updateArticleView(position); 

    }else{ 
     //small screen 
     ArticleFragment newFragment = new ArticleFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ArticleFragment.ARG_POSITION, position); 
     newFragment.setArguments(args); 
     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 

     // Replace whatever is in the fragment_container view with this fragment, 
     // and add the transaction to the back stack so the user can navigate back 
     transaction.replace(R.id.fragment_container, newFragment); 
     transaction.addToBackStack(null); 

     //Commit the transaction 
     transaction.commit(); 

}

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