2016-01-03 127 views
0

我已經Implented抽屜採用了滑蓋聽者 但是換手指請告訴我,我做錯了什麼,當它不工作:這裏 是我的frag類Android的滑動抽屜不工作

public class frag extends android.support.v4.app.Fragment{ 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) 
    { 
     View view = inflater.inflate(R.layout.lists , container); 
     return view; 
    } 

    public void setUp(int Fid ,DrawerLayout drawerLayout) 
    { 
      drawerLayout = (DrawerLayout)getActivity().findViewById(R.id.drawer); 
      drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); 
      final ActionBar ab = getActivity().getActionBar(); 
      ActionBarDrawerToggle abdl = new ActionBarDrawerToggle(
        getActivity(), 
        drawerLayout, 
        R.string.action_settings , 
        R.string.hello_world)  { 
      public void onDrawerClosed(View view) { 
        super.onDrawerClosed(view); 
        ab.setTitle("closed"); 
       } 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       ab.setTitle("opened"); 
      } 
     }; 
     drawerLayout.setDrawerListener(abdl); 
    } 

    public void close() 
    { 
     DrawerLayout drawerLayout = (DrawerLayout)getActivity().findViewById(R.id.drawer); 
     drawerLayout.closeDrawers(); 
    } 
} 

這裏是佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="240dp" 
android:id="@+id/dll" 
android:layout_height="match_parent"> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:gravity="right" 
    android:id="@+id/drawer" 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    > 
    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/sidebarlist" 
    android:text="thadssasdisk" 
     android:textAlignment="center"> 
    </ListView> 
</android.support.v4.widget.DrawerLayout> 

滑塊不工作,我想創建一個側面抽屜式導航類 ŧ他活動:

package com.example.shubham.navigation; 
import android.content.Context; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.text.Layout; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.TextView; 
import java.util.List; 
public class MainActivity extends AppCompatActivity { 
    DrawerLayout dl; 
    frag fg; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     fg = (frag)getSupportFragmentManager().findFragmentById(R.id.sidebar); 
     fg.setUp(R.id.drawer, (DrawerLayout) findViewById(R.id.drawer)); 
     ListView lv = (ListView)findViewById(R.id.sidebarlist); 
     drawer aa = new drawer(this); 
     lv.setAdapter(aa); 
     aa.add("text"); 
     aa.add("fun"); 
     aa.add("learn"); 
     aa.add("To do"); 
     aa.add("Pending"); 
     aa.add("Current"); 
     aa.add("future"); 
     aa.add("Learn"); 
     aa.add("tasks"); 
     aa.notifyDataSetChanged(); 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
       getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
      int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
    @Override 
    protected void onPostCreate(Bundle savedInstanceState) 
    { 
     super.onPostCreate(savedInstanceState); 
     fg.onViewStateRestored(savedInstanceState); 
     fg.abdl.syncState(); 
    } 
} 

的alyout文件wher片段稱爲:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/tview" 
    android:text="hello world you know who" 
    android:visibility="invisible" 
    /> 
     <fragment 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/sidebar" 
      class="com.example.shubham.navigation.frag" 
      ></fragment> 
</RelativeLayout> 
+0

它正在做什麼? –

+0

在'onCreateView'中調用'setUp' –

+0

其他任何人仍然不工作 – user49557

回答

0

您的NavigationDrawer實現是完全錯誤的。您應該從父母活動的onCreate()函數調用setup()來初始化ActionBar和NavigationDrawer。

public class MainActivity extends Activity { 
    private DrawerLayout mDrawerLayout; 
    private ActionBarDrawerToggle mDrawerToggle; 
    ... 

    public void onCreate(Bundle savedInstanceState) { 
     ... 

     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerToggle = new ActionBarDrawerToggle(
       this,     /* host Activity */ 
       mDrawerLayout,   /* DrawerLayout object */ 
       R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */ 
       R.string.drawer_open, /* "open drawer" description */ 
       R.string.drawer_close /* "close drawer" description */ 
       ) { 

      /** Called when a drawer has settled in a completely closed state. */ 
      public void onDrawerClosed(View view) { 
       super.onDrawerClosed(view); 
       getActionBar().setTitle(mTitle); 
      } 

      /** Called when a drawer has settled in a completely open state. */ 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       getActionBar().setTitle(mDrawerTitle); 
      } 
     }; 

     // Set the drawer toggle as the DrawerListener 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 

     getActionBar().setDisplayHomeAsUpEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 
    } 

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

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     mDrawerToggle.onConfigurationChanged(newConfig); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Pass the event to ActionBarDrawerToggle, if it returns 
     // true, then it has handled the app icon touch event 
     if (mDrawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 
     // Handle your other action bar items... 

     return super.onOptionsItemSelected(item); 
    } 
} 

Android的文檔:下面 示例代碼給出http://developer.android.com/training/implementing-navigation/nav-drawer.html

+0

那麼這對我來說並沒有什麼作用,但是我仍然需要在那個時候留下這個問題,所以我會在稍後嘗試,但是我仍然會給你賞金 – user49557

0

當我實現了這一點,我用下面的代碼與本教程的幫助:

sliding menu tutorial

抽屜適配器

public class NavDrawerListAdapter extends BaseAdapter { 

    private Context context; 
    private ArrayList<NavDrawerItem> navDrawerItems; 

    public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){ 
     this.context = context; 
     this.navDrawerItems = navDrawerItems; 
    } 

    @Override 
    public int getCount() { 
     return navDrawerItems.size(); 
    } 

    @Override 
    public Object getItem(int position) {  
     return navDrawerItems.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      LayoutInflater mInflater = (LayoutInflater) 
        context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
      convertView = mInflater.inflate(R.layout.drawer_list_item, null); 
     } 

     ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon); 
     TextView txtTitle = (TextView) convertView.findViewById(R.id.title); 
     TextView txtCount = (TextView) convertView.findViewById(R.id.counter); 

     imgIcon.setImageResource(navDrawerItems.get(position).getIcon());   
     txtTitle.setText(navDrawerItems.get(position).getTitle()); 

     // displaying count 
     // check whether it set visible or not 
     if(navDrawerItems.get(position).getCounterVisibility()){ 
      txtCount.setText(navDrawerItems.get(position).getCount()); 
     }else{ 
      // hide the counter view 
      txtCount.setVisibility(View.GONE); 
     } 

     return convertView; 
    } 

} 

主要活動

public class MainActivity extends Activity { 
    private DrawerLayout mDrawerLayout; 
    private ListView mDrawerList; 
    private ActionBarDrawerToggle mDrawerToggle; 

    // nav drawer title 
    private CharSequence mDrawerTitle; 

    // used to store app title 
    private CharSequence mTitle; 

    // slide menu items 
    private String[] navMenuTitles; 
    private TypedArray navMenuIcons; 

    private ArrayList<NavDrawerItem> navDrawerItems; 
    private NavDrawerListAdapter adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mTitle = mDrawerTitle = getTitle(); 

     // load slide menu items 
     navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); 

     // nav drawer icons from resources 
     navMenuIcons = getResources() 
       .obtainTypedArray(R.array.nav_drawer_icons); 

     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerList = (ListView) findViewById(R.id.list_slidermenu); 

     navDrawerItems = new ArrayList<NavDrawerItem>(); 

     // Home 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1))); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1))); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1))); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1))); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1))); 

     navMenuIcons.recycle(); 

     // setting the nav drawer list adapter 
     adapter = new NavDrawerListAdapter(getApplicationContext(), 
       navDrawerItems); 
     mDrawerList.setAdapter(adapter); 

     // enabling action bar app icon and behaving it as toggle button 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 

     mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
       R.drawable.ic_drawer, //nav menu toggle icon 
       R.string.app_name, // nav drawer open - description for accessibility 
       R.string.app_name // nav drawer close - description for accessibility 
     ){ 
      public void onDrawerClosed(View view) { 
       getActionBar().setTitle(mTitle); 
       // calling onPrepareOptionsMenu() to show action bar icons 
       invalidateOptionsMenu(); 
      } 

      public void onDrawerOpened(View drawerView) { 
       getActionBar().setTitle(mDrawerTitle); 
       // calling onPrepareOptionsMenu() to hide action bar icons 
       invalidateOptionsMenu(); 
      } 
     }; 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 

     if (savedInstanceState == null) { 
      // on first time display view for first nav item 
      displayView(0); 
     } 
    } 

    @Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
     // if nav drawer is opened, hide the action items 
     boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); 
     menu.findItem(R.id.action_settings).setVisible(!drawerOpen); 
     return super.onPrepareOptionsMenu(menu); 
    } 

    @Override 
    public void setTitle(CharSequence title) { 
     mTitle = title; 
     getActionBar().setTitle(mTitle); 
    } 

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

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     // Pass any configuration change to the drawer toggls 
     mDrawerToggle.onConfigurationChanged(newConfig); 
    } 

然後,您需要分別添加的代碼,每個片段和所需的資源。

爲導航滑塊的XML佈局文件將類似於下面的一個:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    android:background="@drawable/list_selector"> 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="25dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="12dp" 
     android:layout_marginRight="12dp" 
     android:contentDescription="@string/desc_list_item_icon" 
     android:src="@drawable/ic_home" 
     android:layout_centerVertical="true" /> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_toRightOf="@id/icon" 
     android:minHeight="?android:attr/listPreferredItemHeightSmall" 
     android:textAppearance="?android:attr/textAppearanceListItemSmall" 
     android:textColor="@color/list_item_title" 
     android:gravity="center_vertical" 
     android:paddingRight="40dp"/> 

    <TextView android:id="@+id/counter" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/counter_bg" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="8dp" 
     android:textColor="@color/counter_text_color"/> 

</RelativeLayout>