简体   繁体   English

Android tableview表现为listview

[英]Android tableview behaving as a listview

I am using table layout to display data, but i want it to behave like list items (ability to select, when select change background, when click, having hover effect, click able) for that purpose i am using following listeners 我正在使用表布局来显示数据,但是为此我希望它的行为像列表项(选择功能,选择更改背景,单击,具有悬停效果,单击能够)的能力一样,我正在使用以下侦听器

  • OnClickListener(to perform action)
  • OnFocusChangeListener(To change background color)
  • OnTouchListener(to focus specific row)

Now problem is when user touch any item it get focus first and then have to touch again to fire onclick event, to fix this i made a change and ontouch i fire action on specific to row. 现在的问题是,当用户触摸任何项目时,它会首先获得焦点,然后必须再次触摸以触发onclick事件,以解决此问题,我进行了更改,并针对特定行对ontouch触发了动作。

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() != MotionEvent.ACTION_UP){
        v.requestFocus();
        int viewId= v.getId();
        handleEvent(viewId);//Switch cases to perform row specific actions.
    }
    return false;
}

now if user touch the row event get fired that works perfect, But one more problem rises here when even user want to scroll down the data via dragging finger onto the screen ontouch event get fired.... and action automatically performed although user think it will scroll down the screen. 现在,如果用户触发了行事件,则可以很好地触发该行事件,但是,即使用户甚至想要通过将手指拖到屏幕上的ontouch事件来触发数据来向下滚动数据, ontouch 。...尽管用户认为它会自动执行操作将scroll down屏幕。

I don't know if this solution is applicable in your case, but maybe you can do like this: Let's assume that user clicked your item (so probably you will get sequence of three events to your OnTouch() method: ACTION_ DOWN, ACTION_ MOVE (not necessarily) and ACTION_UP. Now you can react accordingly. 我不知道此解决方案是否适用于您的情况,但也许您可以这样做:假设用户单击了您的项目(因此您可能会在OnTouch()方法中获得三个事件的序列:ACTION_ DOWN,ACTION_ MOVE(不一定)和ACTION_UP。现在您可以做出相应的反应了。

  1. If it's ACTION_DOWN, you can save x and y coordinates. 如果是ACTION_DOWN,则可以保存x和y坐标。
  2. If it's ACTION_ MOVE, take its x and y and calculate the distance from corresponding ACTION_DOWN. 如果是ACTION_MOVE,则取其x和y并计算与相应ACTION_DOWN的距离。 If it's longer than some assumed value, then make the scroll and set the flag indicating that items were scrolled. 如果它比某个假定的值长,则进行滚动并设置标志以指示项目已滚动。
  3. If it's ACTION_UP check your flag. 如果是ACTION_UP,请检查您的标志。 If items were not scrolled, fire your action and clear the flag. 如果没有滚动项​​目,请执行操作并清除标志。

Probably calculations is not what you should do in ACTION_MOVE event, because it should be fast, but give it a try. 可能不是在ACTION_MOVE事件中应该执行的计算,因为它应该很快,但是请尝试一下。
Regards! 问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM