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

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"
/> />