Bonjour son été 2 jours je suis à la recherche d'une solution pour cela et je n'ai toujours aucune idée de comment faire, je veux faire un balayage vers la gauche et droite également réorganiser vue recycler, Lorsque l'utilisateur balayez elle sera automatiquement listée dans la liste complétée et si elle est balayée à gauche, elle supprimera l'élément de la liste dans la vue de recyclage. L'utilisateur peut également réorganiser l'élément de liste.Ajout de glisser et de réorganiser à une vue de recyclage
est ici activité principale:
public class MainActivity extends AppCompatActivity {
private RecyclerView shoppingItems;
private Toolbar toolbar;
private Realm realm;
private CoordinatorLayout coordinatorLayout;
private List<ShoppingItem> dataSet;
private RecyclerView.Adapter shoppingItemsAdapter = new RecyclerView.Adapter() {
private final int ACTIVE_VIEW=1;
private final int INACTIVE_VIEW=2;
private final int SUBHEADER_VIEW=3;
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == ACTIVE_VIEW) {
View v = getLayoutInflater().inflate(R.layout.active_item, parent, false);
return new ActiveItemViewHolder(v,
(CheckBox)v.findViewById(R.id.item_status),
(TextView)v.findViewById(R.id.item_name),
(TextView)v.findViewById(R.id.item_quantity),
(ImageView)v.findViewById(R.id.item_action)
);
} else if(viewType == INACTIVE_VIEW) {
View v = getLayoutInflater().inflate(R.layout.inactive_item, parent, false);
return new InactiveItemViewHolder(v,
(CheckBox)v.findViewById(R.id.item_status),
(TextView)v.findViewById(R.id.item_name),
(ImageView)v.findViewById(R.id.item_action)
);
} else {
View v = getLayoutInflater().inflate(R.layout.subheader, parent, false);
return new SubheaderViewHolder(v);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final ShoppingItem currentItem = dataSet.get(position);
if(currentItem.getTimestamp()==-1) return;
if(currentItem.isCompleted()) {
InactiveItemViewHolder h = (InactiveItemViewHolder)holder;
h.itemName.setText(currentItem.getName());
h.itemName.setPaintFlags(h.itemName.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
h.itemAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Task is restored", Snackbar.LENGTH_LONG);
snackbar.show();
realm.beginTransaction();
currentItem.setCompleted(false);
currentItem.setTimestamp(System.currentTimeMillis());
realm.commitTransaction();
initializeDataSet();
shoppingItemsAdapter.notifyDataSetChanged();
}
});
}else{
ActiveItemViewHolder h = (ActiveItemViewHolder)holder;
h.itemName.setText(currentItem.getName());
h.itemQuantity.setText(currentItem.getQuantity());
h.itemStatus.setChecked(false);
h.itemStatus.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
if (checked) {
realm.beginTransaction();
currentItem.setCompleted(true);
currentItem.setTimestamp(System.currentTimeMillis());
realm.commitTransaction();
initializeDataSet();
shoppingItemsAdapter.notifyDataSetChanged();
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "Task is Completed", Snackbar.LENGTH_LONG)
.setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View view) {
realm.beginTransaction();
currentItem.setCompleted(false);
currentItem.setTimestamp(System.currentTimeMillis());
realm.commitTransaction();
initializeDataSet();
shoppingItemsAdapter.notifyDataSetChanged();
Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Task is restored!", Snackbar.LENGTH_SHORT);
snackbar1.show();
}
});
snackbar.show();
}
}
});
h.itemAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, ItemActivity.class);
i.putExtra("TITLE", "Edit item");
i.putExtra("ITEM_NAME", currentItem.getName());
i.putExtra("ITEM_QUANTITY", currentItem.getQuantity());
i.putExtra("ITEM_ID", currentItem.getId());
startActivityForResult(i, 1);
}
});
}
}
@Override
public int getItemCount() {
return dataSet.size();
}
@Override
public int getItemViewType(int position) {
ShoppingItem currentItem = dataSet.get(position);
if(currentItem.getTimestamp()==-1) return SUBHEADER_VIEW;
if(currentItem.isCompleted()) return INACTIVE_VIEW;
return ACTIVE_VIEW;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RealmConfiguration configuration =
new RealmConfiguration.Builder(this).build();
Realm.setDefaultConfiguration(configuration);
realm = Realm.getDefaultInstance();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, ItemActivity.class);
i.putExtra("TITLE", "Add Task");
startActivityForResult(i, 1);
}
});
shoppingItems = (RecyclerView)findViewById(R.id.shopping_items);
shoppingItems.setLayoutManager(new LinearLayoutManager(this));
initializeDataSet();
shoppingItems.setAdapter(shoppingItemsAdapter);
}
private void initializeDataSet() {
dataSet = new ArrayList<>();
RealmResults<ShoppingItem> activeItemResults
= realm.where(ShoppingItem.class).equalTo("completed", false)
.findAllSorted("timestamp", Sort.DESCENDING);
RealmResults<ShoppingItem> inactiveItemResults
= realm.where(ShoppingItem.class).equalTo("completed", true)
.findAllSorted("timestamp", Sort.DESCENDING);
ShoppingItem subheader = new ShoppingItem();
subheader.setTimestamp(-1);
for(ShoppingItem item:activeItemResults) dataSet.add(item);
dataSet.add(subheader);
for(ShoppingItem item:inactiveItemResults) dataSet.add(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
initializeDataSet();
shoppingItemsAdapter.notifyDataSetChanged();
}
}
activité main.xml
android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.github.hathibelagal.shoppinglist.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_add_24dp"
android:tint="@android:color/white" />
</android.support.design.widget.CoordinatorLayout>
active_item.xml:
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="72dp">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_status"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="72dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/item_name"
android:textColor="@android:color/black" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/item_quantity" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_action"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:src="@drawable/ic_create_24dp"
android:alpha="0.26" />
</RelativeLayout>
Plea Aidez-moi! Merci d'avance!
Jetez un oeil à: https://github.com/ h6ah4i/android-advancedrecyclerview –
Pouvez-vous fournir du code pour ma situation? J'essayais tout mais ne fonctionnait toujours pas sur mon code :( –