-1

我已经设置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 我已经添加了代码。请检查它....

4

2 回答 2

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() return 即可false

于 2012-06-17T17:32:33.223 回答
0

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

于 2012-06-18T05:30:25.963 回答