0

这是我的示例设置

Splashscreen --> Points to Mainactivity --> 2 个选项卡(Tab1,Tab2) 我需要在 Tab1 中扩展一个列表视图。

列表显示

这是我的代码。

从启动画面(工作正常)

protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        Intent i = new Intent(SplashScreen.this, MainActivity.class);
        startActivity(i);
    }

MainActivity.java(工作正常 - 在顶部加载 2 个选项卡

public class MainActivity extends SherlockFragmentActivity {
public static String ACTIVE_TAB = "activeTab";  

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.categories_list);
AppRater.displayAsk2Rate(this, 7, 1, false);

getSupportActionBar().setTitle("Keto Recipes");
getSupportActionBar().setSubtitle("ketozen.com");

final ActionBar actionBar = getSupportActionBar();  
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);  
// add tabs  
Tab tab1 = actionBar  
              .newTab()  
              .setText("Art")  
              .setTabListener(  
                        new TabListener<TabFragment>(this, "tab1",  
                                  TabFragment.class));  
actionBar.addTab(tab1);  
Tab tab2 = actionBar  
              .newTab()  
              .setText("Science")  
              .setTabListener(  
                        new TabListener<TabFragment1>(this, "tab2",  
                                  TabFragment1.class));  
actionBar.addTab(tab2);  
// check if there is a saved state to select active tab  
if (savedInstanceState != null) {  
    getSupportActionBar().setSelectedNavigationItem(  
             savedInstanceState.getInt(ACTIVE_TAB));  

}
}

TabFragment.Java(应该加载 LISTVIEW 的第一个选项卡。不工作

public class TabFragment extends SherlockFragment {  
@Override  
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
// Inflate the layout for this fragment  
View view = inflater.inflate(R.layout.categories_list, container, false);  
               // do your view initialization here  
return view;  
 }  

}

这是我的 categories_list XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lytContent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background">

<com.google.ads.AdView
xmlns:googleads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/ads"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/background"
    googleads:adSize="SMART_BANNER"
    googleads:adUnitId="@string/admob_id"
    android:layout_alignParentBottom="true" />

<ListView
    android:id="@+id/listCategories"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/ads"
    android:layout_below="@+id/lytTitleBar"
    android:divider="@color/separator"
    android:dividerHeight="1dp"
    android:fadeScrollbars="true"
    android:fadingEdge="none"
    android:fastScrollEnabled="true" />

<TextView 
    android:id="@+id/txtAlert"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/alert"
    android:textColor="@color/text"
    android:textSize="14sp"
    android:layout_centerInParent="true"
    android:visibility="gone"/>

请有任何想法。

4

1 回答 1

0

我认为本教程可以帮助您

http://neilgoodman.net/2012/03/12/working-with-fragments-on-android-part-2/

于 2013-06-03T07:50:52.820 回答