2014-01-05 64 views
4

現在我正在使用帶有碎片的導航抽屜,並選定了突出顯示紅色的項目。 我使用粗體字體來選擇項目而不是突出顯示。任何人都可以說我,我怎麼能意識到這一點?選擇後的粗體項目導航抽屜

MainActivity

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

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

    navDrawerItems = new ArrayList<NavDrawerItem>(); 

    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0])); 
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1])); 
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[2])); 

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener()); 

    // 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_white, //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) { 
     displayView(0); 
    } 
} 


private class SlideMenuClickListener implements ListView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     displayView(position); 
    } 
} 

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (mDrawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 
    return super.onPrepareOptionsMenu(menu); 
} 

private void displayView(int position) { 
    // update the main content by replacing fragments 
    Fragment fragment = null; 
    switch (position) { 
    case 0: 
     fragment = new MainFragment(); 
     break; 
    case 1: 
     fragment = new TheoryFragment(); 
     break; 
    case 2: 
     fragment = new SettingsFragment(); 
     break; 
    default: 
     break; 
    } 

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


     mDrawerList.setItemChecked(position, true); 
     mDrawerList.setSelection(position); 
     setTitle(navMenuTitles[position]); 
     mDrawerLayout.closeDrawer(mDrawerList); 
    } else { 
     Log.e("MainActivity", "Error in creating fragment"); 
    } 
} 

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

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    mDrawerToggle.syncState(); 
} 

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

DrawerAdapter

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

    TextView txtTitle = (TextView) convertView.findViewById(R.id.title); 

    txtTitle.setText(navDrawerItems.get(position).getTitle()); 

    return convertView; 
} 

}

UPD: 我明白了檢查項目如何大膽,不會被刪除切換到不同的項目時並沒有明星的大膽風格噸。

更新的代碼

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     View rowView = view; 
     TextView tv = (TextView) rowView.findViewById(R.id.title); 
     tv.setTypeface(null, Typeface.BOLD); 
     displayView(position); 
    } 

回答

0

在你SlideMenuClickListeners onItemClick,將所有項目Typeface.DEFAULT,然後設置你感興趣的是Typeface.DEFAULT_BOLD項目:

for (int i = 0; i < parent.getCount(); i++) { 
    TextView v = (TextView)parent.getChildAt(i); 
    if (v != null) 
     v.setTypeface(Typeface.DEFAULT); 
} 
((TextView)view).setTypeface(Typeface.DEFAULT_BOLD); 

爲了擺脫選中後突出顯示,請在您的displayView方法中刪除mDrawerList.setCheckedmDrawerList.setSelection方法

但是,由於我在初始化應用程序時遇到了第一個項目的問題,因此需要使用此解決方案。

6

這裏是我的完整的解決方案...

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
{ 
    setNavDrawerItemNormal(); 
    TextView txtview = ((TextView) view.findViewById(R.id.txtNav)); 
    txtview.setTypeface(null, Typeface.BOLD); 
} 

public void setNavDrawerItemNormal() 
{ 
    for (int i=0; i< mDrawerListView.getChildCount(); i++) 
    { 
     View v = mDrawerListView.getChildAt(i); 
     TextView txtview = ((TextView) v.findViewById(R.id.txtNav)); 
     Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Light.ttf"); 
     txtview.setTypeface(font); 
    } 
} 

而且大膽導航抽屜在應用程序初始化的第一個項目,我做的是,在列表適配器獲得視圖的方法...

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView = inflater.inflate(R.layout.navdrawer_item, parent, false); 
    TextView textView = (TextView) rowView.findViewById(R.id.txtNav); 
    textView.setText(values[position]); 
    if (position == 0) { textView.setTypeface(null, Typeface.BOLD); } 

    return rowView; 
} 

因此,在這裏我檢查了物品的位置是否爲0(意思是它的第一個物品),然後使其變爲粗體。這整個事情對我來說很完美〜

+1

將此標記爲答案 – playmaker420

+1

工作但不好 –

+0

我猜你的答案是誤導,因爲最好使用standart的Checkable接口作爲列表項並使用'myDrawerListView.setChoiceMode (ListView.CHOICE_MODE_MULTIPLE)'。 在你的情況下,你一定要使用CheckedTextView。 –