Navigation Drawer

0

Good evening, I have a problem that I can not solve. I used an adapter to make a list of items, but "item 1" is under the bar (will understand looking at the photo). But I do not know how to solve it. I will pass the code from my navigation.

NavigationDrawer:

publicclassMainActivityextendsAppCompatActivityimplementsNavigationView.OnNavigationItemSelectedListener{NavigationViewnavigationView=null;privateToolbartoolbar;privatestaticfinalintTELA_SOBRE=1;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//SetthefragmentinitiallyMainFragmentfragment=newMainFragment();FragmentTransactiontransaction1=getFragmentManager().beginTransaction();transaction1.replace(R.id.fragment_container,fragment);transaction1.commit();toolbar=(Toolbar)findViewById(R.id.toolbar);setSupportActionBar(toolbar);FloatingActionButtonfab=(FloatingActionButton)findViewById(R.id.fab);fab.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){chamarDialog();}});DrawerLayoutdrawer=(DrawerLayout)findViewById(R.id.drawer_layout);ActionBarDrawerToggletoggle=newActionBarDrawerToggle(this,drawer,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);drawer.setDrawerListener(toggle);toggle.syncState();navigationView=(NavigationView)findViewById(R.id.nav_view);//HowtochangeelementsintheheaderprogramaticallyViewheaderView=navigationView.getHeaderView(0);TextViewemailText=(TextView)headerView.findViewById(R.id.email);emailText.setText("[email protected]");

    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.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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

Adapter class:

public class AcessFragment extends Fragment implements AdapterView.OnItemClickListener {
private ListView lista;

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

    lista = (ListView) rootView.findViewById(R.id.lista);
    String[] listaOpcoes = {"item 1", "item 2", "item 3", "item 4"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
            android.R.layout.simple_list_item_1,
            android.R.id.text1, listaOpcoes);
    // adapter.clear(); Limpar adapter
    // adapter.addAll(listaOpcoes); Adiciona vários
    // adapter.add("Nova String"); Adiciona uma String

    lista.setAdapter(adapter);
    lista.setOnItemClickListener(this);

    return rootView;
}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    //String itemSelecionado = (String) this.lista.getAdapter().getItem(position);
    //Toast.makeText(this, "Item selecionado: " + itemSelecionado, Toast.LENGTH_LONG).show();

    if (position == 0) {

    } else if (position == 1) {

    } else if (position == 2) {

    } else if (position == 3) {


    }


}

adapter xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
    android:id="@+id/lista"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></ListView>

activity_xml:

<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_main"
    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_main"
    app:menu="@menu/activity_main_drawer" />

xml app_bar_main

<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="br.com.projeto.caminhossembarreiras.MainActivity">

<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" />

</android.support.design.widget.AppBarLayout>

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

</FrameLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="58dp"
        android:layout_marginLeft="@dimen/fab_margin"
        android:layout_marginRight="@dimen/fab_margin"
        android:src="@drawable/ic_add_location" />
</RelativeLayout>

    
asked by anonymous 28.04.2016 / 07:29

2 answers

2

The problem happens because you are using a relative layout with the ListView inside, in which case the list will start from the top.

Since you only have one list inside the layout, a simple way to solve it is to change your RelativeLayout to a LinearLayout, and everything will be fine.

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical">

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

</LinearLayout>
    
28.04.2016 / 12:50
1

If you change android.support.v4.widget.DrawerLayout to RelativeLayout . You can use the android:layout_below="@id/id_app_bar" tag (you will need to create an app_bar's include pro id).

Another way, but I do not know if it will work is:

<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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/nav_view"/>

<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_main"
    app:menu="@menu/activity_main_drawer"
    />

In this case I used layout_above because your app_bar xml is defined as relative layout.

    
29.04.2016 / 05:21