Use RecyclerView for custom layout, drag and swipe

This commit is contained in:
MrLetsplay 2021-06-28 21:25:21 +02:00
parent 014d61ce45
commit 514e987cf2
3 changed files with 154 additions and 69 deletions

View File

@ -18,9 +18,14 @@ import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider; import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.floatingactionbutton.FloatingActionButton;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Base64; import java.util.Base64;
import java.util.Objects; import java.util.Objects;
@ -37,11 +42,10 @@ public class FlurFragment extends Fragment implements AddButtonDialogSingle.AddB
private FlurViewModel flurViewModel; private FlurViewModel flurViewModel;
String[] commands = {"Hello1","Hello2","Hello3","Hello4","Hello5"}; String[] commands = {"Hello1","Hello2","Hello3","Hello4","Hello5"};
int[] type = {0,1,0,0,1}; int[] type = {0,1,0,0,1};
ListView listView; RecyclerView listView;
ListAdapter listAdapter; ListAdapter listAdapter;
ArrayList<ListItem> items; ArrayList<ListItem> items;
@ -73,9 +77,41 @@ public class FlurFragment extends Fragment implements AddButtonDialogSingle.AddB
listAdapter= new ListAdapter(getContext(), R.id.text, items); listAdapter= new ListAdapter(getContext(), items);
listView.setLayoutManager(new LinearLayoutManager(getContext()));
listView.setAdapter(listAdapter); listView.setAdapter(listAdapter);
ItemTouchHelper h = new ItemTouchHelper(new ItemTouchHelper.Callback() {
@Override
public int getMovementFlags(@NonNull @NotNull RecyclerView recyclerView, @NonNull @NotNull RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
}
@Override
public boolean onMove(@NonNull @NotNull RecyclerView recyclerView, @NonNull @NotNull RecyclerView.ViewHolder viewHolder, @NonNull @NotNull RecyclerView.ViewHolder target) {
//listAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
listAdapter.swap(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
@Override
public void onSwiped(@NonNull @NotNull RecyclerView.ViewHolder viewHolder, int direction) {
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
@Override
public boolean isItemViewSwipeEnabled() {
return false;
}
});
h.attachToRecyclerView(listView);
FloatingActionButton addfab = root.findViewById(R.id.addbutton_fab); FloatingActionButton addfab = root.findViewById(R.id.addbutton_fab);
addfab.setOnClickListener(new View.OnClickListener() { addfab.setOnClickListener(new View.OnClickListener() {
@Override @Override
@ -105,7 +141,6 @@ public class FlurFragment extends Fragment implements AddButtonDialogSingle.AddB
} else if (i == 3) { } else if (i == 3) {
listAdapter.add(new ListItemSpace(3)); listAdapter.add(new ListItemSpace(3));
listAdapter.notifyDataSetChanged(); listAdapter.notifyDataSetChanged();
} }
dialogInterface.dismiss(); dialogInterface.dismiss();

View File

@ -8,92 +8,140 @@ import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import de.jg_cody.Teraplex.R; import de.jg_cody.Teraplex.R;
public class ListAdapter extends ArrayAdapter { public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListItemViewHolder> {
public static int SINGLEBUTTON = 0; public static final int
public static int DOUBLEBUTTON = 1; SINGLEBUTTON = 0,
public static int HEADLINE = 2; DOUBLEBUTTON = 1,
public static int SPACE = 3; HEADLINE = 2,
SPACE = 3;
private Context context;
private LayoutInflater inflater;
private ArrayList<ListItem> objects; private ArrayList<ListItem> objects;
public ListAdapter(Context context, ArrayList<ListItem> objects) {
//super(context, resource, objects);
this.context = context;
this.inflater = LayoutInflater.from(context);
this.objects = objects;
}
@NonNull
@NotNull
@Override
public ListItemViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
return new ListItemViewHolder(createView(viewType, parent));
}
@Override @Override
public int getViewTypeCount() public void onBindViewHolder(@NonNull @NotNull ListItemViewHolder holder, int position) {
{ bindView(holder.getItemView(), objects.get(position));
return 4;
} }
public int getItemViewType(int position) { public int getItemViewType(int position) {
return objects.get(position).getType(); return objects.get(position).getType();
} }
public ListAdapter(Context context, int resource, ArrayList<ListItem> objects)
{
super(context, resource, objects);
this.objects = objects;
}
@SuppressLint("InflateParams")
@NotNull
@Override @Override
public View getView(int position, View convertView, @NotNull ViewGroup parent) { public int getItemCount() {
return objects.size();
IViewHolder viewHolder = null;
ListItem listViewItem = objects.get(position);
int listViewItemType = getItemViewType(position);
String[] s = listViewItem.getStrings();
if (convertView == null) {
if (listViewItemType == SINGLEBUTTON) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.singlebutton, null);
Button bRun = convertView.findViewById(R.id.singleButton_button);
Button text = convertView.findViewById(R.id.singleButton_text);
bRun.setText(s[2]);
text.setText(s[0]);
//text.setText(listViewItemType.get)
//viewHolder = new ViewHolderSingleButton(bRun , listViewItem.getText());
//convertView.setTag(viewHolder);
} else if (listViewItemType == DOUBLEBUTTON) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.doublebutton, null);
Button bLeft = (Button) convertView.findViewById(R.id.doubleButton_bLeft);
Button bRight = (Button) convertView.findViewById(R.id.doubleButton_bRight);
Button text = (Button) convertView.findViewById(R.id.doubleButton_text);
bLeft.setText(s[3]);
bRight.setText(s[4]);
text.setText(s[0]);
//viewHolder = new ViewHolderDoubleButton(bLeft,bRight,listViewItem.getText());
//convertView.setTag(viewHolder);
} else if (listViewItemType == SPACE) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.space, null);
}else if (listViewItemType == HEADLINE) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.headline, null);
Button text = (Button) convertView.findViewById(R.id.headline_text);
text.setText(s[0]);
} else {
viewHolder = (IViewHolder) convertView.getTag();
} }
return convertView; public void add(ListItem item) {
objects.add(item);
} }
return convertView;
}} public void swap(int idx1, int idx2) {
Collections.swap(objects, idx1, idx2);
notifyDataSetChanged();
}
public View createView(int listViewItemType, ViewGroup parent) {
switch(listViewItemType) {
case SINGLEBUTTON:
{
return inflater.inflate(R.layout.singlebutton, parent, false);
}
case DOUBLEBUTTON:
{
return inflater.inflate(R.layout.doublebutton, parent, false);
}
case SPACE:
{
return inflater.inflate(R.layout.space, parent, false);
}
case HEADLINE:
{
return inflater.inflate(R.layout.headline, parent, false);
}
}
return null;
}
public void bindView(View view, ListItem item) {
String[] strings = item.getStrings();
switch(item.getType()) {
case SINGLEBUTTON:
{
Button bRun = view.findViewById(R.id.singleButton_button);
Button text = view.findViewById(R.id.singleButton_text);
bRun.setText(strings[2]);
text.setText(strings[0]);
break;
}
case DOUBLEBUTTON:
{
Button bLeft = (Button) view.findViewById(R.id.doubleButton_bLeft);
Button bRight = (Button) view.findViewById(R.id.doubleButton_bRight);
Button text = (Button) view.findViewById(R.id.doubleButton_text);
bLeft.setText(strings[3]);
bRight.setText(strings[4]);
text.setText(strings[0]);
break;
}
case SPACE:
{
// NOP
break;
}
case HEADLINE:
{
Button text = (Button) view.findViewById(R.id.headline_text);
text.setText(strings[0]);
break;
}
}
}
public static class ListItemViewHolder extends RecyclerView.ViewHolder {
private View itemView;
public ListItemViewHolder(@NonNull @NotNull View itemView) {
super(itemView);
this.itemView = itemView;
}
public void setItemView(View itemView) {
this.itemView = itemView;
}
public View getItemView() {
return itemView;
}
}
}

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_flur" android:id="@+id/fragment_flur"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@ -25,13 +26,14 @@
android:hapticFeedbackEnabled="true" android:hapticFeedbackEnabled="true"
android:src="@drawable/add_black_24dp" /> android:src="@drawable/add_black_24dp" />
<ListView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/listView" android:id="@+id/listView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginTop="65dp" android:layout_marginTop="65dp"
android:visibility="visible" android:visibility="visible"
android:divider="@null" android:divider="@null"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:dividerHeight="0dp" android:dividerHeight="0dp"
/> />