2015-12-23 59 views
1

如何将图像标题添加到像我这样的导航抽屉布局。我现在遇到的问题是每个listitem都有它自己的图标,但标题图像也印在每个项目上。如何将图像标题添加到我的导航抽屉布局

this is what i want enter image description here

什么,我现在是这样的: this is what i have now enter image description here

mainactivity.java

main activity.java 

package be.yvandamme.ginlovers.activity; 

import android.content.Context; 
import android.content.Intent; 
import android.content.res.Configuration; 
import android.content.res.TypedArray; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListView; 

import com.google.android.gms.analytics.GoogleAnalytics; 
import be.yvandamme.ginlovers.R; 
import be.yvandamme.ginlovers.WebViewAppApplication; 
import be.yvandamme.ginlovers.adapter.DrawerAdapter; 
import be.yvandamme.ginlovers.fragment.MainFragment; 


public class MainActivity extends ActionBarActivity 
{ 
private DrawerLayout mDrawerLayout; 
private ActionBarDrawerToggle mDrawerToggle; 
private ListView mDrawerListView; 

private CharSequence mTitle; 
private CharSequence mDrawerTitle; 
private String[] mTitles; 


public static Intent newIntent(Context context) 
{ 
    Intent intent = new Intent(context, MainActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    return intent; 
} 


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

    // init analytics tracker 
    ((WebViewAppApplication) getApplication()).getTracker(); 
} 


@Override 
public void onStart() 
{ 
    super.onStart(); 

    // analytics 
    GoogleAnalytics.getInstance(this).reportActivityStart(this); 
} 


@Override 
public void onResume() 
{ 
    super.onResume(); 
} 


@Override 
public void onPause() 
{ 
    super.onPause(); 
} 


@Override 
public void onStop() 
{ 
    super.onStop(); 

    // analytics 
    GoogleAnalytics.getInstance(this).reportActivityStop(this); 
} 


@Override 
public void onDestroy() 
{ 
    super.onDestroy(); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) 
{ 
    // action bar menu 
    return super.onCreateOptionsMenu(menu); 
} 


@Override 
public boolean onOptionsItemSelected(MenuItem item) 
{ 
    // open or close the drawer if home button is pressed 
    if(mDrawerToggle.onOptionsItemSelected(item)) 
    { 
     return true; 
    } 

    // action bar menu behaviour 
    switch(item.getItemId()) 
    { 
     case android.R.id.home: 
      Intent intent = MainActivity.newIntent(this); 
      startActivity(intent); 
      return true; 

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


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


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


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


private void setupActionBar() 
{ 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    ActionBar bar = getSupportActionBar(); 
    bar.setDisplayUseLogoEnabled(false); 
    bar.setDisplayShowTitleEnabled(true); 
    bar.setDisplayShowHomeEnabled(true); 
    bar.setDisplayHomeAsUpEnabled(true); 
    bar.setHomeButtonEnabled(true); 
} 


private void setupDrawer(Bundle savedInstanceState) 
{ 
    mTitle = getTitle(); 
    mDrawerTitle = getTitle(); 

    // title list 
    mTitles = getResources().getStringArray(R.array.navigation_title_list); 

    // icon list 
    TypedArray iconTypedArray = getResources().obtainTypedArray(R.array.navigation_icon_list); 
    Integer[] icons = new Integer[iconTypedArray.length()]; 
    for(int i=0; i<iconTypedArray.length(); i++) 
    { 
     icons[i] = iconTypedArray.getResourceId(i, -1); 
    } 
    iconTypedArray.recycle(); 

    // reference 
    mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_main_layout); 
    mDrawerListView = (ListView) findViewById(R.id.activity_main_drawer); 

    // set drawer 
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 
    mDrawerListView.setAdapter(new DrawerAdapter(this, mTitles, icons)); 
    mDrawerListView.setOnItemClickListener(new OnItemClickListener() 
    { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View clickedView, int position, long id) 
     { 
      selectDrawerItem(position, false); 
     } 
    }); 
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) 
    { 
     @Override 
     public void onDrawerClosed(View view) 
     { 
      getSupportActionBar().setTitle(mTitle); 
      supportInvalidateOptionsMenu(); 
     } 

     @Override 
     public void onDrawerOpened(View drawerView) 
     { 
      getSupportActionBar().setTitle(mDrawerTitle); 
      supportInvalidateOptionsMenu(); 
     } 
    }; 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 

    // show initial fragment 
    if(savedInstanceState == null) 
    { 
     selectDrawerItem(0, true); 
    } 
} 


private void selectDrawerItem(int position, boolean init) 
{ 
    String[] urlList = getResources().getStringArray(R.array.navigation_url_list); 
    String[] shareList = getResources().getStringArray(R.array.navigation_share_list); 

    Fragment fragment = MainFragment.newInstance(urlList[position], shareList[position]); 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
     fragmentManager.beginTransaction().replace(R.id.activity_main_container, fragment).commitAllowingStateLoss(); 

    mDrawerListView.setItemChecked(position, true); 
    if(!init) setTitle(mTitles[position]); 
    mDrawerLayout.closeDrawer(mDrawerListView); 
} 
} 

抽屉adapter.java

package be.yvandamme.ginlovers.adapter; 

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

import be.yvandamme.ginlovers.R; 


public class DrawerAdapter extends BaseAdapter 
{ 
private Context mContext; 
private String[] mTitleList; 
private Integer[] mIconList; 


public DrawerAdapter(Context context, String[] titleList, Integer[] iconList) 
{ 
    mContext = context; 
    mTitleList = titleList; 
    mIconList = iconList; 
} 


@Override 
public View getView(final int position, View convertView, ViewGroup parent) 
{ 
    // inflate view 
    View view = convertView; 
    if(view == null) 
    { 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.drawer_item, parent, false); 

     // view holder 
     ViewHolder holder = new ViewHolder(); 
     holder.titleTextView = (TextView) view.findViewById(R.id.drawer_item_title); 
     holder.iconImageView = (ImageView) view.findViewById(R.id.drawer_item_icon); 
     view.setTag(holder); 
    } 

    // entity 
    String title = mTitleList[position]; 
    Integer icon = mIconList[position]; 

    if(title!=null && icon!=null) 
    { 
     // view holder 
     ViewHolder holder = (ViewHolder) view.getTag(); 

     // content 
     holder.titleTextView.setText(title); 
     holder.iconImageView.setImageResource(icon); 
    } 

    return view; 
} 


@Override 
public int getCount() 
{ 
    if(mTitleList!=null) return mTitleList.length; 
    else return 0; 
} 


@Override 
public Object getItem(int position) 
{ 
    if(mTitleList!=null) return mTitleList[position]; 
    else return null; 
} 


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


public void refill(Context context, String[] titleList, Integer[] iconList) 
{ 
    mContext = context; 
    mTitleList = titleList; 
    mIconList = iconList; 
    notifyDataSetChanged(); 
} 


static class ViewHolder 
{ 
    TextView titleTextView; 
    ImageView iconImageView; 
} 
} 

这是我activity_main.xml中

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

<include layout="@layout/toolbar" /> 

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/activity_main_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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

    </FrameLayout> 

    <ListView 
     android:id="@+id/activity_main_drawer" 
     android:layout_width="@dimen/drawer_width" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:drawSelectorOnTop="true" 
     android:fastScrollEnabled="false" 
     android:listSelector="@drawable/selector_clickable_item_bg" 
     android:background="@color/global_bg_front" 
     /> 

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

</LinearLayout> 

,这我drawer_item.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="wrap_content" 
android:minHeight="@dimen/global_spacing_l" 
android:gravity="center_vertical" 
android:orientation="horizontal" 
android:background="?attr/drawerItemBackground"> 



<ImageView 
    android:id="@+id/drawer_image" 
    android:layout_width="280dp" 
    android:layout_height="140dp" 
    android:src="@drawable/loading" /> 


<ImageView 
    android:id="@+id/drawer_item_icon" 
    android:layout_width="@dimen/global_spacing_m" 
    android:layout_height="@dimen/global_spacing_m" 
    android:layout_marginLeft="@dimen/global_keyline_s" 
    android:layout_marginRight="@dimen/global_spacing_xs" 
    android:adjustViewBounds="true" 
    android:scaleType="fitCenter" 
    android:layout_below="@+id/drawer_image"/> 


<TextView 
    android:id="@+id/drawer_item_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="@dimen/global_keyline_s" 
    android:textAppearance="@style/TextAppearance.WebViewApp.Body1" 
    android:layout_below="@+id/drawer_image"/> 

</RelativeLayout> 
+0

您可以使用listview.addheaderview添加imageview作为listview标题 –

+0

我需要把它放在主要活动java文件或其他? –

回答

0

在您的XML并将其位置上方activity_main_drawer添加另一种观点认为你DrawerLayout内。标题可能不应该滚动,所以它不应该在你的列表视图。另外,从列表视图项目布局中删除drawer_image。因为它不应该包含在所有的项目中。

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

    <include layout="@layout/toolbar" /> 

    <android.support.v4.widget.DrawerLayout 
     android:id="@+id/activity_main_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

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

     </FrameLayout> 

     <LinearLayout 
      android:layout_width="@dimen/drawer_width" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:orientation="vertical"> 
      <ImageView 
       android:id="@+id/drawer_image" 
       android:layout_width="280dp" 
       android:layout_height="140dp" 
       android:src="@drawable/loading" /> 
      <ListView 
       android:id="@+id/activity_main_drawer" 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       android:choiceMode="singleChoice" 
       android:drawSelectorOnTop="true" 
       android:fastScrollEnabled="false" 
       android:listSelector="@drawable/selector_clickable_item_bg" 
       android:background="@color/global_bg_front" 
       /> 
     </LinearLayout> 

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

</LinearLayout> 

或使用http://developer.android.com/reference/android/support/design/widget/NavigationView.html并添加您的图像作为标题视图。

navView = (NavigationView) findViewById(R.id.navView); 
navView.inflateHeaderView(R.layout.myheader) 

或XML

app:headerLayout="@layout/myheader" 
+0

我仍然有相同的,如果我把它放在我的抽屉xml headerview打印在每个项目 –

+0

这是因为你应该从项目布局中删除图像。 –

+0

是的,我做到了,但它是相同的结果 –

0

尝试通过添加标题,以便您的列表视图(头也将滚动在这种情况下),你需要在你的MainActivity做这样的:

View header = (View) getLayoutInflater().inflate(
      R.layout.header_layout, null); 
ImageView img=(ImageView)header.findViewById(R.id.imgView); 
img.setBackgroundResource(R.drawable.icon); 
mainListView.addHeaderView(header); 

或者您可以使用DesignLibrary,如herehere

相关问题