- Forum-Beiträge: 3
08.01.2015, 18:03:02 via Website
08.01.2015 18:03:02 via Website
Hej ho ich habe in der letzten Zeit eine App programmiert mit einem Navigation Drawer, wo auf der Linken Seite eine ListView ist, wo die einzelnen Items drinne sind. Das funktioniert auch schon. Das Problem ist jetzt, dass ich untermenüs brauche. Daraufhin habe ich gegoogelt und bin auf Expandable ListView gestoßen. Ich probiere jetzt schon seit Tagen rum, bekomme es aber einfach nicht auf die Reihe. Jetzt bin ich auf die Idee gekommen, einfach eine zweite ListView auf der RechtenSeite zu machen, die dynamisch mit den auf der linkenSeite geklickten Items funktioniert. Jetzt ist aber wieder das Problem, dass ich überhaupt keinen Ansatz dafür habe, besonders weil ich nichteinmal weiß, wie ich z.B. einen zweiten ActionBarDrawerToggle mache und den auf die rechte Seite bekomme. Vielleicht könnt ihr mir ja ein paar Ansätze zeigen oder mir allgemein Helfen
Hier einmal meine
MainActivity.java
package com.bs_eutin.codex.bs_eutin;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity
{
DrawerLayout mDrawerLayout;
ListView mDrawerList;
ListView mDrawerListRight;
ActionBarDrawerToggle mDrawerToggle;
String[] mDrawerListItems;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer);
mDrawerListRight = (ListView)findViewById(R.id.rightList);
mDrawerList = (ListView)findViewById(android.R.id.list);
mDrawerListItems = getResources().getStringArray(R.array.drawer_list);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDrawerListItems));
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
FragmentManager fragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
int editedPosition = position+1;
if(editedPosition == 1)
{
FragmentNews fragmentNews = new FragmentNews();
fragmentTransaction.replace(R.id.content_frame, fragmentNews);
fragmentTransaction.commit();
}
if(editedPosition == 2)
{
FragmentWirUeberUns fragmentWirUeberUns = new FragmentWirUeberUns();
fragmentTransaction.replace(R.id.content_frame, fragmentWirUeberUns);
fragmentTransaction.commit();
}
if(editedPosition == 3)
{
FragmentBildungsgaenge fragmentBildungsgaenge = new FragmentBildungsgaenge();
fragmentTransaction.replace(R.id.content_frame, fragmentBildungsgaenge);
fragmentTransaction.commit();
}
if(editedPosition == 4)
{
FragmentInformation fragmentInformation = new FragmentInformation();
fragmentTransaction.replace(R.id.content_frame, fragmentInformation);
fragmentTransaction.commit();
}
if(editedPosition == 5)
{
FragmentAktuelles fragmentAktuelles = new FragmentAktuelles();
fragmentTransaction.replace(R.id.content_frame, fragmentAktuelles);
fragmentTransaction.commit();
}
if(editedPosition == 6)
{
FragmentInternes fragmentInternes = new FragmentInternes();
fragmentTransaction.replace(R.id.content_frame, fragmentInternes);
fragmentTransaction.commit();
}
if(editedPosition == 7)
{
FragmentService fragmentService = new FragmentService();
fragmentTransaction.replace(R.id.content_frame, fragmentService);
fragmentTransaction.commit();
}
Toast.makeText(MainActivity.this, "Du hast " + mDrawerListItems[position] + " ausgewählt", Toast.LENGTH_SHORT).show();
mDrawerLayout.closeDrawer(mDrawerList);
}
});
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,
toolbar,
R.string.drawer_open,
R.string.drawer_close){
public void onDrawerClosed(View v)
{
super.onDrawerClosed(v);
invalidateOptionsMenu();
syncState();
}
public void onDrawerOpened(View v)
{
super.onDrawerOpened(v);
invalidateOptionsMenu();
syncState();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle.syncState();
}
@Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
{
if (mDrawerLayout.isDrawerOpen(mDrawerList))
{
mDrawerLayout.closeDrawer(mDrawerList);
}
else
{
mDrawerLayout.openDrawer(mDrawerList);
}
return true;
}
default: return super.onOptionsItemSelected(item);
}
}
}
Hier nochmal die XML
activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/primary"
app:theme="@style/ToolbarTheme"
app:popupTheme="@style/Theme.AppCompat"/>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ListView
android:id="@android:id/list"
android:background="@android:color/white"
android:layout_width="305dp"
android:layout_height="match_parent"
android:layout_gravity="start"/>
<ListView
android:id="@+id/rightList"
android:background="@android:color/white"
android:layout_width="305dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:headerDividersEnabled="false" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
— geändert am 08.01.2015, 18:20:41
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.