This commit is contained in:
Daniele Verducci (Slimpenguin) 2022-01-31 09:22:52 +01:00
parent 63b08fe718
commit 60791459c0
3 changed files with 39 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View; import android.view.View;
import androidx.navigation.NavController; import androidx.navigation.NavController;
@ -13,8 +14,10 @@ import it.danieleverducci.ojo.R;
import it.danieleverducci.ojo.databinding.ActivityMainBinding; import it.danieleverducci.ojo.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private ActivityMainBinding binding; private ActivityMainBinding binding;
private NavController navController;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -24,7 +27,7 @@ public class MainActivity extends AppCompatActivity {
setContentView(binding.getRoot()); setContentView(binding.getRoot());
// Show FAB only on first fragment // Show FAB only on first fragment
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main); navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
navController.addOnDestinationChangedListener((controller, destination, arguments) -> { navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
if (destination.getId() == R.id.HomeFragment) if (destination.getId() == R.id.HomeFragment)
binding.fab.show(); binding.fab.show();
@ -35,8 +38,17 @@ public class MainActivity extends AppCompatActivity {
binding.fab.setOnClickListener(new View.OnClickListener() { binding.fab.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
navController.navigate(R.id.action_homeToSettings); navigateToFragment(R.id.action_homeToSettings);
} }
}); });
} }
public void navigateToFragment(int actionId) {
if (navController == null) {
Log.e(TAG, "Not initialized");
return;
}
navController.navigate(actionId);
}
} }

View File

@ -55,6 +55,13 @@ public class SettingsFragment extends Fragment {
touchHelper.attachToRecyclerView(recyclerView); touchHelper.attachToRecyclerView(recyclerView);
adapter.setOnDragListener(touchHelper::startDrag); adapter.setOnDragListener(touchHelper::startDrag);
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
// Onclick listener
adapter.setOnClickListener(new SettingsRecyclerViewAdapter.OnClickListener() {
@Override
public void onItemClick(Camera c) {
((MainActivity)getActivity()).navigateToFragment(R.id.action_homeToSettings);
}
});
} }
return view; return view;
} }

View File

@ -24,6 +24,7 @@ public class SettingsRecyclerViewAdapter extends RecyclerView.Adapter<SettingsRe
private final List<Camera> mValues; private final List<Camera> mValues;
private OnDragListener dragListener; private OnDragListener dragListener;
private OnClickListener clickListener;
public SettingsRecyclerViewAdapter(List<Camera> items) { public SettingsRecyclerViewAdapter(List<Camera> items) {
mValues = items; mValues = items;
@ -38,6 +39,12 @@ public class SettingsRecyclerViewAdapter extends RecyclerView.Adapter<SettingsRe
} }
return false; return false;
}); });
vh.root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clickListener.onItemClick((Camera)view.getTag());
}
});
return vh; return vh;
} }
@ -48,6 +55,8 @@ public class SettingsRecyclerViewAdapter extends RecyclerView.Adapter<SettingsRe
cameraName = holder.name.getContext().getString(R.string.stream_list_default_camera_name).replace("{camNo}", (position+1)+""); cameraName = holder.name.getContext().getString(R.string.stream_list_default_camera_name).replace("{camNo}", (position+1)+"");
holder.name.setText(cameraName); holder.name.setText(cameraName);
holder.url.setText(mValues.get(position).getRtspUrl()); holder.url.setText(mValues.get(position).getRtspUrl());
// Save item
holder.root.setTag(mValues.get(position));
} }
@Override @Override
@ -85,8 +94,12 @@ public class SettingsRecyclerViewAdapter extends RecyclerView.Adapter<SettingsRe
public void setOnDragListener(OnDragListener dragListener) { public void setOnDragListener(OnDragListener dragListener) {
this.dragListener = dragListener; this.dragListener = dragListener;
} }
public void setOnClickListener(OnClickListener clickListener) {
this.clickListener = clickListener;
}
public class ViewHolder extends RecyclerView.ViewHolder { public class ViewHolder extends RecyclerView.ViewHolder {
public View root;
public TextView name; public TextView name;
public TextView url; public TextView url;
public View dragHandle; public View dragHandle;
@ -94,6 +107,7 @@ public class SettingsRecyclerViewAdapter extends RecyclerView.Adapter<SettingsRe
public ViewHolder(FragmentSettingsItemBinding binding) { public ViewHolder(FragmentSettingsItemBinding binding) {
super(binding.getRoot()); super(binding.getRoot());
this.root = binding.getRoot();
this.name = binding.cameraName; this.name = binding.cameraName;
this.url = binding.cameraUrl; this.url = binding.cameraUrl;
this.dragHandle = binding.cameraDragHandle; this.dragHandle = binding.cameraDragHandle;
@ -103,4 +117,8 @@ public class SettingsRecyclerViewAdapter extends RecyclerView.Adapter<SettingsRe
public interface OnDragListener { public interface OnDragListener {
void onItemDrag(ViewHolder vh); void onItemDrag(ViewHolder vh);
} }
public interface OnClickListener {
void onItemClick(Camera c);
}
} }