28

के साथ टैबहोस्ट मैं एंड्रॉइड ऐप पर काम कर रहा हूं और मैं प्रत्येक टैब के लिए फ्रैगमेंट्स का उपयोग करके नेविगेशन के लिए 3 टैब का उपयोग करना चाहता हूं, लेकिन मुझे नहीं पता कि इसे करने के लिए संरचना कैसे बनाएं।फ्रैगमेंट्स और फ्रैगमेंट एक्टिविटी

मैं प्रत्येक टुकड़ा अलग से जोड़ना चाहता हूं क्योंकि प्रत्येक एक अलग है, लेकिन मुझे नहीं पता कि उन्हें फ्रैगमेंट एक्टिविटी में कहां जोड़ना है।

मेरे पास ये फ़ाइलें हैं।

tabs_layout.xml

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

    <TabHost android:id="@android:id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" > 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
      /> 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 

      > 

       <FrameLayout 
        android:id="@+id/tabRateAPet" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 

       /> 

       <FrameLayout 
        android:id="@+id/tabViewMyRates" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 

       /> 

       <FrameLayout 
        android:id="@+id/tabViewGlobalRates" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 

       /> 


      </FrameLayout> 
     </LinearLayout> 
    </TabHost> 
</LinearLayout> 

TabsMain.java

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 

public class MainTabsActivity extends FragmentActivity { 
public static final String RATE_A_PET = "Rate a Pet"; 
public static final String MY_RATES = "My Rates"; 
public static final String GLOBAL_RATES = "Global Rates"; 

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

Tabs.java

import android.app.Activity; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TabHost; 
import android.widget.TabHost.OnTabChangeListener; 
import android.widget.TabHost.TabSpec; 
import android.widget.TextView; 

public class Tabs extends Fragment implements OnTabChangeListener { 
    private static final String TAG = "FragmentTabs"; 
    public static final String RATE_A_PET = "Rate a Pet"; 
    public static final String MY_RATES = "My Rates"; 
    public static final String GLOBAL_RATES = "Global Rates"; 

    private View mRoot; 
    private TabHost mTabHost; 
    private int mCurrentTab; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
//  super.onCreateView(inflater, container, savedInstanceState); 
     mRoot = inflater.inflate(R.layout.tabs_layout, null); 
     mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost); 
     setupTabs(); 
     return mRoot; 
    } 

    private void setupTabs() { 
     mTabHost.setup(); // important! 
     mTabHost.addTab(newTab(RATE_A_PET, R.string.tabRateAPet, R.id.tabRateAPet)); 
     mTabHost.addTab(newTab(MY_RATES, R.string.tabViewMyRates, R.id.tabViewMyRates)); 
    } 

    private TabSpec newTab(String tag, int labelId, int tabContentId) { 
     Log.d(TAG, "buildTab(): tag=" + tag); 

     View indicator = LayoutInflater.from(getActivity()).inflate(
       R.layout.tab, 
       (ViewGroup) mRoot.findViewById(android.R.id.tabs), false); 
     ((TextView) indicator.findViewById(R.id.text)).setText(labelId); 

     TabSpec tabSpec = mTabHost.newTabSpec(tag); 
     tabSpec.setIndicator(indicator); 
     tabSpec.setContent(tabContentId); 
     return tabSpec; 
    } 


    @Override 
    public void onTabChanged(String tabId) { 
     Log.d(TAG, "onTabChanged(): tabId=" + tabId); 
     if (RATE_A_PET.equals(tabId)) { 
      updateTab(tabId, R.id.tabRateAPet); 
      mCurrentTab = 0; 
      return; 
     } 
     if (MY_RATES.equals(tabId)) { 
      updateTab(tabId, R.id.tabViewMyRates); 
      mCurrentTab = 1; 
      return; 
     } 
     if (GLOBAL_RATES.equals(tabId)) { 
      updateTab(tabId, R.id.tabViewGlobalRates); 
      mCurrentTab = 2; 
      return; 
     } 
    } 
    private void updateTab(String tabId, int placeholder) { 
     FragmentManager fm = getFragmentManager(); 
     if (fm.findFragmentByTag(tabId) == null) { 
      fm.beginTransaction() 
        .replace(placeholder, new RateMyPetActivity(), tabId) 
        .commit(); 
     } 
    } 

} 
+0

इस ट्यूटोरियल देख सकते हैं और यदि आप पाते हैं किसी भी difficulty.u मेरे लिए पूछ सकते हैं ... http: // wptrafficanalyzer: //manishkpr.webheavens.com/android-viewpager-example/ – TheFlash

+0

तुम भी this.http उल्लेख कर सकते हैं। इन/ब्लॉग/जोड़-नेविगेशन-टैब-युक्त-सूचीदृश्य-टू-एक्शन-बार-इन-एंड्रॉइड/ – Shadow

उत्तर

73

मैं प्रत्येक टैब के लिए एक अलग टुकड़ा फ़ाइल बनाने का सुझाव देते हैं। मैं हाल ही में इस रूप में अच्छी तरह से किया था, इसलिए मैं अपने कोड के नीचे रेखांकित किया है:

लेआउट फ़ाइलें

activity_main.xml

<android.support.v4.app.FragmentTabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TabWidget 
     android:id="@android:id/tabs" 

     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"/> 

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="0"/> 

    <FrameLayout 
     android:id="@+id/realtabcontent" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 

</LinearLayout> 
</android.support.v4.app.FragmentTabHost> 

tab1_view.xml // इस प्रारूप का उपयोग अपने-अपने टैब लेआउट जोड़ने (स्ट्रिंग चर बदलने के लिए सुनिश्चित करें)

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

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/tab1_fragment_string" /> 

</LinearLayout> 

एसआरसी फ़ाइलें

MainActivity.java // नोटिस कि .addTab प्रक्रिया में मैंने केवल टेक्स्ट का उपयोग किया था। आप ड्रॉबल्स का उपयोग करके आइकन भी जोड़ सकते हैं जिन्हें आपको अपने एचडीपीआई फ़ोल्डर में जोड़ना होगा। मैंने इस उदाहरण में केवल तीन टैब बनाए हैं।

package com.example.applicationname; 

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentTabHost; 

public class MainActivity extends FragmentActivity { 
    // Fragment TabHost as mTabHost 
    private FragmentTabHost mTabHost; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost); 
     mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); 

     mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"), 
      Tab1Fragment.class, null); 
     mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"), 
      Tab2Fragment.class, null); 
     mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab3"), 
      Tab3Fragment.class, null); 
    } 
} 

Tab1Fragment.java // एक बार फिर से टैब की वांछित संख्या के लिए दोहराने

package com.example.applicationname; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class Tab1Fragment extends Fragment { 

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View V = inflater.inflate(R.layout.tab1_view, container, false); 

     return V; 
    } 
} 

सुनिश्चित करें कि आपके R.java और strings.xml फ़ाइलों को ठीक से स्थापित कर रहे हैं, और उसके बाद अपने टैब होना चाहिए बनाओ अभी भी अच्छा चल रहा है।

+0

ठीक है। मैं इसे देखने के लिए जाऊंगा। धन्यवाद। मैं बाद में आपको यह कहने जा रहा हूं कि यह मेरे लिए काम करता है :) – imarban

+1

आपका स्वागत है! खुश है इससे मदद मिली –

+0

क्या हम टैब को आईओएस की तरह नीचे ले जा सकते हैं? – Dharmendra

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