2015-12-14 36 views
0

我是新的android工作室,我试图创建一个带有列表的片段并显示它在我用来显示我所有片段的抽屉类上,我已经阅读了很多帖子,但找不到我要找的答案,让我粘贴您的代码,以便您可以检查它。你的内容必须有一个ListView的ID属性是'android.R.id.list'即时通讯尝试创建一个列表片段

布局代码:

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

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/lista" 
     android:layout_gravity="center_horizontal" /> 
</LinearLayout> 

片段类代码:

public class FragmentoPrincipalChofer extends ListFragment { 
    private List<ParseObject> mViaje; 
    private ListView mLista; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View x = inflater.inflate(R.layout.fragmento_principal_chofer, container, false); 
     mLista = (ListView)x.findViewById(R.id.lista); 
     String[] nombres= new String[2]; 
     nombres[0]="gaston"; 
     nombres[1]="gaston"; 
     ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, nombres); 
     mLista.setAdapter(adaptador); 

     return x; 

    } 

抽屉类代码:

public class DrawerPrincipal extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener,FragmentoPrincipalUsuario.OnFragmentInteractionListener { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_drawer_principal); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    //cargar fragment principal usuario 

    FragmentoPrincipalChofer fragmento=(FragmentoPrincipalChofer) getSupportFragmentManager().findFragmentByTag("fragmento"); 
    if (fragmento==null){ 
     fragmento=new FragmentoPrincipalChofer(); 
     android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
     transaction.add(android.R.id.content,fragmento,"fragmento"); 
     transaction.commit(); 

    } 


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
} 

回答

0

您使用的是ListFragment所以...

ListFragment具有由单个列表视图组成的默认布局。但是,如果您愿意,您可以通过从onCreateView(LayoutInflater,ViewGroup,Bundle)返回自己的视图层次来自定义片段布局。要做到这一点,您的视图层次结构必须包含的ID的ListView控件对象“@android:ID /列表”(或列表,如果它在代码)

更改ListView XML以下

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@android:id/list" 
    android:layout_gravity="center_horizontal" /> 

而且你提到它是这样

mLista = (ListView)x.findViewById(android.R.id.list); 
+0

嗨安德鲁非常感谢您前面回答现在它的工作,但我有一个问题,它的重叠标题栏的列表,你知道我怎么能查那是什么?非常感谢。 –

+0

你能提供一张照片吗?尽管 –

+0

可以,但在另一个问题中问你可能会更好。 –

相关问题