gpt4 book ai didi

android - 如何使ListView中的Header View不可选?

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

我正在创建自定义 ListView ,如 Android ListView headers 中所述.

这是我的Item界面

公共(public)接口(interface)项{
public int getViewType();
public View getView(LayoutInflater inflater, View convertView);
}

这是我的标题类

public class Header implements Item {

private final String name;

public Header(String name) {
this.name = name;
}

@Override
public int getViewType() {
return TwoTextArrayAdapter.rowType.HEADER_ITEM.ordinal();
}

@Override
public View getView(LayoutInflater inflater, View convertView) {
View rootView;
if (convertView == null) {
rootView = (View) inflater.inflate(R.layout.nav_header, null);
} else {
rootView = convertView;
}

TextView text = (TextView) rootView.findViewById(R.id.separator);
text.setText(name);

rootView.setEnabled(false);

return rootView;
}

这是我的列表类

public class ListItem implements Item {

private final String item;

public ListItem(String item) {
this.item = item;
}

@Override
public int getViewType() {
return TwoTextArrayAdapter.rowType.LIST_ITEM.ordinal();
}

@Override
public View getView(LayoutInflater inflater, View convertView) {
View rootView;

if (convertView == null) {
rootView = (View) inflater.inflate(android.R.layout.simple_list_item_activated_1, null);
} else {
rootView = convertView;
}

TextView text = (TextView) rootView.findViewById(android.R.id.text1);
text.setText(item);

return rootView;
}
}

此外,我正在使用从 ArrayAdapter 扩展而来的 TwoTextArrayAdapter

public class TwoTextArrayAdapter extends ArrayAdapter<Item> {

private LayoutInflater mInflater;

public enum rowType {
LIST_ITEM, HEADER_ITEM
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getItem(position).getView(mInflater, convertView);
}

我是这样用的

mDrawerListView = (ListView) inflater.inflate(R.layout.fragment_navigation_list, container, false);
mDrawerListView.setFitsSystemWindows(true);

mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});

List<Item> services = new ArrayList<Item>();
services.add(new Header("Call Management"));
services.add(new ListItem("Service 01"));
services.add(new ListItem("Service 02"));

TwoTextArrayAdapter adapter = new TwoTextArrayAdapter(getActivity(), services);
mDrawerListView.setAdapter(adapter);

我的问题是,当我为 ListView 设置 onClickListener() 时,所有项目(包括标题项目)都被视为可点击项目。我需要确保 Android 忽略标题项并允许用户仅单击 ListItem 中的项。

我该怎么做?

谢谢

最佳答案

覆盖 TwoTextArrayAdapter 中的 isEnabled。比如

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

here文档

关于android - 如何使ListView中的Header View不可选?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22688804/

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