我从这里获得了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) {
}
}
);
但是我不知道如何确定单击/触摸事件是否发生在一个小组或孩子身上。
有任何想法吗?
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/