gpt4 book ai didi

java - 回收 View 时抽屉导航项目消失

转载 作者:行者123 更新时间:2023-12-02 05:28:23 27 4
gpt4 key购买 nike

当我第一次启动应用程序时,我以正确的方式获得了抽屉导航。当我向下滚动抽屉时,问题就出现了。元素开始消失,其中一些甚至交换了位置。我试图找出问题所在,但找不到。希望你能帮助我。

我已经使用 getItemViewType(int)getViewTypeCount() 更新了我的代码,但回收 View 时问题仍然存在。

我在这里留下一些代码:

新代码

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>

最佳答案

您在使用 ListView 和适配器时遇到问题。

getView 方法中的适配器重用“旧”项目 View 。并且您必须重新设置所有内容

我的意思是:如果您使用 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");
}

关于java - 回收 View 时抽屉导航项目消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25782404/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com