0

我想實現片段tabhost,但我不明白什麼是在實現它時這裏的問題。這裏低於實現與自定義列表視圖的片段tabhost

ListTab.java

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

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

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

     mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Today's Deal"), 
     TabFragment.class, null); 
     mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Upcomming Deal"), 
      TabFragment.class, null); 
    } 
} 

TabFragment.java

public class TabFragment extends Fragment { 
    private static final String TAG = "MyActivity"; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
     View V = inflater.inflate(R.layout.tab_fragment, container, false); 
     return V; 
    } 

    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     Log.v(TAG, "Initializing sounds..."); 
     DBHelper dbhelper=new DBHelper(getActivity()); 
     View v = getView(); 
     final ListView listview=(ListView) v.findViewById(R.id.list); 
     final ArrayList<Items> list=new ArrayList<Items>(); 
     ArrayList<Items> itemlist= dbhelper.getAllItem(); 
     Log.v("Query Check", "Working"); 
     for(Items item : itemlist){ 
      //item.getName(); 
      Log.v("Get_FragmentName:", item.getName()); 
      item.getDetail(); 
      Log.v("Get_FragmentDetail:", item.getDetail()); 
      item.getPrice(); 
      Log.v("Get_FragmentPrice:", String.valueOf(item.getPrice())); 
      item.getEndDate(); 
      Log.v("Get_FragmentEndDate:", item.getEndDate()); 
      item.getStartDate(); 
      Log.v("Get_FragmentStartDate:", item.getStartDate()); 
      item.getImage(); 
      list.add(item); 
      ItemAdapter adapter=new ItemAdapter(getActivity(), list); 
      listview.setAdapter(adapter); 
      adapter.notifyDataSetChanged(); 
     } 
    } 
} 

tab_activity.xml

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

    <LinearLayout 
    android:orientation="vertical" 
    android:layout_width="350dp" 
    android:layout_height="80dp"> 

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

的代碼,這是該片段佈局tab_fragment.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" 
    android:layout_marginTop="90dp" 
    tools:context=".DeviceFragment" > 

    <ListView 
    android:id="@+id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:divider="#b5b5b5" 
    android:dividerHeight="1dp" 
    android:listSelector="@drawable/list_selector" /> 
    </LinearLayout> 
+0

問題是什麼? – Farhan

+0

listview不顯示.....只是選項卡有 – hira

+0

後片段XML也PLZ,還檢查是否dbHelper.getAllItem()返回任何項目? – Farhan

回答

0

對於錯誤:

mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Today's Deal"), TabFragment.class, TAG); 

TAG可以「TAB1」或與其相關的任何字符串:「無標籤聞名標籤空」,請在下面的代碼行給一些獨特的標籤名只有這個片段。

  • 從你的XML +活動碼:

    mTab​​Host.setup(ListTab.this,getSupportFragmentManager(),android.R.id tabcontent);

    <的FrameLayout機器人:ID = 「@機器人:ID/tabcontent」 機器人:layout_width = 「0dp」 機器人:layout_height = 「0dp」 機器人:layout_weight = 「」/>

這是我看到的問題,0重量分配,它沒有給任何寬度或高度tabcontent。

相關問題