mirror of
https://github.com/emanuele-f/PCAPdroid.git
synced 2026-06-19 21:05:25 +08:00
Implement connections autoscroll and scroll to bottom
This commit is contained in:
parent
86ae093e31
commit
d08c3d3f64
@ -23,6 +23,7 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@ -32,12 +33,16 @@ import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
public class ConnectionsFragment extends Fragment implements AppStateListener, ConnectionsListener {
|
||||
private static final String TAG = "ConnectionsFragment";
|
||||
private MainActivity mActivity;
|
||||
private Handler mHandler;
|
||||
private ConnectionsAdapter mAdapter;
|
||||
private View mFabDown;
|
||||
private EmptyRecyclerView mRecyclerView;
|
||||
private boolean autoScroll;
|
||||
|
||||
@Override
|
||||
public void onAttach(@NonNull Context context) {
|
||||
@ -66,17 +71,18 @@ public class ConnectionsFragment extends Fragment implements AppStateListener, C
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
EmptyRecyclerView connList = view.findViewById(R.id.connections_view);
|
||||
connList.setLayoutManager(new LinearLayoutManager(mActivity));
|
||||
mRecyclerView = view.findViewById(R.id.connections_view);
|
||||
LinearLayoutManager layoutMan = new LinearLayoutManager(mActivity);
|
||||
mRecyclerView.setLayoutManager(layoutMan);
|
||||
|
||||
TextView emptyText = view.findViewById(R.id.no_connections);
|
||||
connList.setEmptyView(emptyText);
|
||||
mRecyclerView.setEmptyView(emptyText);
|
||||
|
||||
mAdapter = new ConnectionsAdapter(mActivity);
|
||||
connList.setAdapter(mAdapter);
|
||||
mRecyclerView.setAdapter(mAdapter);
|
||||
|
||||
mAdapter.setClickListener(v -> {
|
||||
int pos = connList.getChildLayoutPosition(v);
|
||||
int pos = mRecyclerView.getChildLayoutPosition(v);
|
||||
ConnDescriptor item = mAdapter.getItem(pos);
|
||||
|
||||
if(item != null) {
|
||||
@ -100,6 +106,34 @@ public class ConnectionsFragment extends Fragment implements AppStateListener, C
|
||||
}
|
||||
});
|
||||
|
||||
mFabDown = view.findViewById(R.id.fabDown);
|
||||
autoScroll = true;
|
||||
showFabDown(false);
|
||||
|
||||
mFabDown.setOnClickListener(v -> scrollToBottom());
|
||||
|
||||
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
|
||||
@Override
|
||||
//public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
|
||||
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int state) {
|
||||
int first_visibile_pos = layoutMan.findFirstCompletelyVisibleItemPosition();
|
||||
int last_visible_pos = layoutMan.findLastCompletelyVisibleItemPosition();
|
||||
int last_pos = mAdapter.getItemCount() - 1;
|
||||
boolean reached_bottom = (last_visible_pos >= last_pos);
|
||||
boolean is_scrolling = (first_visibile_pos != 0) || (!reached_bottom);
|
||||
|
||||
if(is_scrolling) {
|
||||
if(reached_bottom) {
|
||||
autoScroll = true;
|
||||
showFabDown(false);
|
||||
} else {
|
||||
autoScroll = false;
|
||||
showFabDown(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
mHandler = new Handler();
|
||||
ConnectionsRegister reg = CaptureService.getConnsRegister();
|
||||
if(reg != null)
|
||||
@ -108,6 +142,20 @@ public class ConnectionsFragment extends Fragment implements AppStateListener, C
|
||||
mActivity.addAppStateListener(this);
|
||||
}
|
||||
|
||||
private void showFabDown(boolean visible) {
|
||||
if(visible)
|
||||
mFabDown.setVisibility(View.VISIBLE);
|
||||
else
|
||||
mFabDown.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
|
||||
private void scrollToBottom() {
|
||||
int last_pos = mAdapter.getItemCount() - 1;
|
||||
mRecyclerView.scrollToPosition(last_pos);
|
||||
|
||||
showFabDown(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
@ -122,6 +170,10 @@ public class ConnectionsFragment extends Fragment implements AppStateListener, C
|
||||
|
||||
if(reg != null)
|
||||
reg.setListener(this);
|
||||
|
||||
autoScroll = true;
|
||||
showFabDown(false);
|
||||
mAdapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@ -133,6 +185,12 @@ public class ConnectionsFragment extends Fragment implements AppStateListener, C
|
||||
|
||||
@Override
|
||||
public void connectionsChanges() {
|
||||
mHandler.post(() -> mAdapter.notifyDataSetChanged());
|
||||
mHandler.post(() -> {
|
||||
mAdapter.notifyDataSetChanged();
|
||||
|
||||
if(autoScroll)
|
||||
scrollToBottom();
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,7 +33,6 @@ import android.view.LayoutInflater;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
5
app/src/main/res/drawable/ic_arrow_drop_down.xml
Normal file
5
app/src/main/res/drawable/ic_arrow_drop_down.xml
Normal file
@ -0,0 +1,5 @@
|
||||
<vector android:height="24dp" android:tint="#FFFFFF"
|
||||
android:viewportHeight="24" android:viewportWidth="24"
|
||||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="@android:color/white" android:pathData="M7,10l5,5 5,-5z"/>
|
||||
</vector>
|
||||
@ -1,14 +1,25 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<com.emanuelef.remote_capture.EmptyRecyclerView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/connections_view"
|
||||
></com.emanuelef.remote_capture.EmptyRecyclerView>
|
||||
android:scrollbars="vertical"
|
||||
android:scrollbarStyle="outsideOverlay"
|
||||
android:id="@+id/connections_view" />
|
||||
|
||||
<com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
android:id="@+id/fabDown"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_margin="16dp"
|
||||
app:maxImageSize="48dp"
|
||||
android:src="@drawable/ic_arrow_drop_down" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/no_connections"
|
||||
@ -20,4 +31,4 @@
|
||||
android:textSize="15dp"
|
||||
android:text="@string/no_connections">
|
||||
</TextView>
|
||||
</LinearLayout>
|
||||
</RelativeLayout>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user