2016-07-31 6 views
1

Hallo, es war 2 Tage Ich bin auf der Suche nach einer Lösung für diese und noch keine Ahnung, wie zu tun, ich möchte einen Swipe nach links und rechts auch neu ordnen Ansicht, wenn Benutzer nach links wischen Es wird automatisch in der fertigen Liste Liste und wenn Swipe auf der linken Seite wird es löschen Listenelement in der Recycling-Ansicht auch Benutzer kann das Listenelement neu anordnen meine lange drücken. HierHinzufügen von Swiping und Neuanordnen zu einer Recycling-Ansicht

ist Haupttätigkeit:

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(); 
     } 
    } 

Aktivität 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 Hilf mir! Danke für den Fortschritt!

+0

Werfen Sie einen Blick auf: https://github.com/ h6ah4i/android-advancedrecyclerview –

+0

Können Sie Code für meine Situation bereitstellen? Ich habe alles versucht, aber immer noch nicht an meinem Code arbeiten :( –

Antwort

0
+0

ja, ich versuchte das Itemtouchhelper, aber es zeigt mir Fehler und versuchte den Code den ganzen Tag .. können Sie bitte Code zur Verfügung stellen? Ich brauche wirklich Hilfe :( –

+0

Post the error Stapelverfolgung. –

Verwandte Themen