Skip to content

Commit

Permalink
Merge pull request #60 from stefan-niedermann/RecyclerView
Browse files Browse the repository at this point in the history
Recycler View
  • Loading branch information
stefan-niedermann committed Jan 21, 2016
2 parents 7460d38 + 439267e commit f57a877
Show file tree
Hide file tree
Showing 15 changed files with 303 additions and 165 deletions.
2 changes: 2 additions & 0 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.0.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/design/23.0.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/gridlayout-v7/23.0.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/recyclerview-v7/23.0.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/23.0.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.commit451/bypasses/1.0.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
Expand All @@ -87,6 +88,7 @@
</content>
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="recyclerview-v7-23.0.1" level="project" />
<orderEntry type="library" exported="" name="gridlayout-v7-23.0.1" level="project" />
<orderEntry type="library" exported="" name="bypasses-1.0.1" level="project" />
<orderEntry type="library" exported="" name="support-v4-23.0.1" level="project" />
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ dependencies {
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:gridlayout-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
compile fileTree(include: ['*.jar'], dir: 'libs')
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.widget.EditText;

import java.util.Timer;
Expand All @@ -23,6 +24,7 @@ public class EditNoteActivity extends AppCompatActivity {
private EditText content = null;
private Note note = null;
private Timer timer = new Timer();
private ActionBar actionBar;

@Override
protected void onCreate(final Bundle savedInstanceState) {
Expand All @@ -34,6 +36,12 @@ protected void onCreate(final Bundle savedInstanceState) {
content.setEnabled(false);
content.setText(note.getContent());
content.setEnabled(true);
actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setTitle(note.getTitle());
actionBar.setSubtitle(getString(R.string.action_edit_editing));
actionBar.setDisplayHomeAsUpEnabled(true);
}
content.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
Expand Down Expand Up @@ -76,6 +84,22 @@ public void onBackPressed() {
finish();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
content.setEnabled(false);
saveData();
Intent data = new Intent();
data.setAction(Intent.ACTION_VIEW);
data.putExtra(NoteActivity.EDIT_NOTE, note);
setResult(RESULT_OK, data);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

private void saveData() {
ActionBar ab = getSupportActionBar();
if (ab != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.ActionMode;
import android.util.SparseBooleanArray;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Calendar;
Expand All @@ -29,7 +27,7 @@
import it.niedermann.owncloud.notes.util.ICallback;

public class NotesListViewActivity extends AppCompatActivity implements
OnItemClickListener, View.OnClickListener {
ItemAdapter.NoteClickListener, View.OnClickListener {

public final static String SELECTED_NOTE = "it.niedermann.owncloud.notes.clicked_note";
public final static String CREATED_NOTE = "it.niedermann.owncloud.notes.created_notes";
Expand All @@ -41,7 +39,7 @@ public class NotesListViewActivity extends AppCompatActivity implements
private final static int server_settings = 2;
private final static int about = 3;

private ListView listView = null;
private RecyclerView listView = null;
private ItemAdapter adapter = null;
private ActionMode mActionMode;
private SwipeRefreshLayout swipeRefreshLayout = null;
Expand Down Expand Up @@ -74,6 +72,10 @@ public void onRefresh() {
@Override
public void onFinish() {
swipeRefreshLayout.setRefreshing(false);
adapter.clearSelection();
if (mActionMode != null) {
mActionMode.finish();
}
setListView(db.getNotes());
}
});
Expand Down Expand Up @@ -170,45 +172,11 @@ public void setListView(List<Note> noteList) {
itemList.add(currentNote);
}

adapter = new ItemAdapter(getApplicationContext(), itemList);
listView = (ListView) findViewById(R.id.list_view);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
adapter = new ItemAdapter(itemList);
ItemAdapter.setNoteClickListener(this);
listView = (RecyclerView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
onListItemSelect(position);
return true;
}
});
}

/**
* A short click on one list item. Creates a new instance of NoteActivity.
*/
@Override
public void onItemClick(AdapterView<?> parentView, View childView,
int position, long id) {
Item item = adapter.getItem(position);
if (!item.isSection()) {
listView.setItemChecked(position, !listView.isItemChecked(position));
if (listView.getCheckedItemCount() < 1) {
removeSelection();
Intent intent = new Intent(getApplicationContext(),
NoteActivity.class);
if (!item.isSection()) {
intent.putExtra(SELECTED_NOTE, (Note) item);
intent.putExtra(SELECTED_NOTE_POSITION, position);
startActivityForResult(intent, show_single_note_cmd);
}
} else { // perform long click if already something is selected
onListItemSelect(position);
}
} else {
listView.setItemChecked(position, false);
}
listView.setLayoutManager(new LinearLayoutManager(this));
}

/**
Expand Down Expand Up @@ -259,9 +227,12 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == create_note_cmd) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
//not need because of db.synchronisation in createActivity

Note createdNote = (Note) data.getExtras().getSerializable(
CREATED_NOTE);
adapter.insert(createdNote, 0);
adapter.add(createdNote);
//setListView(db.getNotes());
}
} else if (requestCode == show_single_note_cmd) {
if (resultCode == RESULT_OK || resultCode == RESULT_FIRST_USER) {
Expand All @@ -271,7 +242,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Note editedNote = (Note) data.getExtras().getSerializable(
NoteActivity.EDIT_NOTE);
adapter.insert(editedNote, 0);
adapter.add(editedNote);
}
}
} else if (requestCode == server_settings) {
Expand All @@ -287,16 +258,18 @@ public void onFinish() {
}
}

/**
* Long click on one item in the list view. It starts the Action Mode and allows selecting more
* items and execute bulk functions (e. g. delete)
*
* @param position int - position of the clicked item
*/
private void onListItemSelect(int position) {
if (!adapter.getItem(position).isSection()) {
listView.setItemChecked(position, !listView.isItemChecked(position));
int checkedItemCount = listView.getCheckedItemCount();
@Override
public void onNoteClick(int position, View v) {
if (mActionMode != null) {
if (!adapter.select(position)) {
v.setSelected(false);
adapter.deselect(position);
} else {
v.setSelected(true);
}
mActionMode.setTitle(String.valueOf(adapter.getSelected().size())
+ " " + getString(R.string.ab_selected));
int checkedItemCount = adapter.getSelected().size();
boolean hasCheckedItems = checkedItemCount > 0;

if (hasCheckedItems && mActionMode == null) {
Expand All @@ -312,25 +285,34 @@ private void onListItemSelect(int position) {
// there no selected items, finish the actionMode
mActionMode.finish();
}

if (mActionMode != null) {
mActionMode.setTitle(String.valueOf(listView.getCheckedItemCount())
+ " " + getString(R.string.ab_selected));
}
} else {
listView.setItemChecked(position, false);


Intent intent = new Intent(getApplicationContext(),
NoteActivity.class);

Item item = adapter.getItem(position);
intent.putExtra(SELECTED_NOTE, (Note) item);
intent.putExtra(SELECTED_NOTE_POSITION, position);
Log.v("Note",
"notePosition | NotesListViewActivity wurde abgesendet "
+ position);
startActivityForResult(intent, show_single_note_cmd);

}
}

/**
* Removes all selections.
*/
private void removeSelection() {
SparseBooleanArray checkedItemPositions = listView
.getCheckedItemPositions();
for (int i = 0; i < checkedItemPositions.size(); i++) {
listView.setItemChecked(i, false);
@Override
public boolean onNoteLongClick(int position, View v) {
boolean selected = adapter.select(position);
if (selected) {
v.setSelected(selected);
mActionMode = startSupportActionMode(new MultiSelectedActionModeCallback());
int checkedItemCount = adapter.getSelected().size();
mActionMode.setTitle(String.valueOf(checkedItemCount)
+ " " + getString(R.string.ab_selected));
}
return selected;
}

/**
Expand Down Expand Up @@ -361,17 +343,16 @@ public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_delete:
SparseBooleanArray checkedItemPositions = listView
.getCheckedItemPositions();
for (int i = (checkedItemPositions.size() - 1); i >= 0; i--) {
if (checkedItemPositions.valueAt(i)) {
Note note = (Note) adapter.getItem(checkedItemPositions
.keyAt(i));
db.deleteNoteAndSync(note.getId());
adapter.remove(note);
}
List<Integer> selection = adapter.getSelected();
for (Integer i : selection) {
Note note = (Note) adapter.getItem(i);
db.deleteNoteAndSync(note.getId());
// Not needed because of dbsync
//adapter.remove(note);
}
mode.finish(); // Action picked, so close the CAB
//after delete selection has to be cleared
setListView(db.getNotes());
return true;
default:
return false;
Expand All @@ -380,7 +361,7 @@ public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

@Override
public void onDestroyActionMode(ActionMode mode) {
removeSelection();
adapter.clearSelection();
mActionMode = null;
adapter.notifyDataSetChanged();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RemoteViews;

import java.util.ArrayList;
Expand All @@ -28,7 +29,7 @@ public class SelectSingleNoteActivity extends AppCompatActivity implements Adapt

int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
private NoteSQLiteOpenHelper db = null;
private ListView listView = null;
private RecyclerView listView = null;
private ItemAdapter adapter = null;

@Override
Expand Down Expand Up @@ -64,11 +65,11 @@ public void onCreate(Bundle savedInstanceState) {
private void setListView(List<Note> noteList) {
List<Item> itemList = new ArrayList<>();
itemList.addAll(noteList);
adapter = new ItemAdapter(getApplicationContext(), itemList);
listView = (ListView) findViewById(R.id.select_single_note_list_view);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
adapter = new ItemAdapter(itemList);
listView = (RecyclerView) findViewById(R.id.select_single_note_list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
listView.setLayoutManager(new LinearLayoutManager(this));
//httpslistView.setOnItemClickListener(this);
}

@Override
Expand Down
Loading

0 comments on commit f57a877

Please sign in to comment.