Implement my own hokey pull to refresh on Android. Nobody's got time for all those dependencies.

This commit is contained in:
Cory McWilliams 2024-04-17 19:55:14 -04:00
parent d9ce569eb9
commit 2ecae7da93
2 changed files with 46 additions and 13 deletions

View File

@ -15,7 +15,9 @@ import android.os.strictmode.Violation;
import android.util.Base64;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
@ -55,6 +57,7 @@ public class MainActivity extends Activity {
private ValueCallback<Uri[]> upload_message;
private final static int FILECHOOSER_RESULT = 1;
private float touch_down_y;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -238,12 +241,9 @@ public class MainActivity extends Activity {
}
});
Button refresh = (Button)findViewById(R.id.refresh);
refresh.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
web_view.reload();
}
});
TextView refresh = (TextView)findViewById(R.id.refresh);
refresh.setVisibility(View.GONE);
refresh.setText("REFRESH");
}
@Override
@ -298,6 +298,39 @@ public class MainActivity extends Activity {
return super.onKeyDown(keyCode, event);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final int k_drag_distance = 160;
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
touch_down_y = event.getY();
break;
case MotionEvent.ACTION_MOVE:
{
float delta = event.getY() - touch_down_y;
TextView refresh = (TextView)findViewById(R.id.refresh);
LayoutParams layout = refresh.getLayoutParams();
layout.height =
Math.min(Math.max((int)delta, 0), k_drag_distance) +
(delta > k_drag_distance ? (int)Math.sqrt(delta - k_drag_distance) : 0);
refresh.setLayoutParams(layout);
refresh.setVisibility(layout.height > 0 ? View.VISIBLE : View.GONE);
}
break;
case MotionEvent.ACTION_UP:
{
float delta = event.getY() - touch_down_y;
if (delta > getWindow().getDecorView().getHeight() / 4) {
web_view.reload();
}
TextView refresh = (TextView)findViewById(R.id.refresh);
refresh.setVisibility(View.GONE);
}
break;
}
return super.dispatchTouchEvent(event);
}
private int read_port(String path) {
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
return Integer.parseInt(reader.readLine());

View File

@ -13,12 +13,12 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"/>
<Button
<TextView
android:id="@+id/refresh"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="REFRESH"/>
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_alignParentTop="true"
android:gravity="center_horizontal|center_vertical"
android:textColor="#fff"
android:background="#44f"/>
</RelativeLayout>