Android ListView OnItemClick事件有时会触发,但并非总是如此

时间:2011-08-02 07:04:43

标签: android event-handling intermittent

我对Android ListView组件有一个非常奇怪的问题。

情况:我基本上使用setOnItemClickListener方法,然后重写“onItemClick”方法,以便在单击列表视图中的项目时实现自定义操作。

问题:onItemClick事件在项目点击时应该触发,但是已经点击了相当数量的项目(20-30-40),突然一个,只有一个项目变得不可点击,这意味着什么都没有点击它时会发生。与此同时,所有其他项目仍然可以使用。

我重写了ListView组件以捕获“dispatchTouchEvent”(参见下面的代码),这表明当一个项突然变得不可用时,dispatchTouchEvent确实仍然被触发,具有合理的X和Y坐标,但不知何故“onItemClick”事件易于触发。

public class ExtendedListView extends ListView 
{
public ExtendedListView(Context context) {
    super(context);     
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev)
{
    Log.v("dispatchTouchEvent", "click|X:" + ev.getX() + "|Y:" + ev.getY());
    return super.dispatchTouchEvent(ev);        
}

}

启动ListView的方法:

@SuppressWarnings("unchecked")
private void renderHomeScreenItemsList() {
    HomeScreenItemAdapter m_adapter;
    ArrayList<HomeScreenItem> lHomeScreenItems = null;

    LayoutParams lpHomeScreenItems = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);        
    lpHomeScreenItems.leftMargin = Util.dipsToPx(0, this);
    lpHomeScreenItems.topMargin = Util.dipsToPx(155, this);     
    lpHomeScreenItems.gravity = 3; //3 = LEFT. Some gravity is necessary.       
    lvHomeScreenItems = new ExtendedListView(this);
    lvHomeScreenItems.setLayoutParams(lpHomeScreenItems);

    ColorDrawable sage = new ColorDrawable(this.getResources().getColor(R.color.HomeScreenDividerColor));        
    lvHomeScreenItems.setDivider(sage);
    lvHomeScreenItems.setDividerHeight(1);

    lvHomeScreenItems.setCacheColorHint(Color.WHITE);
    lvHomeScreenItems.setBackgroundColor(Color.WHITE);      

    lHomeScreenItems = new ArrayList<HomeScreenItem>();        

    Iterator<EnabledSection> ite = Global.EnabledSectionsVector.iterator();     

    while (ite.hasNext()) {
        EnabledSection es = (EnabledSection) ite.next();

        HomeScreenItem h1 = new HomeScreenItem();
        h1.setItemText(es.FunctionTitle);
        h1.setFunctionName(es.FunctionName);

        int imageId = getResources().getIdentifier("homescreen_" + es.FunctionName.toLowerCase(), "drawable", "XXXXXXXXX");

        h1.setItemImageResourceId(imageId);

        lHomeScreenItems.add(h1);
    }

    HomeScreenItem hCopyright = new HomeScreenItem();
    hCopyright.setItemText("copyright");
    hCopyright.setItemImageResourceId(-1);
    lHomeScreenItems.add(hCopyright);       

    m_adapter = new HomeScreenItemAdapter(this, R.layout.row, lHomeScreenItems);
    lvHomeScreenItems.setAdapter(m_adapter);        

    lvHomeScreenItems.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.v("onItemClick", "Click Detected");
            HomeScreenItem hmi = (HomeScreenItem)parent.getItemAtPosition(position);

            Boolean blnAcceptClick = true;
            if (WrapperWebView == null)
                blnAcceptClick = true;
            else
                if (WrapperWebView.getProgress() != 100)
                    blnAcceptClick = false;

            if (hmi.getItemImageResourceId() != -1 && blnAcceptClick)
            {   
                Global.IsLoadingWrapper = true;
                vSelectedItem = view;

                ImageView ivIcon = (ImageView)view.findViewById(R.id.rightarrow);

                LinearLayout ll = (LinearLayout)view.findViewById(R.id.RowContainer);
                TextView ll2 = (TextView)view.findViewById(R.id.toptext);

                ll.setBackgroundResource(R.color.BlueActive);
                ll2.setBackgroundResource(R.color.BlueActive);
                ll2.setTextColor(Color.WHITE);                  

                int[] intCoordinates = new int[2];                  
                view.getLocationInWindow(intCoordinates);
                lpLoadingSpinner.topMargin = intCoordinates[1] - Util.dipsToPx(10, HomeScreen.this); 
                ivLoadingSpinner.setVisibility(View.VISIBLE);
                ivLoadingSpinner.bringToFront();

                LoadingSpinnerAnimation.start();

                ivIcon.setVisibility(View.GONE);
                ivClickedItemIcon = ivIcon;                 

                InitializeWebView(hmi.getFunctionName());        
            }               
        }
    });
}

任何帮助都会受到很大的限制。确认该错误发生在多个设备和多个Android版本上。

更新:问题已修复 我设法找到问题并修复它。基本上,项目列表包含所有“正常”项目(在我的上下文中),除了底部项目,这是一个“版权”项目,其中包含必须可点击的链接(蓝色)。为了使这个点击效果有效,我使用了:

tt.setMovementMethod(LinkMovementMethod.getInstance());

如果正在呈现的项目是版权项目。

但是,如果正在渲染的项目是“正常”项目,我没有反击该效果,因此只需在渲染正常项目时添加以下行,问题就会消失:

tt.setMovementMethod(null);

完整的示例代码(删除了不相关的代码)

public class HomeScreenItemAdapter extends ArrayAdapter<HomeScreenItem> {

private ArrayList<HomeScreenItem> items;

public HomeScreenItemAdapter(Context context, int textViewResourceId, ArrayList<HomeScreenItem> items) {
    super(context, textViewResourceId, items);
    this.items = items;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{   
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.row, null);
    }        

    HomeScreenItem o = items.get(position);
    if (o != null) {            
        TextView tt = (TextView) v.findViewById(R.id.toptext);

        if (iv != null && tt != null)
        {
            if (position != (items.size() - 1))
            {       
            tt.setMovementMethod(null);                 
            }
            else
            {
                //last element, so setup element to show copyright notice instead
                tt.setMovementMethod(LinkMovementMethod.getInstance()); //this makes the link clickable                 
            }
        }
    }
    return v;
}

}

0 个答案:

没有答案