列表片段与我的主抽屉重叠
listfragment overlapping my main drawer
我是 android 中的新手 我正在创建一个应用程序,但我遇到了一个列表片段问题,因为它显示了列表但它与标题栏重叠(我必须添加 margin top 来更改它),并且我也在使用抽屉,当我试图打开列表时,它也显示在选项抽屉上,让我把代码和图像粘贴给你,这样你就可以了解更多:
xml代码:
<?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"
android:weightSum="1">
<ListView
android:layout_width="match_parent"
android:layout_height="420dp"
android:id="@android:id/list"
android:layout_weight="0.82"
android:layout_marginTop="60dp" />
</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(android.R.id.list);
return x;
}
@Override
public void onResume() {
super.onResume();
//obtener pedido de taxi
ParseQuery<ParseObject> query = ParseQuery.getQuery("Viaje");
query.whereEqualTo("chofer", "prueba3");
query.addDescendingOrder("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> viajes, com.parse.ParseException e) {
if (e == null) {
mViaje=viajes;
String[] nombreUsuarios = new String[mViaje.size()];
int i=0;
for (ParseObject viaje : mViaje){
nombreUsuarios[i] = viaje.getString("cliente");
i++;
}
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1,nombreUsuarios);
mLista.setAdapter(adaptador);
} else {
}
}
});
}
}
主抽屉代码:
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();
}
/*
FragmentoPrincipalUsuario principal = new FragmentoPrincipalUsuario();
android.support.v4.app.FragmentTransaction FragmentTransaction =
getSupportFragmentManager().beginTransaction();
FragmentTransaction.replace(R.id.contenedor_principal,principal);
FragmentTransaction.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);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pantalla_principal_usuario, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.cuenta) {
// Handle the camera action
} else if (id == R.id.historial_viajes) {
} else if (id == R.id.contacto) {
} else if (id == R.id.compartir) {
} else if (id == R.id.version) {
} else if (id == R.id.salir) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
这里是 activity_drawer_principal.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_drawer_principal"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_drawer_principal"
app:menu="@menu/activity_pantalla_principal_usuario_drawer" />
</android.support.v4.widget.DrawerLayout>
这里是 app_bar_drawer_principal:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".DrawerPrincipal">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<FrameLayout
android:id="@+id/contenedor_principal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<include layout="@layout/content_drawer_principal" />
</android.support.design.widget.CoordinatorLayout>
颠倒 activity 布局的顺序,将列表视图放在任何其他主要内容之前
您的问题是您使用此行将列表片段设置为整个视图 ID
// android.R.id.content is the WHOLE screen of your Activity
transaction.add(android.R.id.content,fragmento,"fragmento");
transaction.commit();
在您的 content_drawer_principal.xml:
中创建一个 FrameLayout
<FrameLayout android:id="@+id/list_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
然后做:
transaction.add(R.id.list_content,fragmento,"fragmento");
transaction.commit();
更新
这里真正的问题是你告诉你的 FragmentTransaction 将你的 FragmentoPrincipalChofer
加载到 android.R.id.content
中,这是一个保留的 id
android.R.id.content gives you the root element of a view, without having to know its actual name/type/ID.
我是 android 中的新手 我正在创建一个应用程序,但我遇到了一个列表片段问题,因为它显示了列表但它与标题栏重叠(我必须添加 margin top 来更改它),并且我也在使用抽屉,当我试图打开列表时,它也显示在选项抽屉上,让我把代码和图像粘贴给你,这样你就可以了解更多:
xml代码:
<?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"
android:weightSum="1">
<ListView
android:layout_width="match_parent"
android:layout_height="420dp"
android:id="@android:id/list"
android:layout_weight="0.82"
android:layout_marginTop="60dp" />
</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(android.R.id.list);
return x;
}
@Override
public void onResume() {
super.onResume();
//obtener pedido de taxi
ParseQuery<ParseObject> query = ParseQuery.getQuery("Viaje");
query.whereEqualTo("chofer", "prueba3");
query.addDescendingOrder("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> viajes, com.parse.ParseException e) {
if (e == null) {
mViaje=viajes;
String[] nombreUsuarios = new String[mViaje.size()];
int i=0;
for (ParseObject viaje : mViaje){
nombreUsuarios[i] = viaje.getString("cliente");
i++;
}
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1,nombreUsuarios);
mLista.setAdapter(adaptador);
} else {
}
}
});
}
}
主抽屉代码:
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();
}
/*
FragmentoPrincipalUsuario principal = new FragmentoPrincipalUsuario();
android.support.v4.app.FragmentTransaction FragmentTransaction =
getSupportFragmentManager().beginTransaction();
FragmentTransaction.replace(R.id.contenedor_principal,principal);
FragmentTransaction.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);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pantalla_principal_usuario, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.cuenta) {
// Handle the camera action
} else if (id == R.id.historial_viajes) {
} else if (id == R.id.contacto) {
} else if (id == R.id.compartir) {
} else if (id == R.id.version) {
} else if (id == R.id.salir) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
这里是 activity_drawer_principal.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_drawer_principal"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_drawer_principal"
app:menu="@menu/activity_pantalla_principal_usuario_drawer" />
</android.support.v4.widget.DrawerLayout>
这里是 app_bar_drawer_principal:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".DrawerPrincipal">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<FrameLayout
android:id="@+id/contenedor_principal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<include layout="@layout/content_drawer_principal" />
</android.support.design.widget.CoordinatorLayout>
颠倒 activity 布局的顺序,将列表视图放在任何其他主要内容之前
您的问题是您使用此行将列表片段设置为整个视图 ID
// android.R.id.content is the WHOLE screen of your Activity
transaction.add(android.R.id.content,fragmento,"fragmento");
transaction.commit();
在您的 content_drawer_principal.xml:
中创建一个 FrameLayout<FrameLayout android:id="@+id/list_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
然后做:
transaction.add(R.id.list_content,fragmento,"fragmento");
transaction.commit();
更新
这里真正的问题是你告诉你的 FragmentTransaction 将你的 FragmentoPrincipalChofer
加载到 android.R.id.content
中,这是一个保留的 id
android.R.id.content gives you the root element of a view, without having to know its actual name/type/ID.