listfragment overlapping my main drawer

2020-05-03 11:44发布

问题:

im new in android im creating an app and i have a problem with a listfragment, because the list its showing but its overlapping the title bar(i had to add margin top to change that), and also im using a drawer and when im trying to open the list it shows over the options drawer too, letme paste you the code and the image so you can understan more:

The xml code:

    <?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>

The listFregment code:

  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 {

                }
            }
        });

    }
}

the main drawer code:

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) {

    }
}

list fragment overlapping

here its the 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>

here its the 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>

回答1:

your problem is your setting your list fragment to the whole view Id with this line

// android.R.id.content is the WHOLE screen of your Activity
transaction.add(android.R.id.content,fragmento,"fragmento");
transaction.commit();

Create a FrameLayout in your content_drawer_principal.xml:

<FrameLayout android:id="@+id/list_content"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>

then do:

transaction.add(R.id.list_content,fragmento,"fragmento");
transaction.commit();

UPDATE

The real problem here is that your telling your FragmentTransaction to load your FragmentoPrincipalChofer into android.R.id.content which is a reserved id

android.R.id.content gives you the root element of a view, without having to know its actual name/type/ID.



回答2:

Revers the order of your activity layout, put list view brfore any other main content