开发者

Android ListView OnItemClick event fires sometimes, but not always

I have a very wierd problem with the Android ListView component.

Situation: I'm basically utilizing the setOnItemClickListener method and subsequently overriding the "onItemClick" method to implement a custom action when clicking on an item in the listview.

Problem: The onItemClick event fires as it should on an item tap, however having tapped a decent amount of items (20-30-40), suddenly one, and only one, of the items become un-tapable, meaning nothing happens when you tap it. Meanwhile, all the other items are still tapable.

I have overridden the ListView component to capture the "dispatchTouchEvent" (see code below), and that reveals that when an item suddenly becomes un-tapable, the dispatchTouchEvent is indeed still fired, with reasonable X and Y coordinates, but somehow the "onItemClick" event failes to fire.

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);        
}

}

Method that initiates the ListView:

@SuppressWarnings("un开发者_JAVA技巧checked")
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());        
            }               
        }
    });
}

Any help would be greatly appeciated. The bug is confirmed to happen on multiple devices and on multiple android versions.

UPDATE: Problem fixed I've managed to find the problem and fix it. Basically, the list of items contained all "normal" items (in my context), except for the bottom one, which was a "copyright" item with a link (in blue) that had to be clickable. To get this click effect working, I used:

tt.setMovementMethod(LinkMovementMethod.getInstance());

if the item being rendered was the copyright item.

However, I didn't counter that effect if the item being rendered was a "normal" item, so by only adding the line below when a normal item is rendered, the problem went away:

tt.setMovementMethod(null);

Full example code (with non-relevant code removed)

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜