簡體   English   中英

如何將圖像標題添加到導航抽屜布局

[英]how can i add an image header to my navigation drawer layout

我該如何在我的導航抽屜布局中添加一個圖像標題。 我現在遇到的問題是,每個列表項都有其自己的圖標,但標題圖像也打印在每個項目上。

這就是我要的 在此處輸入圖片說明

我現在擁有的是: 這就是我現在擁有的 在此處輸入圖片說明

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

抽屜式適配器

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>

在XML中添加另一個視圖,並將其放置在DrawerLayout 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"

嘗試通過將標題視圖添加到列表視圖中(在這種情況下標題也會滾動),您需要在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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM