onItemClicklistener无法在自定义列表视图中工作

时间:2012-06-17 17:27:48

标签: android onitemclicklistener

我在自定义列表视图中设置了onItemClicklistener,但onItemClicklistener无效 我还在自定义列表视图的控件上尝试了集focusable="false"focusableInTouchMode="false"。我也看到了相关的问题,但没有帮助。

请帮忙

 public class SwipingActivity extends Activity {
 public OnTouchListener gestureListener;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView lv = (ListView)findViewById(R.id.mylistview);

        gestureListener = new View.OnTouchListener() {
            private int padding = 0;
            private int initialx = 0;
            private int currentx = 0;
            private  ViewHolder viewHolder;
            public boolean onTouch(View v, MotionEvent event) {
                if ( event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    padding = 0;
                    initialx = (int) event.getX();
                    currentx = (int) event.getX();
                    viewHolder = ((ViewHolder) v.getTag());
                }
                if ( event.getAction() == MotionEvent.ACTION_MOVE)
                {
                    currentx = (int) event.getX();
                    padding = currentx - initialx;
                }

                if ( event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL)
                {
                    padding = 0;
                    initialx = 0;
                    currentx = 0;
                }

                if(viewHolder != null)
                {
                    if(padding == 0)
                    {
                        v.setBackgroundColor(0xFF000000 );
                        Toast.makeText(getApplicationContext(), viewHolder.text.getText().toString(), 5);
                    }
                    if(padding > 75)
                    {
                        viewHolder.setRunning(true,viewHolder);
                    }
                    if(padding < -75)
                    {
                        viewHolder.setRunning(false,viewHolder);
                    }
                    v.setBackgroundColor(viewHolder.getColor());  
                    v.setPadding(padding, 0,0, 0);
                }
                return true;
            }
        };

        ModelArrayAdapter adapter = new ModelArrayAdapter(this, getData(),gestureListener);
        lv.setAdapter(adapter);
        lv.setFocusable(true);
        lv.setOnTouchListener(gestureListener);
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                TextView txt = (TextView) arg1.findViewById(R.id.TextView01);

                Toast.makeText(getApplicationContext(), txt.getText().toString(), 5);

            }
        });
    }

@sam我添加了代码。请检查....

2 个答案:

答案 0 :(得分:0)

只是一个猜测,但你是否调用了ListView.setOnItemClickListener()?我相信您希望将focusablefocusableIntouchMode设置为true

<强>加成

你必须调用show()来显示Toast,改变它:

Toast.makeText(getApplicationContext(), txt.getText().toString(), 5);

到此:

Toast.makeText(getApplicationContext(), txt.getText().toString(), 5).show();

这里也是:

Toast.makeText(getApplicationContext(), viewHolder.text.getText().toString(), 5).show();

另一个补充

你的OnTouchListener在它到达OnItemClickListener之前就消耗了这个事件,只需让onTouch()返回false

答案 1 :(得分:0)

尝试在自定义适配器的getView方法中实现ItemClickListener。 现在运行应用程序。它将正常工作