1

Bunun için sonsuza dek mücadele ediyordum ve sonunda bir miktar yardımla anlamaya çalışıyorum. Uygulamamın ana ekranındaki (Ana Etkinlik) araç çubuğuna bir gezinme çubuğu eklemeyi deniyorum. Şu anda, araç çubuğum, XML'imdeki DrawerLayout ve nav çekmecesi simgesiyle birlikte gösterilmiyor. çekmeceyi açmak için sağa kaydırmam gerekiyor. Sonra çekmecedeki bir öğeye tıkladığımda görünüm değişmez. Çekmecelerde bulunan 2 parçanın her ikisi de parçalardır. Kodumda XML'imin yanı sıra bir sorun olduğuna inanıyorum ama android XML dosyaları ile iyi değilim.Gezinme çubuğu gösterilmiyor ve çalışmıyor üzerinde çalışıyor

RobotChooser.java ilgili kod

public class RobotChooser extends AppCompatActivity implements AddEditRobotDialogFragment.DialogListener, ConfirmDeleteDialogFragment.DialogListener, ListView.OnItemClickListener { 

private View mEmptyView; 
private RecyclerView mRecyclerView; 
private RecyclerView.Adapter mAdapter; 

private ShowcaseView showcaseView; 
private boolean addedRobot; 
private Toolbar mToolbar; 

private String[] mFeatureTitles; 
private DrawerLayout mDrawerLayout; 
private ListView mDrawerList; 
private int drawerIndex = 1; 
private String mTitle; 
private String mDrawerTitle; 
private ActionBarDrawerToggle mDrawerToggle; 

Fragment fragment; 


public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    this.setContentView(R.layout.robot_chooser); 

    mEmptyView = findViewById(R.id.robot_empty_view); 
    mRecyclerView = (RecyclerView) findViewById(R.id.robot_recycler_view); 

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this); 
    mRecyclerView.setItemAnimator(new DefaultItemAnimator()); 
    mRecyclerView.setLayoutManager(mLayoutManager); 

    mToolbar = (Toolbar) findViewById(R.id.robot_chooser_toolbar); 
    setSupportActionBar(mToolbar); 

    RobotStorage.load(this); 

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout_robot_chooser); 
    mFeatureTitles = getResources().getStringArray(R.array.chooser_titles); //Where you set drawer item titles 
    mDrawerList = (ListView) findViewById(R.id.left_drawer2); 

    mTitle="ROS Control"; 
    mDrawerTitle=mTitle; 

    if (getActionBar() != null) { 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 
    } 

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
      R.string.drawer_open, 
      R.string.drawer_close) { 
     public void onDrawerClosed(View view) { 
      //getActionBar().setTitle(mTitle); 
      invalidateOptionsMenu(); // creates call to 
      // onPrepareOptionsMenu() 
     } 

     public void onDrawerOpened(View drawerView) { 
      //getActionBar().setTitle(mDrawerTitle); 
      invalidateOptionsMenu(); // creates call to 
      // onPrepareOptionsMenu() 
     } 
    }; 

    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 

    int[] imgRes = new int[]{ 
      R.drawable.ic_android_black_24dp, 
      R.drawable.ic_settings_black_24dp, 
      R.drawable.ic_info_outline_black_24dp 
    }; 

    List<DrawerItem> drawerItems = new ArrayList<>(); 

    for (int i = 0; i < mFeatureTitles.length; i++) { 
     drawerItems.add(new DrawerItem(mFeatureTitles[i], imgRes[i])); 
    } 

    NavDrawerAdapter drawerAdapter = new NavDrawerAdapter(this, 
      R.layout.nav_drawer_menu_item, 
      drawerItems); 

    mDrawerList.setAdapter(drawerAdapter); 
    mDrawerList.setOnItemClickListener(this); 

    private void selectItem(int position){ 
    Bundle args = new Bundle(); 
    FragmentManager fragmentManager = getFragmentManager(); 

    switch (position) { 
     case 0: 

      mDrawerLayout.closeDrawers(); 
      return; 

     case 1: 
      fragment = new PreferencesFragment(); 
      fragment.setArguments(args); 

      // Insert the fragment by replacing any existing fragment 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_frame2, fragment) 
        .commit(); 
      //fragmentsCreatedCounter = 0 

      break; 

     case 2: 
      fragment = new AboutFragment(); 
      //fragmentsCreatedCounter = fragmentsCreatedCounter + 1; 
      fragment.setArguments(args); 

      // Insert the fragment by replacing any existing fragment 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_frame2, fragment) 
        .commit(); 

      break; 

     default: 
      break; 
    } 


    // Highlight the selected item, update the title, and close the drawer 
    mDrawerList.setItemChecked(position, true); 
    mDrawerLayout.closeDrawer(mDrawerList); 
    setTitle(mFeatureTitles[position]); 
} 

@Override 
public void setTitle(CharSequence title) { 
    try { 
     //noinspection ConstantConditions 
     getActionBar().setTitle(title); 
    } catch (NullPointerException e) { 
     // Ignore 
    } 
} 

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    // Sync the toggle state after onRestoreInstanceState has occurred. 
    mDrawerToggle.syncState(); 
} 

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    selectItem(position); 

robot_chooser.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
    android:id="@+id/robot_chooser_toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    android:elevation="4dp" 
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout_robot_chooser" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- The main content view --> 
    <LinearLayout 
     android:id="@+id/content_frame2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" /> 

    <fragment 
     android:id="@+id/hud_fragment" 
     android:name="com.robotca.ControlApp.Fragments.PreferencesFragment" 
     android:layout_width="wrap_content" 
     android:layout_height="64sp"/> 
    <!--tools:layout="@layout/fragment_hud"--> 

    <fragment 
     android:id="@+id/about_fragment" 
     android:name="com.robotca.ControlApp.Fragments.AboutFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:layout="@layout/fragment_about"/> 


<android.support.v7.widget.RecyclerView 
    android:id="@+id/robot_recycler_view" 
    android:paddingTop="5dp" 
    android:scrollbars="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 
<TextView 
    android:id="@+id/robot_empty_view" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:textSize="20sp" 
    android:text="@string/no_robots" 
    android:elevation="3dp" 
    android:layout_gravity="center" 
    android:visibility="gone" 
    android:gravity="center" /> 

    <ListView android:id="@+id/left_drawer2" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="#ddd"/> 
</android.support.v4.widget.DrawerLayout> 

+0

Android studio kullanıyor musunuz? Dahili şablonla demo oluştur ve bu kodu seninkiyle karşılaştır. bunu da tutulmadan yapabilirsin. – Harry

+0

Evet android stüdyosu kullanıyorum. Sanırım büyük sorun benim xml'im ve ayrıca çekmecede tıklanan parçanın yeni ekran olarak nasıl ayarlandığını –

cevap

0

xml anahtarı durumlarda 1. ve 2. o zaman bu

<?xml version="1.0" encoding="utf-8"?> 

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:id="@+id/profileDrawer" 
android:layout_height="match_parent" > 

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

    <android.support.v7.widget.Toolbar 
     android:id="@+id/robot_chooser_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     android:elevation="4dp" 
     android:theme="@style/ThemeOverlay.AppCompat.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

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

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/robot_recycler_view" 
     android:paddingTop="5dp" 
     android:scrollbars="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <TextView 
     android:id="@+id/robot_empty_view" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:textSize="20sp" 
     android:text="@string/no_robots" 
     android:elevation="3dp" 
     android:layout_gravity="center" 
     android:visibility="gone" 
     android:gravity="center" /> 
    </FrameLayout> 

</LinearLayout> 

<ListView 
    android:id="@+id/left_drawer2" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="#eee" 
    android:choiceMode="singleChoice" 
    android:divider="@android:color/transparent" 
    android:dividerHeight="0dp" 
    android:listSelector="@android:color/darker_gray" /> 

</android.support.v4.widget.DrawerLayout> 

gibi olmalıdır sen ne yazdı gizlemek için geri dönüşümcü gibi görmek

mRecyclerView.setVisibility(View.GONE); 
İlgili konular