gpt4 book ai didi

android 自定义 Listview 项目困惑

转载 作者:行者123 更新时间:2023-11-29 15:51:15 26 4
gpt4 key购买 nike

大家好 :) 我创建了自定义 ListView ,但是如果项目超过一定数量, ListView 就会变得奇怪。元素杂乱无章。



我添加了这样的项目:

mItems.add(new VenuesListHeader("Barra"));
mItems.add(new VenuesListItem("Olympic gold course"));
mItems.add(new VenuesListItem("Pontal"));
mItems.add(new VenuesListItem("Barra olympic park"));
mItems.add(new VenuesListItem("Riocentro"));

mItems.add(new VenuesListHeader("Copacabana"));
mItems.add(new VenuesListItem("Beach volleyball arena"));
mItems.add(new VenuesListItem("Flamengo park"));
mItems.add(new VenuesListItem("Fort copacabana"));
mItems.add(new VenuesListItem("Lagoa stadium"));
mItems.add(new VenuesListItem("Marina da gloria"));

mItems.add(new VenuesListHeader("Deodora"));
mItems.add(new VenuesListItem("Drodora aquatics centre"));
mItems.add(new VenuesListItem("Drodora stadium"));
mItems.add(new VenuesListItem("Olympic equestrian centre"));
mItems.add(new VenuesListItem("Olympic hockey centre"));
mItems.add(new VenuesListItem("Olympic shooting centre"));
mItems.add(new VenuesListItem("Youth arena"));
mItems.add(new VenuesListItem("X-park"));

mItems.add(new VenuesListHeader("Maracana"));
mItems.add(new VenuesListItem("Olympic stadium"));
mItems.add(new VenuesListItem("Sambodromo"));
mItems.add(new VenuesListItem("Maracana"));

但是从第三个header项开始就乱了。(我试了几次,header的个数不重要。)

enter image description here



这是我的代码:

View mHeader;
ListView mListView;
ArrayList<Item> mItems;
VenuesListAdapter mAdapter;

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

//listview set list
mListView = (ListView)rootView.findViewById(R.id.venues_list);
mHeader = inflater.inflate(R.layout.rio2016_venues_map, mListView, false);
mItems = new ArrayList<>();

setList("all");

mAdapter = new VenuesListAdapter(getActivity(), mItems);

mListView.addHeaderView(mHeader);
mListView.setAdapter(mAdapter);
}


void setList(String type){
switch (type){
case "barra":
mItems.clear();
case "all_barra":
mItems.add(new VenuesListHeader("Barra"));
mItems.add(new VenuesListItem("Olympic gold course"));
mItems.add(new VenuesListItem("Pontal"));
mItems.add(new VenuesListItem("Barra olympic park"));
mItems.add(new VenuesListItem("Riocentro"));
break;

case "copacabana":
mItems.clear();
case "all_copacabana":
mItems.add(new VenuesListHeader("Copacabana"));
mItems.add(new VenuesListItem("Beach volleyball arena"));
mItems.add(new VenuesListItem("Flamengo park"));
mItems.add(new VenuesListItem("Fort copacabana"));
mItems.add(new VenuesListItem("Lagoa stadium"));
mItems.add(new VenuesListItem("Marina da gloria"));
break;

case "deodoro":
mItems.clear();
case "all_deodoro":
mItems.add(new VenuesListHeader("Deodora"));
mItems.add(new VenuesListItem("Drodora aquatics centre"));
mItems.add(new VenuesListItem("Drodora stadium"));
mItems.add(new VenuesListItem("Olympic equestrian centre"));
mItems.add(new VenuesListItem("Olympic hockey centre"));
mItems.add(new VenuesListItem("Olympic shooting centre"));
mItems.add(new VenuesListItem("Youth arena"));
mItems.add(new VenuesListItem("X-park"));
break;

case "maracana":
mItems.clear();
case "all_maracana":
mItems.add(new VenuesListHeader("Maracana"));
mItems.add(new VenuesListItem("Olympic stadium"));
mItems.add(new VenuesListItem("Sambodromo"));
mItems.add(new VenuesListItem("Maracana"));
break;

case "all":
mItems.clear();
setList("all_barra");
setList("all_copacabana");
setList("all_copacabana");
setList("all_maracana");
break;
}
}

如果你需要,这是自定义适配器

public class VenuesListAdapter extends ArrayAdapter<Item>{

private static final int TYPE_ITEM = 0;
private static final int TYPE_HEADER = 1;

private LayoutInflater mInflater;

public VenuesListAdapter(Context context, List<Item> items) {
super(context, 0, items);
mInflater = LayoutInflater.from(context);
}

public enum RowType {
LIST_ITEM, HEADER_ITEM
}

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

@Override
public int getViewTypeCount() {
return RowType.values().length;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int rowType = getItemViewType(position);

if(convertView == null){
holder = new ViewHolder();

switch (rowType){
case TYPE_ITEM :
convertView = mInflater.inflate(R.layout.rio2016_venues_item, null);
holder.View = getItem(position).getView(mInflater, convertView);
break;
case TYPE_HEADER :
convertView = mInflater.inflate(R.layout.rio2016_venues_header, null);
holder.View = getItem(position).getView(mInflater, convertView);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

return convertView;
}

public static class ViewHolder {
public View View;
}
}

(即使不使用递归函数也会出现同样的问题。)



XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
android:id="@+id/venues_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

元素适配器

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:background="@color/white"
android:padding="15dp">


<TextView
android:id="@+id/venues_list_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:layout_centerVertical="true"/>

</RelativeLayout>

header 适配器:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="@color/nav_header_background">

<TextView
android:id="@+id/venues_list_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:layout_centerVertical="true"/>

</RelativeLayout>



这种现象为什么会发生呢?我该怎么做才能解决这个问题?

最佳答案

更改您的代码并尝试以下操作:

 public class VenuesListAdapter extends ArrayAdapter<Item>{

private static final int TYPE_ITEM = 0;
private static final int TYPE_HEADER = 1;

private LayoutInflater mInflater;

public VenuesListAdapter(Context context, List<Item> items) {
super(context, 0, items);
mInflater = LayoutInflater.from(context);
}

public enum RowType {
LIST_ITEM, HEADER_ITEM
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 2;
}

@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub


if(position == TYPE_ITEM)
{return 1;}
else{return 0;}


}

@Override
public int getViewTypeCount() {
return RowType.values().length;
}

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


if(convertView == null){
holder = new ViewHolder();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

int type=getItemViewType(position);
if(type==1){
convertView = mInflater.inflate(R.layout.rio2016_venues_item, null);
holder.View = getItem(position).getView(mInflater, convertView);
}else{
convertView = mInflater.inflate(R.layout.rio2016_venues_header, null);
holder.View = getItem(position).getView(mInflater, convertView);
}
return convertView;
}

public static class ViewHolder {
public View View;
}
}

这是长 ListView 的常见问题可以解决使用:

@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return integer(number of different cases in your list);
}



@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub

}

关于android 自定义 Listview 项目困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29935237/

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