2014-05-19 95 views
0

我有一個ListViewViewPager片段,它當前應該由一個字符串數組填充,但它是完全空白的。下面是片段代碼:ViewPager片段中的ListView空白

public class DirectoryFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_directory, container, false); 

     ListView listView = (ListView) rootView.findViewById(R.id.list); 

     String[] values = new String[] {"Viewer 1", 
             "Viewer 2", 
             "Viewer 3" 
             }; 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, values); 

     listView.setAdapter(adapter); 

     return rootView; 
    } 

} 

碎片的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#ff8400" > 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_marginTop="20dp"> 
    </ListView> 


</RelativeLayout> 

而且在MainActivity:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener { 

    private ViewPager viewPager; 
    private TabsPagerAdapter mAdapter; 
    private ActionBar actionBar; 

    //Tab Titles 
    private String[] tabs = { "sfn Viewer", "Directory", "Help" }; 

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

     //Initialization 
     viewPager = (ViewPager) findViewById(R.id.pager); 
     actionBar = getActionBar(); 
     mAdapter = new TabsPagerAdapter(getSupportFragmentManager()); 

     viewPager.setAdapter(mAdapter); 
     actionBar.setHomeButtonEnabled(false); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     //Adding Tabs 
     for (String tab_name : tabs) { 
      actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this)); 
     } 

     viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 
      @Override 
     public void onPageSelected(int position) { 
       //On changing page, make sure right tab is selected 
       actionBar.setSelectedNavigationItem(position); 
      } 

      @Override 
      public void onPageScrolled(int arg0, float arg1, int arg2) {} 

      @Override 
      public void onPageScrollStateChanged(int arg0) {} 
     }); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
     //When the tab is selected show the correct fragment 
     viewPager.setCurrentItem(tab.getPosition()); 
    } 

    @Override 
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 

    } 

    @Override 
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 

    } 
} 

我在我的DirectoryFragment標籤很期待的帶有查看器1,查看器2和查看器3的字符串ListView

提前致謝!

回答

1

您的ListViewlayout_height0dp以XML格式。將其更改爲match_parent並刪除layout_weight,因爲這對RelativeLayout沒有任何影響。

+0

這真是一個荒謬的菜鳥錯誤。謝謝! –