mirror of
https://github.com/leanote/leanote-android.git
synced 2025-10-14 22:25:40 +00:00
sort note by UpdateTime,CreateTime, Title
This commit is contained in:
@@ -342,7 +342,7 @@ public class Note extends BaseModel implements Serializable {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public static class UpdateTimeComparetor implements Comparator<Note> {
|
||||
public static class UpdateTimeDescComparetor implements Comparator<Note> {
|
||||
@Override
|
||||
public int compare(Note lhs, Note rhs) {
|
||||
long lTime = lhs.getUpdatedTimeVal();
|
||||
@@ -356,4 +356,68 @@ public class Note extends BaseModel implements Serializable {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class UpdateTimeAscComparetor implements Comparator<Note> {
|
||||
@Override
|
||||
public int compare(Note lhs, Note rhs) {
|
||||
long lTime = lhs.getUpdatedTimeVal();
|
||||
long rTime = rhs.getUpdatedTimeVal();
|
||||
if (lTime > rTime) {
|
||||
return 1;
|
||||
} else if (lTime < rTime) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class CreateTimeDescComparetor implements Comparator<Note> {
|
||||
@Override
|
||||
public int compare(Note lhs, Note rhs) {
|
||||
long lTime = lhs.getCreatedTimeVal();
|
||||
long rTime = rhs.getCreatedTimeVal();
|
||||
if (lTime > rTime) {
|
||||
return -1;
|
||||
} else if (lTime < rTime) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class CreateTimeAscComparetor implements Comparator<Note> {
|
||||
@Override
|
||||
public int compare(Note lhs, Note rhs) {
|
||||
long lTime = lhs.getCreatedTimeVal();
|
||||
long rTime = rhs.getCreatedTimeVal();
|
||||
if (lTime > rTime) {
|
||||
return 1;
|
||||
} else if (lTime < rTime) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class TitleDescComparetor implements Comparator<Note> {
|
||||
@Override
|
||||
public int compare(Note lhs, Note rhs) {
|
||||
String lTitle = lhs.getTitle();
|
||||
String rTitle = rhs.getTitle();
|
||||
return lTitle.compareTo(rTitle);
|
||||
}
|
||||
}
|
||||
|
||||
public static class TitleAscComparetor implements Comparator<Note> {
|
||||
@Override
|
||||
public int compare(Note lhs, Note rhs) {
|
||||
String lTitle = lhs.getTitle();
|
||||
String rTitle = rhs.getTitle();
|
||||
return rTitle.compareTo(lTitle);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -13,6 +13,7 @@ import org.houxg.leamonax.R;
|
||||
|
||||
public class BaseActivity extends AppCompatActivity {
|
||||
|
||||
private Toolbar mToolbar;
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -26,8 +27,13 @@ public class BaseActivity extends AppCompatActivity {
|
||||
initToolBar(toolbar, false);
|
||||
}
|
||||
|
||||
public Toolbar getToolbar() {
|
||||
return mToolbar;
|
||||
}
|
||||
|
||||
protected void initToolBar(Toolbar toolbar, boolean hasBackArrow) {
|
||||
if (toolbar != null) {
|
||||
mToolbar = toolbar;
|
||||
setSupportActionBar(toolbar);
|
||||
toolbar.setTitleTextColor(0xffFAFAFA);
|
||||
toolbar.setTitle(getTitle());
|
||||
|
@@ -7,12 +7,14 @@ import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.houxg.leamonax.Leamonax;
|
||||
@@ -28,9 +30,11 @@ import org.houxg.leamonax.utils.NetworkUtils;
|
||||
import org.houxg.leamonax.utils.SharedPreferenceUtils;
|
||||
import org.houxg.leamonax.utils.ToastUtils;
|
||||
import org.houxg.leamonax.widget.NoteList;
|
||||
import org.houxg.leamonax.widget.SelectPopupWindow;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.BindView;
|
||||
@@ -57,6 +61,7 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
|
||||
NoteList mNoteList;
|
||||
Mode mCurrentMode;
|
||||
OnSearchFinishListener mOnSearchFinishListener;
|
||||
private int mSortType = -1;
|
||||
|
||||
public void setOnSearchFinishListener(OnSearchFinishListener onSearchFinishListener) {
|
||||
this.mOnSearchFinishListener = onSearchFinishListener;
|
||||
@@ -72,6 +77,7 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
mSortType = SharedPreferenceUtils.read(SharedPreferenceUtils.CONFIG, SelectPopupWindow.SP_SORT_TYPE, -1);
|
||||
setHasOptionsMenu(true);
|
||||
}
|
||||
|
||||
@@ -86,10 +92,26 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
|
||||
if (item.getItemId() == R.id.action_view_type) {
|
||||
mNoteList.toggleType();
|
||||
SharedPreferenceUtils.write(SharedPreferenceUtils.CONFIG, SP_VIEW_TYPE, mNoteList.getType());
|
||||
} else if (item.getItemId() == R.id.action_view_more) {
|
||||
final SelectPopupWindow popupWindow = new SelectPopupWindow(getContext());
|
||||
if (getActivity() instanceof BaseActivity) {
|
||||
Toolbar toolbar = ((BaseActivity) getActivity()).getToolbar();
|
||||
popupWindow.setOnItemClickListener(new SelectPopupWindow.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int value) {
|
||||
mSortType = value;
|
||||
renderNotes();
|
||||
popupWindow.dismiss();
|
||||
}
|
||||
});
|
||||
popupWindow.showPopWindow(toolbar);
|
||||
}
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
@@ -148,13 +170,38 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
|
||||
notes = new ArrayList<>();
|
||||
}
|
||||
mNotes = notes;
|
||||
Collections.sort(mNotes, new Note.UpdateTimeComparetor());
|
||||
renderNotes();
|
||||
}
|
||||
|
||||
private void renderNotes() {
|
||||
Collections.sort(mNotes, getComparatorBySortType(mSortType));
|
||||
mNoteList.render(mNotes);
|
||||
if (mNotes.size() == 0 && mOnSearchFinishListener != null) {
|
||||
mOnSearchFinishListener.doSearchFinish();
|
||||
}
|
||||
}
|
||||
|
||||
private Comparator<Note> getComparatorBySortType(int sortType) {
|
||||
switch (sortType) {
|
||||
case 1:
|
||||
return new Note.CreateTimeAscComparetor();
|
||||
case 2:
|
||||
return new Note.CreateTimeDescComparetor();
|
||||
case 3:
|
||||
return new Note.UpdateTimeAscComparetor();
|
||||
case 4:
|
||||
return new Note.UpdateTimeDescComparetor();
|
||||
case 5:
|
||||
return new Note.TitleAscComparetor();
|
||||
case 6:
|
||||
return new Note.TitleDescComparetor();
|
||||
default:
|
||||
return new Note.UpdateTimeDescComparetor();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onClickNote(Note note) {
|
||||
if (mActionModeHandler.isActionMode()) {
|
||||
|
@@ -0,0 +1,115 @@
|
||||
package org.houxg.leamonax.widget;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.support.v7.widget.ListPopupWindow;
|
||||
import android.view.Gravity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.houxg.leamonax.R;
|
||||
import org.houxg.leamonax.utils.DisplayUtils;
|
||||
import org.houxg.leamonax.utils.SharedPreferenceUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class SelectPopupWindow extends ListPopupWindow {
|
||||
public static final String SP_SORT_TYPE = "sp_sort_type";
|
||||
private Context mContext;
|
||||
private OnItemClickListener mOnItemClickListener;
|
||||
private Adapter mAdapter;
|
||||
private int mChecked = -1;
|
||||
|
||||
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
|
||||
this.mOnItemClickListener = onItemClickListener;
|
||||
}
|
||||
|
||||
public SelectPopupWindow(Context context) {
|
||||
super(context);
|
||||
this.mContext = context;
|
||||
}
|
||||
|
||||
@SuppressLint("RestrictedApi")
|
||||
public void showPopWindow(View anchorView) {
|
||||
String[] selectKey = mContext.getResources().getStringArray(R.array.select_key);
|
||||
final String[] selectValue = mContext.getResources().getStringArray(R.array.select_value);
|
||||
mAdapter = new Adapter();
|
||||
mAdapter.setDatas(new ArrayList<>(Arrays.asList(selectKey)));
|
||||
mChecked = SharedPreferenceUtils.read(SharedPreferenceUtils.CONFIG, SP_SORT_TYPE, mChecked);
|
||||
mAdapter.setChecked(mChecked);
|
||||
setAdapter(mAdapter);
|
||||
setWidth(DisplayUtils.dp2px(180));
|
||||
setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
setDropDownGravity(Gravity.END);
|
||||
setAnchorView(anchorView);
|
||||
setOverlapAnchor(true);
|
||||
setModal(true);
|
||||
setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
int value = Integer.valueOf(selectValue[position]);
|
||||
SharedPreferenceUtils.write(SharedPreferenceUtils.CONFIG, SP_SORT_TYPE, value);
|
||||
if (mOnItemClickListener != null) {
|
||||
mOnItemClickListener.onItemClick(parent, view, value);
|
||||
}
|
||||
}
|
||||
});
|
||||
show();
|
||||
}
|
||||
|
||||
class Adapter extends BaseAdapter {
|
||||
List<String> datas = new ArrayList<>();
|
||||
private int checked = -1;
|
||||
|
||||
void setDatas(List<String> datas) {
|
||||
this.datas = datas;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return datas.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getItem(int position) {
|
||||
return datas.get(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
if (convertView == null) {
|
||||
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.simple_list_item_1, parent, false);
|
||||
}
|
||||
ImageView checkView = convertView.findViewById(R.id.check);
|
||||
TextView textView = convertView.findViewById(android.R.id.text1);
|
||||
if (checked == (position + 1)) {
|
||||
checkView.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
checkView.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
textView.setText(getItem(position));
|
||||
return convertView;
|
||||
}
|
||||
|
||||
public void setChecked(int checked) {
|
||||
this.checked = checked;
|
||||
}
|
||||
}
|
||||
|
||||
public interface OnItemClickListener {
|
||||
void onItemClick(AdapterView<?> parent, View view, int value);
|
||||
}
|
||||
|
||||
}
|
BIN
app/src/main/res/drawable-xxhdpi/ic_menu_more.png
Normal file
BIN
app/src/main/res/drawable-xxhdpi/ic_menu_more.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 599 B |
BIN
app/src/main/res/drawable-xxhdpi/ic_pop_checked.png
Normal file
BIN
app/src/main/res/drawable-xxhdpi/ic_pop_checked.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 533 B |
27
app/src/main/res/layout/simple_list_item_1.xml
Normal file
27
app/src/main/res/layout/simple_list_item_1.xml
Normal file
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/check"
|
||||
android:layout_width="36dp"
|
||||
android:scaleType="center"
|
||||
android:src="@drawable/ic_pop_checked"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@android:id/text1"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center_vertical"
|
||||
android:minHeight="36dp"
|
||||
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
|
||||
android:textAppearance="?android:attr/textAppearanceListItemSmall" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
@@ -7,4 +7,10 @@
|
||||
android:icon="@drawable/ic_view_type"
|
||||
android:title="@string/view_type"
|
||||
app:showAsAction="always"/>
|
||||
|
||||
<item
|
||||
android:id="@+id/action_view_more"
|
||||
android:icon="@drawable/ic_menu_more"
|
||||
app:showAsAction="always"
|
||||
android:title="" />
|
||||
</menu>
|
20
app/src/main/res/values-zh/arrays.xml
Normal file
20
app/src/main/res/values-zh/arrays.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string-array name="select_key">
|
||||
<item>按创建时间-升序</item>
|
||||
<item>按创建时间-降序</item>
|
||||
<item>按更新时间-升序</item>
|
||||
<item>按更新时间-降序</item>
|
||||
<item>按标题-升序</item>
|
||||
<item>按标题-降序</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="select_value">
|
||||
<item>1</item>
|
||||
<item>2</item>
|
||||
<item>3</item>
|
||||
<item>4</item>
|
||||
<item>5</item>
|
||||
<item>6</item>
|
||||
</string-array>
|
||||
</resources>
|
20
app/src/main/res/values/arrays.xml
Normal file
20
app/src/main/res/values/arrays.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string-array name="select_key">
|
||||
<item>Date Created-ASC</item>
|
||||
<item>Date Created-DESC</item>
|
||||
<item>Date Updated-ASC</item>
|
||||
<item>Date Updated-DESC</item>
|
||||
<item>Title-ASC</item>
|
||||
<item>Title-DESC</item>
|
||||
</string-array>
|
||||
|
||||
<string-array name="select_value">
|
||||
<item>1</item>
|
||||
<item>2</item>
|
||||
<item>3</item>
|
||||
<item>4</item>
|
||||
<item>5</item>
|
||||
<item>6</item>
|
||||
</string-array>
|
||||
</resources>
|
Reference in New Issue
Block a user