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.Base64;
import android.util.Log; import android.util.Log;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window; import android.view.Window;
import android.webkit.CookieManager; import android.webkit.CookieManager;
import android.webkit.DownloadListener; import android.webkit.DownloadListener;
@ -55,6 +57,7 @@ public class MainActivity extends Activity {
private ValueCallback<Uri[]> upload_message; private ValueCallback<Uri[]> upload_message;
private final static int FILECHOOSER_RESULT = 1; private final static int FILECHOOSER_RESULT = 1;
private float touch_down_y;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -238,12 +241,9 @@ public class MainActivity extends Activity {
} }
}); });
Button refresh = (Button)findViewById(R.id.refresh); TextView refresh = (TextView)findViewById(R.id.refresh);
refresh.setOnClickListener(new View.OnClickListener() { refresh.setVisibility(View.GONE);
public void onClick(View view) { refresh.setText("REFRESH");
web_view.reload();
}
});
} }
@Override @Override
@ -298,6 +298,39 @@ public class MainActivity extends Activity {
return super.onKeyDown(keyCode, event); 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) { private int read_port(String path) {
try (BufferedReader reader = new BufferedReader(new FileReader(path))) { try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
return Integer.parseInt(reader.readLine()); return Integer.parseInt(reader.readLine());

View File

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