2015-10-14 50 views
1

我有一個導航畫與部分和項目,但我需要把一個圖像和文本作爲標題,但不知道從我有什麼確切的,我有的導航抽屜是android studio創建的默認一個當你點擊右鍵並選擇新建>活動>抽屜活動。如何將圖像和文本視圖的標題添加到我的導航抽屜中?

這是我想有作爲報頭中的XML: nav_header.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- Character's Avatar --> 
    <ImageView android:id="@+id/navigation_drawer_header_iv_image" 
     android:layout_width="200dp" 
     android:layout_height="200dp" 
     android:src="@drawable/image_sorcerer"/> 

    <!-- Character's name --> 
    <TextView android:id="@+id/navigation_drawer_header_tv_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Example text" 
     android:paddingLeft="16dp"/> 
</LinearLayout> 

導航抽屜內容通過自定義適配器

GameNavigationFragment類(設置這是管理片段導航抽屜)

public class GameNavigationFragment extends Fragment { 

private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position"; 

private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned"; 
private NavigationDrawerCallbacks mCallbacks; 
private ActionBarDrawerToggle mDrawerToggle; 

private DrawerLayout mDrawerLayout; 
private ListView mDrawerListView; 
private View mFragmentContainerView; 

private int mCurrentSelectedPosition = 0; 
private boolean mFromSavedInstanceState; 
private boolean mUserLearnedDrawer; 

public GameNavigationFragment() { 
} 

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

    // Read in the flag indicating whether or not the user has demonstrated awareness of the 
    // drawer. See PREF_USER_LEARNED_DRAWER for details. 
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
    mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false); 

    if (savedInstanceState != null) { 
     mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION); 
     mFromSavedInstanceState = true; 
    } 

    // Select either the default item (0) or the last selected item. 
    selectItem(mCurrentSelectedPosition); 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    // Indicate that this fragment would like to influence the set of actions in the action bar. 
    setHasOptionsMenu(true); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    mDrawerListView = (ListView) inflater.inflate(
      R.layout.fragment_navigation_drawer, container, false); 
    mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      selectItem(position); 
     } 
    }); 

    CustomListViewAdapter adapter = new CustomListViewAdapter(getActivity()); 
    adapter.addSeparatorItem("Menu"); 
    adapter.addItem("Section 1"); 
    adapter.addItem("Section 2"); 
    adapter.addItem("Section 3"); 
    adapter.addSeparatorItem("Opciones"); 
    adapter.addItem("Section 1"); 
    adapter.addItem("Section 2"); 
    adapter.addItem("Section 3"); 


    mDrawerListView.setAdapter(adapter); 

    return mDrawerListView; 
} 

CustomAdapterListView

package com.jomasa.pocketfantasy.adapters; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.TreeSet; 
import com.jomasa.pocketfantasy.R; 

public class CustomListViewAdapter extends BaseAdapter { 
    private static final int TYPE_ITEM = 0; 
    private static final int TYPE_SEPARATOR = 1; 

    private ArrayList<String> mData = new ArrayList<String>(); 
    private TreeSet<Integer> sectionHeader = new TreeSet<Integer>(); 
    private LayoutInflater mInflater; 

    public CustomListViewAdapter(Context context){ 
     mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public void addItem(final String item) { 
     mData.add(item); 
     notifyDataSetChanged(); 
    } 

    public void addSeparatorItem(final String item) { 
     mData.add(item); 
     sectionHeader.add(mData.size() - 1); 
     notifyDataSetChanged(); 
    } 

    @Override 
    public int getItemViewType(int position) { 
     return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM; 
    } 

    @Override 
    public int getViewTypeCount() { 
     return 2; 
    } 

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

    @Override 
    public String getItem(int position) { 
     return mData.get(position); 
    } 

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 
     int rowType = getItemViewType(position); 

     if (convertView == null) { 
      holder = new ViewHolder(); 

      switch (rowType) { 
       case TYPE_ITEM: 
        convertView = mInflater.inflate(R.layout.snippet_item1, null); 
        holder.textView = (TextView) convertView.findViewById(R.id.text); 
        break; 
       case TYPE_SEPARATOR: 
        convertView = mInflater.inflate(R.layout.snippet_item2, null); 
        holder.textView = (TextView) convertView.findViewById(R.id.textSeparator); 
        break; 
      } 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.textView.setText(mData.get(position)); 

     return convertView; 
    } 

    public static class ViewHolder { 
     public TextView textView; 
    } 
} 

GameActivity

package com.jomasa.pocketfantasy.activities; 

import android.support.v7.app.AppCompatActivity; 
import android.support.v7.app.ActionBar; 
import android.support.v4.app.FragmentManager; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.support.v4.widget.DrawerLayout; 

import com.jomasa.pocketfantasy.R; 
import com.jomasa.pocketfantasy.fragments.GameInventoryFragment; 
import com.jomasa.pocketfantasy.fragments.GameMainFragment; 
import com.jomasa.pocketfantasy.fragments.GameNavigationFragment; 
import com.jomasa.pocketfantasy.fragments.GameSheetFragment; 

public class GameActivity extends AppCompatActivity 
     implements GameNavigationFragment.NavigationDrawerCallbacks { 

    /** 
    * Fragment managing the behaviors, interactions and presentation of the navigation drawer. 
    */ 
    private GameNavigationFragment mGameNavigationFragment; 

    /** 
    * Used to store the last screen title. For use in {@link #restoreActionBar()}. 
    */ 
    private CharSequence mTitle; 

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

     mGameNavigationFragment = (GameNavigationFragment) 
       getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); 
     mTitle = getTitle(); 

     // Set up the drawer. 
     mGameNavigationFragment.setUp(
       R.id.navigation_drawer, 
       (DrawerLayout) findViewById(R.id.drawer_layout)); 
    } 

    @Override 
    public void onNavigationDrawerItemSelected(int position) { 
     FragmentManager fragmentManager = getSupportFragmentManager(); 

     switch (position) 
     { 
      case 0: 
       fragmentManager.beginTransaction() 
         .replace(R.id.container, GameMainFragment.newInstance(position)) 
         .commit(); 
       break; 

      case 1: 
       fragmentManager.beginTransaction() 
         .replace(R.id.container, GameInventoryFragment.newInstance(position)) 
         .commit(); 
       break; 

      case 2: 
       fragmentManager.beginTransaction() 
         .replace(R.id.container, GameSheetFragment.newInstance(position)) 
         .commit(); 
       break; 

      default: break; 
     } 
    } 

    public void onSectionAttached(int number) { 
     switch (number) { 
      case 1: 
       mTitle = getString(R.string.navigation_title_main); 
       break; 
      case 2: 
       mTitle = getString(R.string.navigation_title_inventory); 
       break; 
      case 3: 
       mTitle = getString(R.string.navigation_title_sheet); 
       break; 
     } 
    } 

    public void restoreActionBar() { 
     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
     actionBar.setDisplayShowTitleEnabled(true); 
     actionBar.setTitle(mTitle); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     if (!mGameNavigationFragment.isDrawerOpen()) { 
      // Only show items in the action bar relevant to this screen 
      // if the drawer is not showing. Otherwise, let the drawer 
      // decide what to show in the action bar. 
      getMenuInflater().inflate(R.menu.menu_game, menu); 
      restoreActionBar(); 
      return true; 
     } 
     return super.onCreateOptionsMenu(menu); 
    } 

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

     return super.onOptionsItemSelected(item); 
    } 
} 

這是導航看起來像現在: enter image description here

+0

使用'Material Design's'' Navigation View'示例[here](http://www.android4devs.com/2015/06/navigation-view-material-design-support.html) –

+0

我昨天試過這個選項,沒有幫助我,我想我必須添加一些東西到我的自定義適配器,但我不知道它到底是什麼。 –

+1

將android studio更新到1.4並創建帶有標題圖像和標題的材質滑塊附帶的Slider活動 –

回答

1

更新Android工作室1.4,那麼你可以創建一個標題導航抽屜活動圖片

0

這是事端我正在努力。

activity_main.xml中

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

    <!-- Contenido Principal --> 
    <include layout="@layout/main_content" 
     android:id="@+id/mainContent"/> 

    <!-- Navigation Drawer --> 
    <android.support.design.widget.NavigationView 
     android:id="@+id/navigationView" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:headerLayout="@layout/drawer_header" 
     app:menu="@menu/nav_menu"/> 

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

我目前使用的導航視圖模式。在NavigationView中查看此屬性:

app:headerLayout="@layout/drawer_header" 

如果您需要將您的佈局文件替換爲drawer_header。 如果你想使用NavigationView,你必須添加在您的搖籃這種依賴關係:

compile 'com.android.support:design:22.2.0' 

希望這有助於。

相關問題