-
-
Save Gohan/42b436c5c7ca173ad6237c6ba589846c to your computer and use it in GitHub Desktop.
Simulate Android Event
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="com.baozs.demos.sqlbritedemo2017.listview.ListViewActivity"> | |
<com.baozs.demos.sqlbritedemo2017.listview.MyAutoPullDownListView | |
android:id="@+id/listview" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
/> | |
</FrameLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.baozs.demos.sqlbritedemo2017.listview; | |
import android.content.Context; | |
import android.support.annotation.LayoutRes; | |
import android.support.annotation.NonNull; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ArrayAdapter; | |
import com.baozs.demos.sqlbritedemo2017.R; | |
public class ListViewActivity extends AppCompatActivity { | |
private MyAutoPullDownListView mListView; | |
private MyAdapter mAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_list_view); | |
mListView = (MyAutoPullDownListView) findViewById(R.id.listview); | |
mListView.setOverscrollHeader(ContextCompat.getDrawable(this, R.drawable.rockman)); | |
mAdapter = new MyAdapter(this, android.R.layout.simple_list_item_1); | |
mListView.setAdapter(mAdapter); | |
mListView.setOverScrollMode(View.OVER_SCROLL_ALWAYS); | |
for (int i = 0; i < 100; i++) { | |
mAdapter.add("Hello"); | |
mAdapter.add("World"); | |
} | |
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
mListView.overScrollDown(); | |
} | |
}); | |
} | |
public static class MyAdapter extends ArrayAdapter<String> { | |
public MyAdapter(@NonNull Context context, @LayoutRes int resource) { | |
super(context, resource); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.baozs.demos.sqlbritedemo2017.listview; | |
import android.content.Context; | |
import android.support.v4.view.MotionEventCompat; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.view.VelocityTracker; | |
import android.widget.ListView; | |
import android.widget.OverScroller; | |
import static android.R.attr.duration; | |
import static android.R.interpolator.linear; | |
import static android.support.v4.widget.ViewDragHelper.INVALID_POINTER; | |
/** | |
* Created by vashzhong on 2017/5/15. | |
*/ | |
public class MyAutoPullDownListView extends ListView { | |
public MyAutoPullDownListView(Context context) { | |
super(context); | |
init(context, null); | |
} | |
public MyAutoPullDownListView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(context, attrs); | |
} | |
private void init(Context context, AttributeSet attrs) { | |
} | |
public void overScrollDown() { | |
post(new Runnable() { | |
@Override | |
public void run() { | |
final MotionEvent event = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_DOWN, getWidth() / 2, getHeight() / 2, 0); | |
dispatchTouchEvent(event); | |
event.recycle(); | |
} | |
}); | |
postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
final MotionEvent event = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_MOVE, getWidth() / 2, getHeight() / 2, 0); | |
dispatchTouchEvent(event); | |
event.recycle(); | |
} | |
}, 50); | |
postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
final MotionEvent event = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_MOVE, getWidth() / 2, getHeight() / 2 + 400, 0); | |
dispatchTouchEvent(event); | |
event.recycle(); | |
} | |
}, 100); | |
postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
final MotionEvent event = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis(), MotionEvent.ACTION_UP, getWidth() / 2, getHeight() / 2 + 400, 0); | |
dispatchTouchEvent(event); | |
event.recycle(); | |
} | |
}, 3000); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment