首次启动应用时,我以正确的方式获取了导航抽屉。当我向下滚动抽屉时出现问题。项目即将消失,其中一些甚至交换位置。我试图找出错误但我不能。希望你能帮助我。
我已使用getItemViewType(int)
和getViewTypeCount()
更新了我的代码,但在回收视图时仍存在问题。
我在这里留下一些代码:
新代码
public class NavDrawerListAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_HEADER = 1;
private static final int TYPE_SWITCH = 2;
private static final int TYPE_MAX_COUNT = 3;
private Context mContext;
private ArrayList<NavDrawerItem> navDrawerItems;
LayoutInflater inflater;
public NavDrawerListAdapter(Context context, ArrayList<NavDrawerItem> navDrawerItems){
this.mContext = context;
this.navDrawerItems = navDrawerItems;
inflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}
@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 int getItemViewType(int position) {
// TODO Auto-generated method stub
//return super.getItemViewType(position);
if((!navDrawerItems.get(position).getHeaderVisibility())
&&(!navDrawerItems.get(position).getSwitchVisibility()))
return TYPE_ITEM;
else if((navDrawerItems.get(position).getHeaderVisibility())
&&(!navDrawerItems.get(position).getSwitchVisibility()))
return TYPE_HEADER;
else if((!navDrawerItems.get(position).getHeaderVisibility())
&&(navDrawerItems.get(position).getSwitchVisibility()))
return TYPE_SWITCH;
else return TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
//return super.getViewTypeCount();
return TYPE_MAX_COUNT;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.d("NavDrawerAdapterTest: ", "getView: " + position + " " + convertView);
Log.d("NavDrawerAdapter: ", "0");
ViewHolderItem viewHolder;
int type = getItemViewType(position);
Log.d("NavDrawerAdapter: ", "1");
if (convertView == null) {
Log.d("NavDrawerAdapter: ", "2a");
/*LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);*/
viewHolder = new ViewHolderItem();
switch (type) {
case TYPE_ITEM:
convertView = inflater.inflate(R.layout.drawer_list_item, null);
viewHolder.textViewTitleItem = (TextView) convertView.findViewById(R.id.title);
viewHolder.imageViewIconItem = (ImageView) convertView.findViewById(R.id.icon);
viewHolder.imageViewIconItem.setImageResource(navDrawerItems.get(position).getIcon());
viewHolder.textViewTitleItem.setText(navDrawerItems.get(position).getTitle());
break;
case TYPE_HEADER:
convertView = inflater.inflate(R.layout.drawer_list_item_header, null);
viewHolder.textViewHeaderItem = (TextView) convertView.findViewById(R.id.drawerHeader);
viewHolder.dividerViewItem = (View) convertView.findViewById(R.id.drawerView);
viewHolder.textViewHeaderItem.setText(navDrawerItems.get(position).getHeader());
break;
case TYPE_SWITCH:
convertView = inflater.inflate(R.layout.drawer_list_item_switch, null);
break;
}
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolderItem)convertView.getTag();
}
return convertView;
}
static class ViewHolderItem {
TextView textViewTitleItem;
ImageView imageViewIconItem;
TextView textViewHeaderItem;
View dividerViewItem;
}
}
原始代码
NavDrawerItem.java
public class NavDrawerItem {
private String title;
private int icon;
private String header;
private boolean isHeader;
public NavDrawerItem(){}
public NavDrawerItem(String title, int icon){
this.title = title;
this.icon = icon;
this.isHeader = false;
}
public NavDrawerItem(String header) {
this(null, 0);
this.header = header;
this.isHeader = true;
}
public String getTitle(){
return this.title;
}
public int getIcon(){
return this.icon;
}
public String getHeader() {
return header;
}
public boolean getHeaderVisibility() {
return isHeader;
}
public void setTitle(String title){
this.title = title;
}
public void setIcon(int icon){
this.icon = icon;
}
public void setHeader(String header) {
this.header = header;
}
public void setHeaderVisibility(boolean isHeader) {
this.isHeader = isHeader;
}
}
NavDrawerListAdapter.java
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) {
Log.d("NavDrawerAdapter: ", "0");
ViewHolderItem viewHolder;
Log.d("NavDrawerAdapter: ", "1");
if (convertView == null) {
Log.d("NavDrawerAdapter: ", "2a");
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
viewHolder = new ViewHolderItem();
convertView = inflater.inflate(R.layout.drawer_list_item_test, null);
viewHolder.textViewTitleItem = (TextView) convertView.findViewById(R.id.title);
viewHolder.imageViewIconItem = (ImageView) convertView.findViewById(R.id.icon);
viewHolder.textViewHeaderItem = (TextView) convertView.findViewById(R.id.drawerHeader);
viewHolder.dividerViewItem = (View) convertView.findViewById(R.id.drawerView);
convertView.setTag(viewHolder);
} else {
Log.d("NavDrawerAdapter: ", "2b");
viewHolder = (ViewHolderItem) convertView.getTag();
}
if(navDrawerItems.get(position).getHeaderVisibility()) {
viewHolder.textViewTitleItem.setVisibility(TextView.INVISIBLE);
viewHolder.imageViewIconItem.setVisibility(ImageView.INVISIBLE);
viewHolder.textViewHeaderItem.setText(navDrawerItems.get(position).getHeader());
Log.d("NavDrawerAdapter: ", "3a");
} else {
viewHolder.textViewHeaderItem.setVisibility(TextView.INVISIBLE);
viewHolder.dividerViewItem.setVisibility(View.INVISIBLE);
viewHolder.imageViewIconItem.setImageResource(navDrawerItems.get(position).getIcon());
viewHolder.textViewTitleItem.setText(navDrawerItems.get(position).getTitle());
Log.d("NavDrawerAdapter: ", "3b");
}
return convertView;
}
static class ViewHolderItem {
TextView textViewTitleItem;
ImageView imageViewIconItem;
TextView textViewHeaderItem;
View dividerViewItem;
}
}
编辑:drawer_list_item_test.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="48dp"
android:background="@drawable/list_selector">
<ImageView
android:id="@+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="@string/desc_list_item_icon"
android:src="@drawable/ic_home"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@id/icon"
android:minHeight = "48dp"
android:textSize = "16sp"
android:textColor="@color/list_item_title"
android:gravity="center_vertical"
android:paddingRight="40dp"/>
<TextView
android:id="@+id/drawerHeader"
android:layout_above="@id/drawerView"
android:layout_marginLeft="12dp"
android:layout_marginTop="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textAllCaps="true"
android:textColor="@color/list_item_title" />
<View
android:id="@+id/drawerView"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginBottom="1dp"
android:layout_marginTop="2dp"
android:background="@color/list_background_pressed" />
</RelativeLayout>
答案 0 :(得分:1)
列表视图和适配器有问题。
getView
方法中的适配器重用“旧”项目视图。并且您必须重新设置所有内容
我的意思是:如果您在一个案例中使用viewHolder.imageViewIconItem.setVisibility(ImageView.INVISIBLE);
,则必须在第二个案例中使用viewHolder.imageViewIconItem.setVisibility(ImageView.VISIBLE);
这里有一些可以帮助你的代码:
if(navDrawerItems.get(position).getHeaderVisibility()) {
viewHolder.textViewTitleItem.setVisibility(TextView.INVISIBLE);
viewHolder.imageViewIconItem.setVisibility(ImageView.INVISIBLE);
viewHolder.textViewHeaderItem.setVisibility(TextView.VISIBLE);
viewHolder.dividerViewItem.setVisibility(View.VISIBLE);
viewHolder.textViewHeaderItem.setText(navDrawerItems.get(position).getHeader());
Log.d("NavDrawerAdapter: ", "3a");
} else {
viewHolder.textViewHeaderItem.setVisibility(TextView.INVISIBLE);
viewHolder.dividerViewItem.setVisibility(View.INVISIBLE);
viewHolder.textViewTitleItem.setVisibility(TextView.VISIBLE);
viewHolder.imageViewIconItem.setVisibility(ImageView.VISIBLE);
viewHolder.imageViewIconItem.setImageResource(navDrawerItems.get(position).getIcon());
viewHolder.textViewTitleItem.setText(navDrawerItems.get(position).getTitle());
Log.d("NavDrawerAdapter: ", "3b");
}
答案 1 :(得分:1)
我发现了为什么我无法实现我想要的问题。我将它留在这里只是为了那些可能遇到同样问题的人。
当我在viewHolder中设置文本和图像时,我只是在if(convertView == null){...}
内进行。相反,我在if(){...}else{...}
之后接受了那部分。
因此,else{viewHolder = (ViewHolderItem)convertView.getTag();}
之后我编写了另一个switch(position)
以及针对我的不同布局类型的setText()
和setImageResource()
的相同情况。
希望它对某人有用。