- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我开发了一个基于 Material Design 的抽屉导航,其中包含一个标题 View ,遵循本教程:How To Make Material Design Navigation Drawer With Header View .
我试图在其下方添加一个带有类别名称的分隔符,但与我的分隔符相对应的数据集位置似乎有问题。这意味着分隔符加载正确,但不在我想要的位置。基本上,我尝试做的是将“DrawerItem”对象的 ArrayList 传递到扩展 RecyclerView 的“MyAdapter”类中。 arraylist 包含所有内容:- header 的参数-项目名称和图标- 和分隔符,也将自己设计为 DrawerItem 对象,但只有一个字符串作为标题(在我的例子中是“其他”)。
这是我的代码:
DrawerItem:
package madapps.materialdesignappbar;
public class DrawerItem {
String ItemName;
int imgResID;
String title;
String name ;
String email ;
int profile ;
public DrawerItem(String Name, String Email, int profileResID){
name = Name;
email = Email;
profile = profileResID;
}
public DrawerItem(String itemName, int imgResID) {
ItemName = itemName;
this.imgResID = imgResID;
}
//separator case
public DrawerItem(String title) {
this(null, 0);
this.title = title;
this.ItemName = "separator";
}
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
public String getItemName() {
return ItemName;
}
public int getImgResID() {
return imgResID;
}
public void setItemName(String itemName) {
this.ItemName = itemName;
}
public void setImgResID(int imgResID) {
this.imgResID = imgResID;
}
public void setName(String Name){
this.name = Name;
}
public void setEmail(String Email){
this.email = Email;
}
public void setProfile(int Profile){
this.profile = Profile;
}
public String getName(){
return name;
}
public String getEmail(){
return email;
}
public int getProfile(){
return profile;
}
}
MyAdapter
package madapps.materialdesignappbar;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private static final int TYPE_HEADER = 0; // Declaring Variable to Understand which View is being worked on
// IF the view under inflation and population is header or Item
private static final int TYPE_ITEM = 1;
private static final int TYPE_SEPARATOR = 2;
private ArrayList<DrawerItem> data;
MyAdapter(ArrayList<DrawerItem> Data){
// MyAdapter Constructor with titles and icons parameter
// titles, icons, name, email, profile pic are passed from the main activity as we have seen earlier
data = Data;
}
// Creating a ViewHolder which extends the RecyclerView View Holder
// ViewHolder are used to to store the inflated views in order to recycle them
public static class ViewHolder extends RecyclerView.ViewHolder {
int Holderid;
LinearLayout itemLayout;
TextView textView;
ImageView imageView;
ImageView profile;
TextView Name;
TextView email;
TextView drawerTitle;
LinearLayout separatorLayout;
// Creating ViewHolder Constructor with View and viewType As a parameter
public ViewHolder(View itemView, int ViewType) {
super(itemView);
// Here we set the appropriate view in accordance with the
// the view type as passed when the holder object is created
if (ViewType == TYPE_HEADER){
Name = (TextView) itemView.findViewById(R.id.name); // Creating Text View object from header.xml for name
email = (TextView) itemView.findViewById(R.id.email); // Creating Text View object from header.xml for email
profile = (ImageView) itemView.findViewById(R.id.circleView); // Creating Image view object from header.xml for profile pic
Holderid = 0; // Setting holder id = 0 as the object being populated are of type header view
}
if(ViewType == TYPE_ITEM) {
textView = (TextView) itemView.findViewById(R.id.rowText); // Creating TextView object with the id of textView from item_row.xml
imageView = (ImageView) itemView.findViewById(R.id.rowIcon); // Creating ImageView object with the id of ImageView from item_row.xml
Holderid = 1; // setting holder id as 1 as the object being populated are of type item row
}
if(ViewType == TYPE_SEPARATOR){
drawerTitle = (TextView) itemView.findViewById(R.id.drawerTitle);
Holderid = 2;
}
}
}
//Below first we Override the method onCreateViewHolder which is called when the ViewHolder is
//Created, In this method we inflate the item_row.xml layout if the viewType is Type_ITEM or else we inflate header.xml
// if the viewType is TYPE_HEADER
// and pass it to the view holder
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_HEADER) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false); //Inflating the layout
ViewHolder vhHeader = new ViewHolder(v,viewType); //Creating ViewHolder and passing the object of type view
return vhHeader; //returning the object created
}else
if (viewType == TYPE_ITEM) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false); //Inflating the layout
ViewHolder vhItem = new ViewHolder(v, viewType); //Creating ViewHolder and passing the object of type view
return vhItem; // Returning the created object
//inflate your layout and pass it to view holder
}
if (viewType == TYPE_SEPARATOR){
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.separator, parent, false);
ViewHolder vhSeparator = new ViewHolder(v, viewType);
return vhSeparator;
}
return null;
}
/* Next we override a method which is called when the item in a row is needed to be displayed,
@param position tells us item at which position is being constructed to be displayed
@param holder id of the holder object tell us which view type is being created 1 for item row */
@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
if(holder.Holderid==0) {
holder.profile.setImageResource(data.get(0).getProfile()); // Similarly we set the resources for header view
holder.Name.setText(data.get(0).getName());
holder.email.setText(data.get(0).getEmail());
}
if(holder.Holderid == 1) {
// as the list view is 1going to be called after the header view so we decrement the
holder.textView.setText(data.get(position).title);
holder.imageView.setImageResource(data.get(position).imgResID);
}if (holder.Holderid==2){
holder.drawerTitle.setText(data.get(4).getItemName());
}
}
// This method returns the number of items present in the list
@Override
public int getItemCount() {
return data.size();
}
// With the following method we check what type of view is being passed
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position)) {//if position == 0 return true
return TYPE_HEADER; //0
}
if(isSeparator(position)){
return TYPE_SEPARATOR;
}
return TYPE_ITEM;
}
private boolean isSeparator(int position){
return position==2;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
}
如您所见,我按以下顺序(来自 MainActivity 的代码)使用三种对象填充 DrawerItem 的 ArrayList:
ArrayList data = new ArrayList<DrawerItem>();
data.add(new DrawerItem("Chris Benois","chris_benois@mail.com",R.mipmap.aka));
data.add( new DrawerItem("Home",R.mipmap.ic_home));
data.add( new DrawerItem("Events",R.mipmap.ic_events));
data.add( new DrawerItem("Mail", R.mipmap.ic_mail));
data.add(new DrawerItem("Others"));
data.add( new DrawerItem("Shop", R.mipmap.ic_shop));
data.add( new DrawerItem("Travel", R.mipmap.ic_travel));
问题是我的“分隔符”分隔符没有出现在我想要的位置(就在“商店”项目之前),而是出现在其他地方,如图所示:
如何将它移动到我想要的位置?
最佳答案
不知道您是否找到了解决方案,但由于您的问题帮助我了解了回收站的观点,我想我会看看是否可以返回。
我发现 ViewHolder 类中最重要的方法是 getItemViewType(int position)。在您发布的代码中,您似乎在没有正确检查抽屉导航中元素的位置的情况下为您的 TYPE_SEPARATOR 返回 2,这就是为什么您的分隔线出现在抽屉导航中主页下方的原因。您需要检查第 5 个位置,然后返回您的 TYPE_SEPARATOR。
我在我的抽屉中使用了分隔符,当它通过传递不同类型的 ViewHolder 到达我的第 6 个元素时创建了一个新布局:
public int getItemViewType(int position) {
if (position == 0) {
return TYPE_HEADER;
} else if (position>0 && position <6) {
return TYPE_TAG;
} else if (position ==6) {
return TYPE_DIVIDER;
}
else
return TYPE_ICON;
}
在我的 ViewHolder 构造函数中:
public ViewHolder(View itemView, int viewType) {
super(itemView);
if(viewType == TYPE_HEADER)
{
name = (TextView) itemView.findViewById(R.id.name);
email = (TextView) itemView.findViewById(R.id.email);
profile = (ImageView) itemView.findViewById(R.id.image_view);
holderId = 0;
}
if (viewType == TYPE_TAG)
{
tagText = (TextView) itemView.findViewById(R.id.tag);
holderId = 1;
}
if(viewType == TYPE_ICON)
{
iconText = (TextView) itemView.findViewById(R.id.rowText);
icon = (ImageView) itemView.findViewById(R.id.rowIcon);
holderId = 2;
}
if(viewType == TYPE_DIVIDER)
{
dividerText = (TextView) itemView.findViewById(R.id.rowText);
dividerIcon = (ImageView) itemView.findViewById(R.id.rowIcon);
holderId = 3;
}
}
}
在我的 onCreateViewHolder 方法中,我只是为 TYPE_DIVIDER 增加了一个不同的布局。
希望对您有所帮助。
编辑
我已经创建了一个可用的回收器 View ,我不建议为你的分隔线膨胀不同的布局。它给我带来了各种各样的问题。您应该使用 RecyclerView.ItemDecoration。这里有一些关于 SO 的精彩帖子,How to add dividers and spaces between items in RecyclerView? .
关于android - 带分隔符的 RecyclerView 抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28931484/
我目前正在试用抽屉小部件。我构建了一个带有汉堡菜单图标、标题文本和搜索图标的应用栏。 这是我的代码: ` import 'package:flutter/material.dart'; import
我仍然是 Flutter 的初学者,我想创建一个带有侧边抽屉和底部导航标签栏的 Flutter 移动应用程序。我创建了带有侧边抽屉和底部导航的应用程序,但我的问题是我想从抽屉和底部导航选项卡打开主页。
试图检查位置固定元素的宽度。单击菜单图标容器后,抽屉的宽度应从 0% 增加到 18%。如果单击文档正文,抽屉应关闭,但前提是抽屉的宽度为 18%。当我尝试将抽屉的宽度记录到控制台时,我得到了 0px,
我在 Flutter 项目中有一个简单的抽屉,我想让抽屉在用户滑动/打开时始终位于底部导航栏上方。我玩弄我的代码,但还找不到任何解决方案。 import 'package:flutter/m
我在抽屉里装了一个粘头。但是现在我在 header 和第一个ListTile项之间有一个空格。 如何删除此空间/将项目设置在标题的末尾? 谢谢你的帮助! (我必须删除一些ListTiles,因为它对于
我想提供一种使 antd 抽屉可调整大小的方法? 我读了一篇热门answer专门针对 material-ui/Drawer 但我希望用 antd 做一些非常相似的事情。 有没有人有类似的 antd 示
我想提供一种使 antd 抽屉可调整大小的方法? 我读了一篇热门answer专门针对 material-ui/Drawer 但我希望用 antd 做一些非常相似的事情。 有没有人有类似的 antd 示
是否有用于绘制/可视化图形的良好 C# 库?我说的是节点路径可视化而不是折线图等。 (最好是原生的,而不是 pstricks 的包装器或类似的东西)谢谢 最佳答案 一些提示: QuickGraph是一
在我的页面中,我有两个具有相同项目的 Material Design Component 抽屉。一个对于桌面/平板电脑显示是永久性的,另一个对于移动显示是隐藏/模态的。 A
我的抽屉 View 没有显示它在我没有对布局或 MainActivity 进行任何更改之前工作正常,现在当我检查它时什么也没显示 我的 XML 抽屉菜单
我正在尝试使用 jQuery 实现类似抽屉的效果。我的页面由两个重叠的 div 组成,其中顶部的 div 已稍微向一侧移动,露出一些底部的 div。 当我将鼠标悬停在底部的 div 上时,我希望顶部的
根据 latest guidelines对于导航栏,它应该放在屏幕的底部,当带有工具栏的抽屉放在屏幕的顶部时。 考虑到这一点,横幅广告应该放在哪里?根据ad placement guidelines它
我的目标是拥有一个用于在不同路线之间导航的抽屉。如果我们当前在该路线上,我想通过更改抽屉中路线名称的颜色使其看起来更好一些。 我的问题是,每次按下汉堡包菜单时,都会显示一个新的抽屉实例,因为在每条 r
我正在尝试使用一些上边距来实现 Material UI 抽屉,而不是从页面的最顶部开始,但它没有发生,我尝试应用 marginTop 但它没有发生。这是codeSandBox链接Drawer . 如何
我将 vue 与 vue-material 一起使用. 我目前正在使用 md-app 布置我的 Web 应用程序的主要结构。作为 md-[app-]drawer 的根目录, md-[app-]tool
抽屉扩展 Activity 的抽屉 fragment 的第二个 Activity 我有一些问题..在导航 activity2 到 Activity1 的 fragment 时 我试过这段代码..
我构建了一个类似抽屉的“窗口”,它会在用户触摸按钮时出现。抽屉从屏幕底部开始动画并填满整个屏幕。 抽屉实际上是一个UITableViewController,因此抽屉里有一张 table 。 如果我有
这是场景:我有一个按钮 B,还有一个滑动抽屉,拉出时会覆盖整个屏幕。当我拉出屏幕并触摸 B 曾经可见的屏幕时,它的 Action 仍在执行。 我该如何解决这个问题? 我找到了 this线程描述了同样的
我正在使用 React Native 为 Android 开发应用程序。我使用 Wix 的 React Native Navigation 包处理导航,但我有疑问。 现在我的 mi 应用程序在我的手机
我想在 Android 应用程序中添加 Material 抽屉...所以我们需要包含一个工具栏...但是当我们包含工具栏时我得到了错误我的 activitymain_xml 代码是
我是一名优秀的程序员,十分优秀!