2014-02-21 46 views
0

我currentley有一個adroid部件,當點擊時啓動一個活動。我已經改變了我的整個應用程序片段,以便使用android導航抽屜,並且我不確定如何在單擊我的小部件時啓動我的片段。Android小部件,啓動一個片段?

其推出的活動我的窗口小部件的代碼是:

public class SearchWidget extends AppWidgetProvider { 

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     final int N = appWidgetIds.length; 

     // Perform this loop procedure for each App Widget that belongs to this provider 
     for (int i=0; i<N; i++) { 
      int appWidgetId = appWidgetIds[i]; 

      // Create an Intent to launch ExampleActivity 
      Intent intent = new Intent(context, Search.class); 
      PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); 

      // Get the layout for the App Widget and attach an on-click listener 
      // to the button 
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_search); 
      views.setOnClickPendingIntent(R.id.searchclick, pendingIntent); 

      // Tell the AppWidgetManager to perform an update on the current app widget 
      appWidgetManager.updateAppWidget(appWidgetId, views); 
     } 

    } 


} 

我怎樣可以發動片段,而且在我看來,這握住我的抽屜裏還推出呢?

我有一個MainDrawer活動:

public class MainDrawer2 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.fragment_main); 

     mTitle = mDrawerTitle = getTitle(); 

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

     // nav drawer icons from resources 


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

     navDrawerItems = new ArrayList<NavDrawerItem>(); 

     // adding nav drawer items to array 
     // Home 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[0])); 
     // Find People 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[1])); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[2])); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[3])); 



     // 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); 

     mDrawerList.setOnItemClickListener(new SlideMenuClickListener()); 


     //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); 
     } 


    } 



    /** 
    * Slide menu item click listener 
    * */ 
    private class SlideMenuClickListener implements 
      ListView.OnItemClickListener { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
           long id) { 
      // display view for selected nav drawer item 
      displayView(position); 
     } 
    } 

    /** 
    * Diplaying fragment view for selected nav drawer list item 
    * */ 
    private void displayView(int position) { 
     // update the main content by replacing fragments 
     Fragment fragment = null; 
     switch (position) { 
      case 0: 
       fragment = new StatisticsPage(); 
       break; 
      case 1: 
       fragment = new Discover(); 
       break; 
      case 2: 
       fragment = new Portfolio(); 
       break; 
      case 3: 
       fragment = new NewsWeb(); 
       break; 

      default: 
       break; 
     } 

     if (fragment != null) { 
      FragmentManager fragmentManager = getFragmentManager(); 
      fragmentManager.beginTransaction() 
        .replace(R.id.main, fragment).commit(); 

      // update selected item and title, then close the drawer 
      mDrawerList.setItemChecked(position, true); 
      mDrawerList.setSelection(position); 
      setTitle(navMenuTitles[position]); 
      mDrawerLayout.closeDrawer(mDrawerList); 
     } else { 
      // error in creating fragment 
      Log.e("MainActivity", "Error in creating fragment"); 
     } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // toggle nav drawer on selecting action bar app icon/title 
     if (mDrawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 

     // Handle action bar actions click 
     switch (item.getItemId()) { 
      //case R.id.menu_search2: 



       //return true; 

      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    /*** 
    * Called when invalidateOptionsMenu() is triggered 
    */ 
    @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); 
    } 

    /** 
    * When using the ActionBarDrawerToggle, you must call it during 
    * onPostCreate() and onConfigurationChanged()... 
    */ 

    @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); 
    } 

    public void goToSearch(MenuItem item){ 

     //go to search page 
     //Intent i = new Intent(this,Search.class); 
     //startActivity(i); 

     FragmentManager man=getFragmentManager(); 
     FragmentTransaction tran=man.beginTransaction(); 
     Fragment Fragment_four= new Search(); 
     tran.replace(R.id.main, Fragment_four);//tran. 
     tran.addToBackStack(null); 
     tran.commit(); 

    } 



} 

主要抽屜使用這個XML它可以對導航抽屜和一個用於獲取加載實際片段一個片段。上面的MainDrawer活動以統計信息片段開始。我希望我的小部件以下面的XML啓動,但不啓動統計信息片段。我想要它加載另一個片段。

XML:

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

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

    <ListView 
     android:id="@+id/drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:divider="@android:color/white" 
     android:background="@android:color/black" 
     android:choiceMode="singleChoice"/> 

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

UPDATE:

public class MainDrawer2 extends Activity { 

    public static final String EXTRA_DEFAULT_FRAGMENT = "com.beerportfolio.beerportfoliopro.EXTRA_DEFAULT_FRAGMENT"; 

    public static final int FRAGMENT_STATISTICS = 0; 
    public static final int FRAGMENT_DISCOVER = 1; 

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

     mTitle = mDrawerTitle = getTitle(); 

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

     // nav drawer icons from resources 


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

     navDrawerItems = new ArrayList<NavDrawerItem>(); 

     // adding nav drawer items to array 
     // Home 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[0])); 
     // Find People 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[1])); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[2])); 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[3])); 



     // 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); 

     mDrawerList.setOnItemClickListener(new SlideMenuClickListener()); 


     //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) { 
      Intent i = getIntent(); 

      // Default to the stats fragment if a default wasn't supplied 
      int defaultView = i.getIntExtra(EXTRA_DEFAULT_FRAGMENT, FRAGMENT_STATISTICS); 
      displayView(defaultView); 
     } 


    } 



    /** 
    * Slide menu item click listener 
    * */ 
    private class SlideMenuClickListener implements 
      ListView.OnItemClickListener { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
           long id) { 
      // display view for selected nav drawer item 
      displayView(position); 
     } 
    } 

    /** 
    * Diplaying fragment view for selected nav drawer list item 
    * */ 
    private void displayView(int position) { 
     // update the main content by replacing fragments 
     Fragment fragment = null; 
     switch (position) { 
      case 0: 
       fragment = new StatisticsPage(); 
       break; 
      case 1: 
       fragment = new Discover(); 
       break; 
      case 2: 
       fragment = new Portfolio(); 
       break; 
      case 3: 
       fragment = new NewsWeb(); 
       break; 

      default: 
       break; 
     } 

     if (fragment != null) { 
      FragmentManager fragmentManager = getFragmentManager(); 
      fragmentManager.beginTransaction() 
        .replace(R.id.main, fragment).commit(); 

      // update selected item and title, then close the drawer 
      mDrawerList.setItemChecked(position, true); 
      mDrawerList.setSelection(position); 
      setTitle(navMenuTitles[position]); 
      mDrawerLayout.closeDrawer(mDrawerList); 
     } else { 
      // error in creating fragment 
      Log.e("MainActivity", "Error in creating fragment"); 
     } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // toggle nav drawer on selecting action bar app icon/title 
     if (mDrawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 

     // Handle action bar actions click 
     switch (item.getItemId()) { 
      //case R.id.menu_search2: 



       //return true; 

      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    /*** 
    * Called when invalidateOptionsMenu() is triggered 
    */ 
    @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); 
    } 

    /** 
    * When using the ActionBarDrawerToggle, you must call it during 
    * onPostCreate() and onConfigurationChanged()... 
    */ 

    @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); 
    } 

    public void goToSearch(MenuItem item){ 

     //go to search page 
     //Intent i = new Intent(this,Search.class); 
     //startActivity(i); 

     FragmentManager man=getFragmentManager(); 
     FragmentTransaction tran=man.beginTransaction(); 
     Fragment Fragment_four= new Search(); 
     tran.replace(R.id.main, Fragment_four);//tran. 
     tran.addToBackStack(null); 
     tran.commit(); 

    } 



} 
+1

我想你可能有點困惑。你絕對必須開展一項活動纔能有一個片段。沒有活動,就沒有背景,背景是非常重要的。因此,簡而言之,在您嘗試啓動的活動中,您應該加載片段。 –

+0

所以我有一個MainDrawer的活動,它有我的導航抽屜,並且是我加載我的活動的地方。我想加載到MainDrawer在加載時通​​常不會默認的特定片段。那麼我是否必須製作另一個整體Drawer活動才能加載,但是默認爲我的其他片段?原因然後我幾乎卡在其他抽屜類從小部件訪問... – Mike

+0

我不知道我完全理解你的問題。抽屜裏的意思是什麼?一個從左側滑動?你能提供你的代碼嗎,所以我可以看到你想要做什麼? –

回答

1

所以看起來你已經到位的好框架,就使這個容易。在您的onCreate(),在那裏你當前的呼叫displayView(0),這樣做

public class MainDrawer2 extends Activity { 
    public static final String EXTRA_DEFAULT_FRAGMENT = "my.package.name.EXTRA_DEFAULT_FRAGMENT"; 

    public static final int FRAGMENT_STATISTICS = 0; 
    public static final int FRAGMENT_DISCOVER = 1; 
    //... etc. 

然後:

if (savedInstanceState == null) { 
    Intent i = getIntent(); 
    // Default to the stats fragment if a default wasn't supplied 
    int defaultView = i.getIntExtra(EXTRA_DEFAULT_FRAGMENT, FRAGMENT_STATISTICS); 
    displayView(defaultView); 
} 

這種方式首先,定義一個值,額外的傳遞與您的推出意圖,你可以用你的活動推出Intent創建PendingIntent,但是當你這樣做的,只是包括默認片段還有:

Intent i = new Intent(context, MainDrawer2.class); 
i.putExtra(MainDrawer2.EXTRA_DEFAULT_FRAGMENT, MainDrawer2.FRAGMENT_DISCOVER); 
PendingIntent pi = PendingIntent.getActivity(context, 0, i, 0); 
+0

試圖查看我是否正在跟蹤,if語句是否正在創建我的MAinDrawer類的頂部? – Mike

+0

假設我正確理解您的項目結構,那麼是的。您基本上已經默認在第一次創建活動時添加統計信息片段(即,當savedInstanceState爲空時)。這只是增加了一個機制來攔截,並讓你通過意圖決定要顯示哪個片段。 – kcoppock

+0

因此,我添加了代碼,並且指出了一個錯誤,指出解決方法getIntExtra – Mike