gpt4 book ai didi

java - Android ExpandedListView 希望 SwipeToDismiss 仅适用于群组

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

我从这里得到了似乎是实际版本的 SwipeToDismiss 实现:google code / Roman Nurik .

而且效果很好。但是,我在 ExpandedListView 上使用它,我只希望 Groups 可以忽略。

在 TouchListener 事件中,似乎有两个地方我可以干预并且不允许解散:

mListView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// return false if not on a group ???????
return mSwipeDismissTouchListener.onTouch(view, motionEvent);
..

或在 SwipeDismissListViewTouchListener 事件中:

mSwipeDismissTouchListener = new SwipeDismissListViewTouchListener(
mListView,
new SwipeDismissListViewTouchListener.DismissCallbacks() {
public boolean canDismiss(int position) {
// return false if not on a group ???????
}
public void onDismiss(ListView listView, int[] reverseSortedPositions) {
}
}
);

但我不知道如何判断点击/触摸事件是发生在组还是 child 身上。

有人有什么想法吗?

TIA。

最佳答案

您需要采用您的适配器类并使用 ViewHolder Pattern但有一些修改:公开,公开你的领域,像这样

public static class ViewHolderGroup {
TextView tvName;
ImageView ivLogo;
public boolean isGroup;
}

并将一些变量设置为Tag , 这将定义 wheater

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ViewHolderGroup holder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_group_operator, null);
holder = new ViewHolderGroup();
holder.tvName = (TextView) convertView.findViewById(R.id.layout_row_group_operator_tv_Name);
holder.ivLogo = (ImageView) convertView.findViewById(R.id.layout_row_group_operator_iv_Logo);
holder.ivLogo.setVisibility(View.GONE);
holder.isGroup = true; // Here we setting field to know this is group
convertView.setTag(holder);

} else {
holder = (ViewHolderGroup) convertView.getTag();
}
....
}

然后在你之前描述的代码中得到这个标签,像这样

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
fa = this;
View rootView = inflater.inflate(R.layout.fg_elist, container, false);

final ExpandableListView elMain = (ExpandableListView) rootView.findViewById(R.id.layout_fg_elist_elv_Main);
RouteExpandableListAdapter routesAdapter = new RouteExpandableListAdapter(Rendis.mContext, Rendis.dates, Rendis.routes);
elMain.setAdapter(routesAdapter);
registerForContextMenu(elMain);
elMain.setOnChildClickListener(this);

elMain.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent motionEvent) {

Rect rect = new Rect();
int childCount = elMain.getChildCount();
int[] listViewCoords = new int[2];
elMain.getLocationOnScreen(listViewCoords);
int x = (int) motionEvent.getRawX() - listViewCoords[0];
int y = (int) motionEvent.getRawY() - listViewCoords[1];
View child;
for (int i = 0; i < childCount; i++) {
child = elMain.getChildAt(i);
child.getHitRect(rect);

if (rect.contains(x, y)) {
RouteExpandableListAdapter.ViewHolderGroup holder = (RouteExpandableListAdapter.ViewHolderGroup) child.getTag();
if(holder.isGroup){
// DO WHAT YOU WANT TO DO
}
break;
}
}

return false;
}
});
return rootView;
}

希望对你有帮助

关于java - Android ExpandedListView 希望 SwipeToDismiss 仅适用于群组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25077132/

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