带有标题/分区的导航抽屉

11

我想知道是否有办法在导航抽屉中添加标题/部分。我已经成功添加了类似的东西,但它只显示在列表的顶部,因为addHeaderView需要在setAdapter之前调用,如果我尝试在setAdapter之后添加更多元素,它们将重写第一个元素。

谢谢。

编辑:

public class MenuListAdapter extends BaseAdapter {

// Declare Variables
Context context;
String[] mTitle;
String[] mSubTitle;
int[] mIcon;
LayoutInflater inflater;

public MenuListAdapter(Context context, String[] title, String[] subtitle,
        int[] icon) {
    this.context = context;
    this.mTitle = title;
    this.mSubTitle = subtitle;
    this.mIcon = icon;

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getViewTypeCount() {
    return super.getViewTypeCount();
}

@Override
public int getItemViewType(int position) {
    return super.getItemViewType(position);
}

@Override
public int getCount() {
    return mTitle.length;
}

@Override
public Object getItem(int position) {
    return mTitle[position];
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    // Declare Variables
    TextView txtTitle;
    TextView txtSubTitle;
    ImageView imgIcon;

    View itemView = inflater.inflate(R.layout.drawer_list_item, parent,
            false);

    // Locate the TextViews in drawer_list_item.xml
    txtTitle = (TextView) itemView.findViewById(R.id.title);
    txtSubTitle = (TextView) itemView.findViewById(R.id.subtitle);

    // Locate the ImageView in drawer_list_item.xml
    imgIcon = (ImageView) itemView.findViewById(R.id.icon);

    // Set the results into TextViews
    txtTitle.setText(mTitle[position]);
    txtSubTitle.setText(mSubTitle[position]);

    // Set the results into ImageView
    imgIcon.setImageResource(mIcon[position]);

    return itemView;
}

编辑:

我通过整合来自不同来源的解决方案找到了一个好的解决方案,这些是我使用的主要类:

EntryAdapter

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.androidbegin.sidemenututorial.R;

public class EntryAdapter extends ArrayAdapter<Item> {

    private enum RowType {
        LIST_ITEM, HEADER_ITEM
    }

    private Context context;
    private ArrayList<Item> items;
    private LayoutInflater vi;

    public EntryAdapter(Context context, ArrayList<Item> items) {
        super(context,0, items);
        this.context = context;
        this.items = items;
        vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getViewTypeCount() {  //Returns the number of types of Views that will be created by getView(int, View, ViewGroup).
        return RowType.values().length;
    }

    @Override
    public int getItemViewType(int position) { //framework calls getItemViewType for row n, the row it is about to display.
        //Get the type of View that will be created by getView(int, View, ViewGroup) for the specified item.
        Log.i("LIST", "item at " + position + " is " 
                + ((getItem(position).isSection() ? 0 : 1) == 0 ? "section" : "normal item"));
        return getItem(position).isSection() ? 0 : 1; // get position passes (n) and accertain  is its a header  or not
    }

    @Override
    public boolean isEnabled(int position) {
        return !getItem(position).isSection();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        final Item i = items.get(position);
        if (i != null) {
            if(i.isSection()){
                SectionItem si = (SectionItem) i;
                v = vi.inflate(R.layout.list_item_section, null);

                v.setOnClickListener(null);
                v.setOnLongClickListener(null);
                v.setLongClickable(false);

                final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
                sectionView.setText(si.getTitle());
            }else{
                EntryItem ei = (EntryItem) i;
                v = vi.inflate(R.layout.list_item_entry, null);
                final TextView title = (TextView)v.findViewById(R.id.list_item_entry_title);
                final TextView subtitle = (TextView)v.findViewById(R.id.list_item_entry_summary);

                if (title != null) 
                    title.setText(ei.title);
                if(subtitle != null)
                    subtitle.setText(ei.subtitle);
            }
        }
        return v;
    }

}
EntryItem
public class EntryItem implements Item{

    public final String title;
    public final String subtitle;

    public EntryItem(String title, String subtitle) {
        this.title = title;
        this.subtitle = subtitle;
    }

    @Override
    public boolean isSection() {
        return false;
    }

}

项目

public interface Item {

    public boolean isSection();

}
SectionItem(片段项)
public class SectionItem implements Item{

    private final String title;

    public SectionItem(String title) {
        this.title = title;
    }

    public String getTitle(){
        return title;
    }

    @Override
    public boolean isSection() {
        return true;
    }

}

MainActivity

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.androidbegin.item.EntryAdapter;
import com.androidbegin.item.EntryItem;
import com.androidbegin.item.Item;
import com.androidbegin.item.SectionItem;

public class MainActivity extends SherlockFragmentActivity {

    // Declare Variable
    DrawerLayout mDrawerLayout;
    ListView mDrawerList;
    ActionBarDrawerToggle mDrawerToggle;
    MenuListAdapter mMenuAdapter;
    String[] title;
    String[] subtitle;
    int[] icon;
    Fragment fragment1 = new Fragment1();
    Fragment fragment2 = new Fragment2();
    Fragment fragment3 = new Fragment3();
    Context context;

    ArrayList<Item> items = new ArrayList<Item>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawer_main);
        this.context = this;
        // Generate title
        title = new String[] { "Title Fragment 1", "Title Fragment 2",
                "Title Fragment 3" };

        // Generate subtitle
        subtitle = new String[] { "Subtitle Fragment 1", "Subtitle Fragment 2",
                "Subtitle Fragment 3" };

        // Generate icon
        icon = new int[] { R.drawable.action_about, R.drawable.action_settings,
                R.drawable.collections_cloud };

        // Locate DrawerLayout in drawer_main.xml
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        // Locate ListView in drawer_main.xml
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // Set a custom shadow that overlays the main content when the drawer
        // opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);

        // Pass results to MenuListAdapter Class
//      mMenuAdapter = new MenuListAdapter(this, title, subtitle, icon);

        // Set the MenuListAdapter to the ListView
//      mDrawerList.setAdapter(mMenuAdapter);

        items.add(new SectionItem("Category 1"));
        items.add(new EntryItem("Item 1", "This is item 1.1"));
        items.add(new EntryItem("Item 2", "This is item 1.2"));
        items.add(new EntryItem("Item 3", "This is item 1.3"));


        items.add(new SectionItem("Category 2"));
        items.add(new EntryItem("Item 4", "This is item 2.1"));
        items.add(new EntryItem("Item 5", "This is item 2.2"));
        items.add(new EntryItem("Item 6", "This is item 2.3"));
        items.add(new EntryItem("Item 7", "This is item 2.4"));

        items.add(new SectionItem("Category 3"));
        items.add(new EntryItem("Item 8", "This is item 3.1"));
        items.add(new EntryItem("Item 9", "This is item 3.2"));
        items.add(new EntryItem("Item 10", "This is item 3.3"));
        items.add(new EntryItem("Item 11", "This is item 3.4"));
        items.add(new EntryItem("Item 12", "This is item 3.5"));

        EntryAdapter adapter = new EntryAdapter(this, items);

        mDrawerList.setAdapter(adapter);

        // Capture button clicks on side menu
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // Enable ActionBar app icon to behave as action to toggle nav drawer
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open,
                R.string.drawer_close) {

            public void onDrawerClosed(View view) {
                // TODO Auto-generated method stub
                super.onDrawerClosed(view);
            }

            public void onDrawerOpened(View drawerView) {
                // TODO Auto-generated method stub
                super.onDrawerOpened(drawerView);
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == android.R.id.home) {

            if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
                mDrawerLayout.closeDrawer(mDrawerList);
            } else {
                mDrawerLayout.openDrawer(mDrawerList);
            }
        }

        return super.onOptionsItemSelected(item);
    }

    // The click listener for ListView in the navigation drawer
    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            Log.i("LIST", "item position = " + Integer.toString(position)
                    + "\nitem id = " + String.valueOf(id));
            if (!items.get(position).isSection()) {
                EntryItem item = (EntryItem)items.get(position);

                Toast.makeText(context, "You clicked " + item.title , Toast.LENGTH_SHORT).show();

                selectItem(position);
            }
//          selectItem(position);
        }
    }

    private void selectItem(int position) {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        // Locate Position
        switch (position) {
            case 0:
                ft.replace(R.id.content_frame, fragment1);
                break;
            case 1:
                ft.replace(R.id.content_frame, fragment2);
                break;
            case 2:
                ft.replace(R.id.content_frame, fragment3);
                break;
        }
        ft.commit();
        mDrawerList.setItemChecked(position, true);
        // Close drawer
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
}

但我有一个小问题:部分内容在列表中占据了位置,选取片段时会出现混乱。

7个回答

6
您可以像在 Android 中的任何其他地方一样,在 ListView 中添加头部/分区,以便在 DrawerLayout 中使用。在低级别上,这涉及到一个 ListAdapter
- 重写 getViewTypeCount() 方法以指示有多少不同类型的行(例如,2,一个用于标题,一个用于普通行)。 - 重写 getItemViewType() 方法以指示要为给定的 position 使用哪个行类型。 - 确保 getView()(或 CursorAdapternewView()/bindView())知道多个行类型并相应地处理它们。
在更高的级别上,您可以尝试使用像我的MergeAdapter或其他各种第三方库来帮助简化这个过程。

谢谢,我会尝试使用你的库,虽然它看起来需要花费很多功夫,但这应该是默认存在的东西。 如果你知道其他关于如何实现这个的教程,我将非常感激。 - Ionut Negru
1
@IonutNegru:在您喜欢的搜索引擎上简单搜索“android listview section headers”将会出现许多页面,包括库(例如http://code.google.com/p/android-amazing-listview/),StackOverflow问题(例如https://dev59.com/oGsz5IYBdhLWcg3wYGkz),教程/食谱(例如http://androidcookbook.com/Recipe.seam?recipeId=992)等等。 - CommonsWare

1
这里有一个使用标题/分组的导航抽屉完整示例
这是结果。

enter image description here


1
在导航抽屉中,项目列表可以使用ListView显示,因此您可以拥有一个类适配器并实现自己的逻辑。这样,您可以添加部分、标题等等。

我该如何实现呢?我已经有了一个BaseAdapter来填充列表(因为我为列表添加了图片和副标题)?我在主帖中已经添加了适配器。 - Ionut Negru
只需实现一个自定义适配器并覆盖像getView等方法即可。 - FrancescoAzzola
你可以看到我已经这样做了,但是你建议如何将标题添加到导航抽屉的列表中? - Ionut Negru

0
你需要将这段代码添加到EntryAdapter类中:
@Override
public boolean areAllItemsEnabled () {
    return false;
}

有了那个,还有:

@Override
public boolean isEnabled(int position) {
    return !getItem(position).isSection();
}

区块不应该占据 ListView 的位置。


0
一个好的解决方案是在你的行布局文件中放置一个标题 TextView,并将其可见性设置为 GONE
然后在你的适配器的 getView 中,有一些逻辑来判断:这是列表中的第一个项目(位置 0),还是此项的类型与上面一项的类型不同?如果是,将标题 TextView 的可见性设置为 VISIBLE
这种方式更受欢迎,因为当你想使用 getItemAtPosition 时,你不必想办法避开你的部分标题,因为如果你按照 OP 和其他人建议的方式实现它们,它们将占据整个位置。

0
如果您的列表项是固定的(不会改变),一个快速的“hack”方法是在适配器的getView()方法中包含一个“position”的switch case,并在这些固定位置上充气headerlayout.xml。您的常规充气将进入switch case的默认部分。这很不规范,但有效。

我想避免这种黑客行为,因为将来如果需要修改将会很困难。但还是谢谢你的提示。 - Ionut Negru

0
我建议通过添加一个标签成员来扩展 EntryItem,以告诉您要创建什么类型的片段。然后,在您的 onItemClick 处理程序中检查标签,以创建正确类型的片段。这样,您就不必依赖位置,因为在添加/删除部分中位置可能会发生变化。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接