android - ListView中的OnFling,获取滑动的Item信息

标签 android listview swipe onfling

我希望在我的应用程序中具有与 native 联系人应用程序相同的行为。具体来说,我想为通话实现向右滑动,为短信实现向左滑动。 我有一个 ListView,我设置了 arrayAdapter,我已经为 onFlingMethod 实现了手势检测器。 我正确地拦截了滑动侧,我可以启动调用应用程序。 我需要将商品编号(和其他信息)放入 Intent,因此我需要获取刷过的商品。这是我的代码。

public class Contacts extends Activity implements OnGestureListener {

    private static final String TAG = "[Contacts]";
    ArrayList < ContactInfo > mData;

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    /** Called when the activity is first created. */
    private GestureDetector detector = new GestureDetector(this);
    Button btnContacts;
    Button btnProfile;
    Button btnSettings;
    ImageButton btnStatus;
    HandleServer cubeServer;
    Button slideHandleButton;
    SlidingDrawer slidingDrawer;
    String filter = "n";
    ArrayAdapter < ContactInfo > adapter;
    ListView listView;


    public boolean onCreateOptionsMenu(Menu menu) {

        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        return true;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact);

        listView = (ListView) findViewById(R.id.arrayList);
        listView.setCacheColorHint(R.color.white);

        mData = getContact();

        adapter = new ArrayAdapter < ContactInfo > (this.getApplicationContext(), R.layout.row, R.id.nome, mData) {
            public View getView(final int position, View convertView, ViewGroup parent) {
                ViewHolder viewHolder = null;
                if (convertView == null) {

                    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(R.layout.row, null);
                    viewHolder = new ViewHolder();
                    final ContactInfo item = getItem(position);

                    viewHolder.name.setText(item.getName());


                    convertView.setClickable(true);

                    OnClickListener myClickListener = new OnClickListener() {

                        public void onClick(View v) {
                            Intent intent = new Intent(v.getContext(), ContactTabs.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            //Log.i(TAG,"id value:"+item.getId());
                            intent.putExtra("id", item.getId());
                            intent.putExtra("name", item.getName());
                            //aggiungi quello che serve per gli extra ed i task
                            intent.putExtra("calendar", item.getCalendarDraw());
                            intent.putExtra("gmail", item.getGmailDraw());
                            intent.putExtra("operator", item.getOperatorDraw());
                            intent.putExtra("imgStatus", item.getImgStatusDraw());
                            startActivity(intent);
                        }
                    };

                    convertView.setOnClickListener(myClickListener);

                    convertView.setOnTouchListener(new OnTouchListener() {

                        public boolean onTouch(View view, MotionEvent e) {
                            detector.onTouchEvent(e);
                            return false;
                        }
                    });

                    return convertView;
                }
            };

            listView.setAdapter(adapter);
        }

        public boolean onDown(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {

                    return false;
                }
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    //CallNativeApp cna = new CallNativeApp(getApplicationContext());
                    //cna.sendSms("11111", "");
                    Toast.makeText(getApplicationContext(), "Left Swipe", Toast.LENGTH_SHORT).show();
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    //CallNativeApp cna = new CallNativeApp(getApplicationContext());
                    //cna.call("1111");

                    Toast.makeText(getApplicationContext(), "Right Swipe", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                // nothing
            }

            return true;
        }
        public void onLongPress(MotionEvent e) {
            // TODO Auto-generated method stub

        }
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
            // TODO Auto-generated method stub
            return true;
        }
        public void onShowPress(MotionEvent e) {
            // TODO Auto-generated method stub

        }
        public boolean onSingleTapUp(MotionEvent e) {
            // TODO Auto-generated method stub
            return true;
        }

    }
}

最佳答案

我用 ListView.pointToPosition() 方法解决了这个问题。

这是我的代码 fragment :

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
    float velocityX, float velocityY) {

    if (e2.getX() - e1.getX() > MOVE) {

        int id = list.pointToPosition((int) e1.getX(), (int) e1.getY());
        Reminder temp = (Reminder) adapter.getItem((id));
        return true;
    }

    return false;
}

首先,您从 list.pointToPosition((int) e1.getX(), (int) e1.getY()); 获取 rowID,然后根据需要对其进行处理。在我的例子中,Reminder 是我的列表自定义数组适配器中的对象类型。

关于android - ListView中的OnFling,获取滑动的Item信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6151290/

相关文章:

android:指定的 child 已经有一个 parent 。你必须调用 removeView()

安卓工作室 : Can you display a view just for preview?

java - 结果未显示在 ListView Android 中

android - 如何在 Android 的 View 寻呼机页面中使用滑动手势?

android - Tab + 滑动 View - 无法从 fragment 中的文本文件加载数据

swift - 仅当 View Controller 通过滑动或后退按钮按下而不是通过切换选项卡从堆栈中弹出时才发送数据

Android 版本的 C# 的 Console.WriteLine?

android - 如何避免 View.getDrawingCache() 中的空指针?

java - 在 ListView 中选择项目时出现问题 - JavaFX

android - 如何将 ListView 项目加载并保存到 SQLiteDatabase?