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>
問題是什麼? – Farhan
listview不顯示.....只是選項卡有 – hira
後片段XML也PLZ,還檢查是否dbHelper.getAllItem()返回任何項目? – Farhan