gpt4 book ai didi

android - 如何在 ListView 适配器类中实现 2 种不同类型的分隔符(即标题)

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:10:07 24 4
gpt4 key购买 nike

我通过这组代码调用适配器:

mAdapter = new MyCustomAdapter(getActivity());

mAdapter.addSeparatorItem(new ContentWrapper(q.get(0).getA_name(),null));
mAdapter.addItem(new ContentWrapper(q.get(0).getAS_name(), q.get(0).getDesc_art()));

考虑这段代码:

private class MyCustomAdapter extends BaseAdapter {

private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

private ArrayList<ContentWrapper> mData = new ArrayList<ContentWrapper>();
private LayoutInflater mInflater;

private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

public MyCustomAdapter(Context context)
{
mInflater = LayoutInflater.from(context);
}

public void addItem(ContentWrapper value) {
mData.add(value);
notifyDataSetChanged();
}

public void addSeparatorItem(ContentWrapper value) {
mData.add(value);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}

public ContentWrapper getItem(int position) {
return mData.get(position);
}
@Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}

@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}

public int getCount() {
return mData.size();
}

public long getItemId(int position) {
Log.v("getItemId Position", ""+position);
return position;

}

public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.white, null);
holder.textView = (TextView)convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.black, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
count++;
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
} holder.textView.setText(mData.get(position).getItem());

if (type == TYPE_ITEM) {
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setIcon(R.drawable.ic_launcher);
final String title = mData.get(position).getItem();
builder.setTitle(title);
builder.setMessage(mData.get(position).getItemDescription());
builder.setCancelable(false);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
AlertDialog alertDialog = (AlertDialog) dialog;
ViewGroup viewGroup = (ViewGroup) alertDialog.getWindow()
.getDecorView();
TextView textView = findTextViewWithTitle(viewGroup, title);
if (textView != null) {
textView.setEllipsize(null);
textView.setMaxHeight((int) (100 * alertDialog.getContext().getResources().getDisplayMetrics().density));
textView.setMovementMethod(new ScrollingMovementMethod());
}
}
});
alertDialog.show();
}

private TextView findTextViewWithTitle(ViewGroup viewGroup, String title) {
for (int i = 0, N = viewGroup.getChildCount(); i < N; i++) {
View child = viewGroup.getChildAt(i);
if (child instanceof TextView) {
TextView textView = (TextView) child;
if (textView.getText().equals(title)) {
return textView;
}
} else if (child instanceof ViewGroup) {
ViewGroup vGroup = (ViewGroup) child;
return findTextViewWithTitle(vGroup, title);
}
}
return null;
}
});
} else {
holder.textView.setOnClickListener(null);
}

return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}

public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}

此代码仅在 AlertDialog 中显示所选项目(此处为 TYPE_ITEM)的描述。

如您所见,TYPE_SEPERATOR 已禁用 onClick(),我想再添加一个 TYPE_SEPERATOR_GRAY(来自 gray.xml)是禁用 onClick() 的另一种类型分隔符。

我是否需要再添加一种类似于 addSeparatorItem(ContentWrapper value) 的方法,例如 addSeparatorItemGray(ContentWrapper value)。我知道我必须在 getView() 的开关中再添加一个案例来膨胀 gray.xml

或者我还应该添加/修改什么?

编辑:ContentWrapper 包含项目文本及其描述。我实现了 ContentWrapper 来为每个 TYPE_ITEM 分配一个描述

public class ContentWrapper {

private String mItem, mItemDescription;

public ContentWrapper(String item, String itemDescription) {
mItem = item;
mItemDescription = itemDescription;
}

public String getItem() {
return mItem;
}

public String getItemDescription() {
return mItemDescription;
}
}

mAdapter 是 MyCustomAdapter 类型。

我的问题的前 3-4 行说 addSeparatorItem 没有任何描述,所以在第二个参数中传递了 null & addItem 有两个文本,描述。

我想在列表中的某些指定位置手动添加另一个 TYPE_GRAY_SEPARATOR ,例如:

 mAdapter.addSeparatorItemGray("HI after 1st view");
mAdapter.addSeparatorItemGray("HI after 23rd view");
mAdapter.addSeparatorItemGray("HI after 45 view");

最佳答案

getViewType 方法应返回 3(列表项 + 分隔符 + 灰色分隔符)。因此将 TYPE_MAX_COUNT 设置为 3。

private static final int TYPE_GRAY_SEPARATOR = 2;
private static final int TYPE_MAX_COUNT = TYPE_GRAY_SEPARATOR + 1;

保存灰色分隔符位置的数据结构:

private TreeSet<Integer> mGraySeparatorsSet = new TreeSet<Integer>();

添加灰色分隔符的方法。

public void addGraySeparatorItem(ContentWrapper value) {
mData.add(value);
// save separator position
mGraySeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}

getItemViewType 方法应该根据位置返回适当的 View 。

@Override
public int getItemViewType(int position) {
int viewType = TYPE_ITEM;
if(mSeparatorSet.contains(position))
viewType = TYPE_SEPARATOR;
else if(mGraySeparatorSet.contains(position)) {
viewType = TYPE_GRAY_SEPARATOR;
}
return viewType;
}

getView 方法应该处理 TYPE_GRAY_SEPARATOR:

public View getView(final int position, View convertView, ViewGroup parent) {
// Existing code
switch(type) {
// Existing cases
case TYPE_GRAY_SEPARATOR:
// Inflate appropriate view
break;
}
// Existing code
}

关于android - 如何在 ListView 适配器类中实现 2 种不同类型的分隔符(即标题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22954124/

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