试图实现滑动移动

时间:2014-05-16 14:16:03

标签: android android-fragments android-ui

public class CoursesActivity扩展了SherlockFragmentActivity {

ArrayList<CourseData> mCoursesList = new ArrayList<CourseData>();

public CoursesListAdapter coursesListAdapter;
public ListView mList;
public AlertDialog.Builder dlgAlert;
public Dialog loginDialog;
public Dialog loginDialogOverflow;

LinearLayout progressBar;
static SQLiteWebcourse dbHelper;
private GestureDetectorCompat mDetector;

public final static String EXTRA_MESSAGE = "com.technion.coolie.webcourse.MESSAGE";

public CoursesActivity() {
    dbHelper = new SQLiteWebcourse(this, "WebcoureDB", null, 1);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Create list of courses
    mCoursesList = new ArrayList<CourseData>();
    setContentView(R.layout.web_activity_courses);
    mList = (ListView) findViewById(R.id.courses_list);
    dlgAlert = new AlertDialog.Builder(getApplicationContext());
    if (!CoolieAccount.WEBCOURSE.isAlreadyLoggedIn()) {

        loginDialog = CoolieAccount.WEBCOURSE.openSigninDialog(this);

        OnDismissListener dismissListener = new OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface dialog) {

                // Check if connection success
                if (CoolieAccount.WEBCOURSE.isAlreadyLoggedIn()) {
                    // Connection SUCCESS

                    progressBar = (LinearLayout) findViewById(R.id.progressBarLayout_courses);
                    progressBar.setVisibility(View.VISIBLE);

                    asyncParse<CourseData> a = new asyncParse<CourseData>() {

                        @Override
                        protected List<CourseData> doInBackground(
                                String... params) {
                            // TODO Auto-generated method stub
                            try {
                                courseList crL = new courseList(
                                        getApplicationContext());
                                crL.getCourses(CoolieAccount.WEBCOURSE
                                        .getUsername(),
                                        CoolieAccount.WEBCOURSE
                                                .getPassword());
                                mCoursesList = dbHelper
                                        .getAllCourses(CoolieAccount.WEBCOURSE
                                                .getUsername());
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            return super.doInBackground(params);
                        }

                        @Override
                        protected void onPostExecute(List<CourseData> result) {
                            // TODO Auto-generated method stub
                            CoursesListAdapter coursesListAdapter = new CoursesListAdapter(
                                    getApplicationContext(), mCoursesList);
                            mList.setAdapter(coursesListAdapter);
                            mList.setBackgroundColor(0xFFF0F0F0);
                            progressBar.setVisibility(View.INVISIBLE);
                            super.onPostExecute(result);
                        }

                    };
                    a.execute("");

                } else {
                    // Connection FAILED
                    Log.v("dbAss", "Second Time");

                }
            }
        };

        loginDialog.setOnDismissListener(dismissListener);

    } else {
        mCoursesList = dbHelper.getAllCourses(CoolieAccount.WEBCOURSE
                .getUsername());
        CoursesListAdapter coursesListAdapter = new CoursesListAdapter(
                getApplicationContext(), mCoursesList);
        mList.setAdapter(coursesListAdapter);
        mList.setBackgroundColor(0xFFF0F0F0);
    }
    Log.v("Gestures", "OnTouchEvent@!$#%@#%^&^$%");

    // OnItemClickListener itemClicked = new OnItemClickListener() {
    //
    // @Override
    // public void onItemClick(AdapterView<?> list, View view,
    // int position, long id) {
    // // TODO Auto-generated method stub
    // loadCourseInformationActivity(((CourseData) mList
    // .getItemAtPosition(position)).CourseDescription);
    // }
    // };
    // mList.setOnItemClickListener(itemClicked);
    mDetector = new GestureDetectorCompat(this, new MyGestureListener());
}



@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.v("Gestures", "OnTouchEvent@!$#%@#%^&^$%");

    this.mDetector.onTouchEvent(event);
    // Be sure to call the superclass implementation
    return super.onTouchEvent(event);
}

class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
    private static final String DEBUG_TAG = "Gestures";

    @Override
    public boolean onFling(MotionEvent event1, MotionEvent event2,
            float velocityX, float velocityY) {
        Log.v(DEBUG_TAG,
                "onFling: " + event1.toString() + event2.toString());
        Toast.makeText(getApplicationContext(), "SWIPED", Toast.LENGTH_LONG)
                .show();
        if (event2.getX() - event1.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
            Toast.makeText(getApplicationContext(), "SWIPED",
                    Toast.LENGTH_LONG).show();
        // if (showDeleteButton(e1))
        // return true;
        return super.onFling(event1, event2, velocityX, velocityY);
    }
}

}

嘿fellas,我正在尝试实施滑动检测我经历了android课程并完成了那里写的内容:http://developer.android.com/training/gestures/detector.html 出于某种原因,当我触摸屏幕时,它不会转到onTouchEvent。什么似乎是问题?

1 个答案:

答案 0 :(得分:0)

您尚未将触摸倾听者设置为ListView

listView.setOnTouchListener(this);