我是新的Android即时通讯创建一个应用程序,我有一个listfragment的问题,因为它显示的列表,但它重叠标题栏(我不得不添加边缘顶部来改变它),而且我使用抽屉和当我试图打开它也显示在选项抽屉上的列表时,letme会粘贴你的代码和图像,这样你就可以了解更多信息:
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>
listFregment代码:
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>
答案 0 :(得分:1)
您的问题是您使用此行将列表片段设置为整个视图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为您提供视图的根元素,而无需知道其实际名称/类型/ ID。
答案 1 :(得分:0)
撤消活动布局的顺序,将列表视图放在任何其他主要内容之前