2015-10-21 51 views
1

我想在NavigationDrawer中顯示一些信息。該信息由用戶在之前的活動中輸入。我使用意圖將信息發送到抽屜的主要活動。在之前活動中的用戶輸入之後更新NavigationDrawer中的TextView

MainActivity(後用戶輸入信息)

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener { 

private static String TAG = MainActivity.class.getSimpleName(); 

private Toolbar mToolbar; 
private FragmentDrawer drawerFragment; 

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

    Intent intent = getIntent(); 
    String info = intent.getStringExtra("info"); 

    mToolbar = (Toolbar) findViewById(R.id.toolbar); 

    setSupportActionBar(mToolbar); 
    getSupportActionBar().setDisplayShowHomeEnabled(true); 

    drawerFragment = (FragmentDrawer) 
      getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer); 
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar); 
    drawerFragment.setDrawerListener(this); 


    // display the first navigation drawer view on app launch 
    displayView(0); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.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; 
    } 

    if(id == R.id.action_search){ 
     Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show(); 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onDrawerItemSelected(View view, int position) { 
    displayView(position); 
} 

private void displayView(int position) { 
    Fragment fragment = null; 
    String title = getString(R.string.app_name); 
    switch (position) { 
     case 0: 
      fragment = new HomeFragment(); 
      title = getString(R.string.nav_item_dashboard); 
      break; 
      ...... 
    } 

我有一個抽屜式導航欄特定的佈局,它包含了我想用字符串測試更新一個TextView。這是片段代碼和它的xml。

FragmentDrawer類

public FragmentDrawer() { 

} 

public void setDrawerListener(FragmentDrawerListener listener) { 
    this.drawerListener = listener; 
} 

public static List<NavDrawerItem> getData() { 
    List<NavDrawerItem> data = new ArrayList<>(); 

    // preparing navigation drawer items 
    for (int i = 0; i < titles.length; i++) { 
     NavDrawerItem navItem = new NavDrawerItem(); 
     navItem.setTitle(titles[i]); 
     navItem.setIcon(icons.getResourceId(i,-1)); 
     data.add(navItem); 
    } 
    return data; 
} 

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

    // drawer labels 
    titles = getActivity().getResources().getStringArray(R.array.nav_drawer_labels); 
    icons = getResources().obtainTypedArray(R.array.nav_drawer_icons); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflating view layout 
    View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false); 
    recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList); 

    TextView userID = (TextView) layout.findViewById(R.id.nav_title_user); 

    adapter = new NavigationDrawerAdapter(getActivity(), getData()); 
    recyclerView.setAdapter(adapter); 
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
    recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() { 
     @Override 
     public void onClick(View view, int position) { 
      drawerListener.onDrawerItemSelected(view, position); 
      mDrawerLayout.closeDrawer(containerView); 
     } 

     @Override 
     public void onLongClick(View view, int position) { 

     } 
    })); 

    return layout; 
} 

public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar) { 
    containerView = getActivity().findViewById(fragmentId); 
    mDrawerLayout = drawerLayout; 
    mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) { 
     @Override 
     public void onDrawerOpened(View drawerView) { 
      super.onDrawerOpened(drawerView); 
      getActivity().invalidateOptionsMenu(); 
     } 

     @Override 
     public void onDrawerClosed(View drawerView) { 
      super.onDrawerClosed(drawerView); 
      getActivity().invalidateOptionsMenu(); 
     } 

     @Override 
     public void onDrawerSlide(View drawerView, float slideOffset) { 
      super.onDrawerSlide(drawerView, slideOffset); 
      toolbar.setAlpha(1 - slideOffset/2); 
     } 
    }; 

    mDrawerLayout.setDrawerListener(mDrawerToggle); 
    mDrawerLayout.post(new Runnable() { 
     @Override 
     public void run() { 
      mDrawerToggle.syncState(); 
     } 
    }); 

} 

FramgentDrawer.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@android:color/white"> 


<RelativeLayout 
    android:id="@+id/nav_header_container" 
    android:layout_width="match_parent" 
    android:layout_height="140dp" 
    android:layout_alignParentTop="true" 
    android:background="@color/colorPrimary"> 

    ...... 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceInverse" 
     android:text="@string/welcome_user" 
     android:id="@+id/nav_title_user" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="10dp"/> 

</RelativeLayout> 


<android.support.v7.widget.RecyclerView 
    android:id="@+id/drawerList" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="15dp" 
    android:layout_below="@id/nav_header_container" /> 

我已成功地更新與圖標,標籤和標題抽屜名單。我通過使用RecyclerViewAdapter來完成。

我遇到的問題是這些細節來自資源(strings.xml)。

如何更新NavigationFragment創建時的textview?我如何發送一個值給片段,以便在片段的OnCreateView過程中使用它?

+0

顯示你的XML和代碼 – Pavan

+0

我面對同樣的問題,你能告訴我如何更新列表項文本,例如「登錄」到「登出」? –

回答

1

我看起來太過分了。解決方案非常簡單...即使我花了3個小時才弄明白這一點:/

在NavigationFragment中,我創建了一個用字符串更新textview的方法。在MainActivity,我得到的意圖的信息,我只是調用的方法是這樣的:

drawerFragment.setUserTextView(info); 

這就是爲什麼採取休息是很重要的... ^^

+0

我面臨同樣的問題,你能告訴我如何更新列表項文本,例如「登錄」到「註銷」? –

相關問題