diff --git a/qmui/src/main/java/com/qmuiteam/qmui/recyclerView/QMUISwipeAction.java b/qmui/src/main/java/com/qmuiteam/qmui/recyclerView/QMUISwipeAction.java index 924ac0580..3b085b0fc 100644 --- a/qmui/src/main/java/com/qmuiteam/qmui/recyclerView/QMUISwipeAction.java +++ b/qmui/src/main/java/com/qmuiteam/qmui/recyclerView/QMUISwipeAction.java @@ -217,8 +217,8 @@ public static class ActionBuilder { int mPaddingStartEnd = 0; int mOrientation = VERTICAL; boolean mReverseDrawOrder = false; - TimeInterpolator mSwipeMoveInterpolator = QMUIInterpolatorStaticHolder.LINEAR_INTERPOLATOR; - int mSwipePxPerMS = 3; + TimeInterpolator mSwipeMoveInterpolator = QMUIInterpolatorStaticHolder.ACCELERATE_INTERPOLATOR; + int mSwipePxPerMS = 2; public ActionBuilder text(String text) { mText = text; diff --git a/qmuidemo/src/main/AndroidManifest.xml b/qmuidemo/src/main/AndroidManifest.xml index 94343938c..a2deefd27 100644 --- a/qmuidemo/src/main/AndroidManifest.xml +++ b/qmuidemo/src/main/AndroidManifest.xml @@ -7,6 +7,8 @@ + + mAdapter; + + private QDItemDescription mQDItemDescription; + + @Override + protected View onCreateView() { + View root = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_pull_refresh_and_load_more_test_layout, null); + ButterKnife.bind(this, root); + + QDDataManager QDDataManager = com.qmuiteam.qmuidemo.manager.QDDataManager.getInstance(); + mQDItemDescription = QDDataManager.getDescription(this.getClass()); + initTopBar(); + initData(); + + return root; + } + + private void initTopBar() { + mTopBar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + popBackStack(); + } + }); + + mTopBar.setTitle(mQDItemDescription.getName()); + } + + private void initData() { + + mPullLayout.setActionListener(new QMUIPullLayout.ActionListener() { + @Override + public void onActionTriggered(@NonNull QMUIPullLayout.PullAction pullAction) { + mPullLayout.postDelayed(new Runnable() { + @Override + public void run() { + if (pullAction.getPullEdge() == QMUIPullLayout.PULL_EDGE_TOP) { + onRefreshData(); + } else if (pullAction.getPullEdge() == QMUIPullLayout.PULL_EDGE_BOTTOM) { + onLoadMore(); + } + mPullLayout.finishActionRun(pullAction); + } + }, 3000); + } + }); + + QMUIRVItemSwipeAction swipeAction = new QMUIRVItemSwipeAction(true, new QMUIRVItemSwipeAction.Callback() { + @Override + public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { + mAdapter.remove(viewHolder.getAdapterPosition()); + } + + @Override + public int getSwipeDirection(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) { + return QMUIRVItemSwipeAction.SWIPE_LEFT; + } + + @Override + public void onClickAction(QMUIRVItemSwipeAction swipeAction, RecyclerView.ViewHolder selected, QMUISwipeAction action) { + super.onClickAction(swipeAction, selected, action); + mAdapter.remove(selected.getAdapterPosition()); + Toast.makeText(getContext(), + "你点击了第 " + selected.getAdapterPosition() + " 个 item 的" + action.getText(), + Toast.LENGTH_SHORT).show(); + swipeAction.clear(); + } + }); + swipeAction.attachToRecyclerView(mRecyclerView); + mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()) { + @Override + public RecyclerView.LayoutParams generateDefaultLayoutParams() { + return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT); + } + }); + + mAdapter = new BaseRecyclerAdapter(getContext(), null) { + @Override + public int getItemLayoutId(int viewType) { + return android.R.layout.simple_list_item_1; + } + + @Override + public void bindData(RecyclerViewHolder holder, int position, String item) { + holder.setText(android.R.id.text1, item); + } + }; + mAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() { + @Override + public void onItemClick(View itemView, int pos) { + Toast.makeText(getContext(), "click position=" + pos, Toast.LENGTH_SHORT).show(); + } + }); + mRecyclerView.setAdapter(mAdapter); + onDataLoaded(); + } + + private void onDataLoaded() { + List data = new ArrayList<>(Arrays.asList("Helps", "Maintain", "Liver", "Health", "Function", "Supports", "Healthy", "Fat", + "Metabolism", "Nuturally", "Bracket", "Refrigerator", "Bathtub", "Wardrobe", "Comb", "Apron", "Carpet", "Bolster", "Pillow", "Cushion")); + Collections.shuffle(data); + mAdapter.setData(data); + } + + private void onRefreshData() { + List data = new ArrayList<>(); + long id = System.currentTimeMillis(); + for (int i = 0; i < 10; i++) { + data.add("onRefreshData-" + id + "-" + i); + } + mAdapter.prepend(data); + mRecyclerView.scrollToPosition(0); + } + + private void onLoadMore() { + List data = new ArrayList<>(); + long id = System.currentTimeMillis(); + for (int i = 0; i < 10; i++) { + data.add("onLoadMore-" + id + "-" + i); + } + mAdapter.append(data); + } +} diff --git a/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/QDRecyclerViewSwipeActionFragment.java b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/swipeAction/QDRVSwipeMutiActionFragment.java similarity index 93% rename from qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/QDRecyclerViewSwipeActionFragment.java rename to qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/swipeAction/QDRVSwipeMutiActionFragment.java index 6a349aba1..fa468a4ac 100644 --- a/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/QDRecyclerViewSwipeActionFragment.java +++ b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/swipeAction/QDRVSwipeMutiActionFragment.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.qmuiteam.qmuidemo.fragment.components; +package com.qmuiteam.qmuidemo.fragment.components.swipeAction; import android.content.Context; import android.graphics.Color; @@ -42,6 +42,7 @@ import com.qmuiteam.qmuidemo.base.BaseFragment; import com.qmuiteam.qmuidemo.base.BaseRecyclerAdapter; import com.qmuiteam.qmuidemo.base.RecyclerViewHolder; +import com.qmuiteam.qmuidemo.lib.Group; import com.qmuiteam.qmuidemo.lib.annotation.Widget; import com.qmuiteam.qmuidemo.manager.QDDataManager; import com.qmuiteam.qmuidemo.model.QDItemDescription; @@ -54,9 +55,9 @@ import butterknife.BindView; import butterknife.ButterKnife; -@Widget(widgetClass = QMUIRVItemSwipeAction.class, iconRes = R.mipmap.icon_grid_rv_item_swipe_action) +@Widget(group = Group.Other, name = "Swipe Left: Muti Actions") @LatestVisitRecord -public class QDRecyclerViewSwipeActionFragment extends BaseFragment { +public class QDRVSwipeMutiActionFragment extends BaseFragment { @BindView(R.id.topbar) QMUITopBarLayout mTopBar; @BindView(R.id.pull_layout) @@ -124,11 +125,14 @@ public int getSwipeDirection(@NonNull RecyclerView recyclerView, @NonNull Recycl @Override public void onClickAction(QMUIRVItemSwipeAction swipeAction, RecyclerView.ViewHolder selected, QMUISwipeAction action) { super.onClickAction(swipeAction, selected, action); - mAdapter.remove(selected.getAdapterPosition()); Toast.makeText(getContext(), "你点击了第 " + selected.getAdapterPosition() + " 个 item 的" + action.getText(), Toast.LENGTH_SHORT).show(); - swipeAction.clear(); + if(action == mAdapter.mDeleteAction){ + mAdapter.remove(selected.getAdapterPosition()); + }else{ + swipeAction.clear(); + } } }); swipeAction.attachToRecyclerView(mRecyclerView); @@ -175,8 +179,8 @@ class Adapter extends RecyclerView.Adapter{ private List mData = new ArrayList<>(); - private final QMUISwipeAction mDeleteAction; - private final QMUISwipeAction mWriteReviewAction; + final QMUISwipeAction mDeleteAction; + final QMUISwipeAction mWriteReviewAction; public Adapter(Context context){ QMUISwipeAction.ActionBuilder builder = new QMUISwipeAction.ActionBuilder() @@ -222,7 +226,7 @@ public QMUISwipeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int vie View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.simple_list_item_1, parent, false); final QMUISwipeViewHolder vh = new QMUISwipeViewHolder(view); vh.addSwipeAction(mDeleteAction); -// vh.addSwipeAction(mWriteReviewAction); + vh.addSwipeAction(mWriteReviewAction); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/swipeAction/QDRVSwipeMutiActionOnlyIconFragment.java b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/swipeAction/QDRVSwipeMutiActionOnlyIconFragment.java new file mode 100644 index 000000000..561a2630f --- /dev/null +++ b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/swipeAction/QDRVSwipeMutiActionOnlyIconFragment.java @@ -0,0 +1,257 @@ +/* + * Tencent is pleased to support the open source community by making QMUI_Android available. + * + * Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the MIT License (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * + * http://opensource.org/licenses/MIT + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.qmuiteam.qmuidemo.fragment.components.swipeAction; + +import android.content.Context; +import android.graphics.Color; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import com.qmuiteam.qmui.recyclerView.QMUIRVItemSwipeAction; +import com.qmuiteam.qmui.recyclerView.QMUISwipeAction; +import com.qmuiteam.qmui.recyclerView.QMUISwipeViewHolder; +import com.qmuiteam.qmui.util.QMUIDisplayHelper; +import com.qmuiteam.qmui.widget.QMUITopBarLayout; +import com.qmuiteam.qmui.widget.pullLayout.QMUIPullLayout; +import com.qmuiteam.qmuidemo.R; +import com.qmuiteam.qmuidemo.base.BaseFragment; +import com.qmuiteam.qmuidemo.lib.Group; +import com.qmuiteam.qmuidemo.lib.annotation.Widget; +import com.qmuiteam.qmuidemo.manager.QDDataManager; +import com.qmuiteam.qmuidemo.model.QDItemDescription; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import butterknife.BindView; +import butterknife.ButterKnife; + +@Widget(group = Group.Other, name = "Swipe Left: Muti Actions With Only Icon") +public class QDRVSwipeMutiActionOnlyIconFragment extends BaseFragment { + @BindView(R.id.topbar) + QMUITopBarLayout mTopBar; + @BindView(R.id.pull_layout) + QMUIPullLayout mPullLayout; + @BindView(R.id.recyclerView) + RecyclerView mRecyclerView; + private Adapter mAdapter; + + private QDItemDescription mQDItemDescription; + + @Override + protected View onCreateView() { + View root = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_pull_refresh_and_load_more_test_layout, null); + ButterKnife.bind(this, root); + + QDDataManager QDDataManager = com.qmuiteam.qmuidemo.manager.QDDataManager.getInstance(); + mQDItemDescription = QDDataManager.getDescription(this.getClass()); + initTopBar(); + initData(); + + return root; + } + + private void initTopBar() { + mTopBar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + popBackStack(); + } + }); + + mTopBar.setTitle(mQDItemDescription.getName()); + } + + private void initData() { + + mPullLayout.setActionListener(new QMUIPullLayout.ActionListener() { + @Override + public void onActionTriggered(@NonNull QMUIPullLayout.PullAction pullAction) { + mPullLayout.postDelayed(new Runnable() { + @Override + public void run() { + if (pullAction.getPullEdge() == QMUIPullLayout.PULL_EDGE_TOP) { + onRefreshData(); + } else if (pullAction.getPullEdge() == QMUIPullLayout.PULL_EDGE_BOTTOM) { + onLoadMore(); + } + mPullLayout.finishActionRun(pullAction); + } + }, 3000); + } + }); + + QMUIRVItemSwipeAction swipeAction = new QMUIRVItemSwipeAction(true, new QMUIRVItemSwipeAction.Callback() { + @Override + public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { + mAdapter.remove(viewHolder.getAdapterPosition()); + } + + @Override + public int getSwipeDirection(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) { + return QMUIRVItemSwipeAction.SWIPE_LEFT; + } + + @Override + public void onClickAction(QMUIRVItemSwipeAction swipeAction, RecyclerView.ViewHolder selected, QMUISwipeAction action) { + super.onClickAction(swipeAction, selected, action); + Toast.makeText(getContext(), + "你点击了第 " + selected.getAdapterPosition() + " 个 item 的" + action.getText(), + Toast.LENGTH_SHORT).show(); + if(mAdapter.mAction1 == action){ + mAdapter.remove(selected.getAdapterPosition()); + }else{ + swipeAction.clear(); + } + } + }); + swipeAction.attachToRecyclerView(mRecyclerView); + mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()) { + @Override + public RecyclerView.LayoutParams generateDefaultLayoutParams() { + return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT); + } + }); + + mAdapter = new Adapter(getContext()); + mRecyclerView.setAdapter(mAdapter); + onDataLoaded(); + } + + private void onDataLoaded() { + List data = new ArrayList<>(Arrays.asList("Helps", "Maintain", "Liver", "Health", "Function", "Supports", "Healthy", "Fat", + "Metabolism", "Nuturally", "Bracket", "Refrigerator", "Bathtub", "Wardrobe", "Comb", "Apron", "Carpet", "Bolster", "Pillow", "Cushion")); + Collections.shuffle(data); + mAdapter.setData(data); + } + + private void onRefreshData() { + List data = new ArrayList<>(); + long id = System.currentTimeMillis(); + for (int i = 0; i < 10; i++) { + data.add("onRefreshData-" + id + "-" + i); + } + mAdapter.prepend(data); + mRecyclerView.scrollToPosition(0); + } + + private void onLoadMore() { + List data = new ArrayList<>(); + long id = System.currentTimeMillis(); + for (int i = 0; i < 10; i++) { + data.add("onLoadMore-" + id + "-" + i); + } + mAdapter.append(data); + } + + class Adapter extends RecyclerView.Adapter{ + + private List mData = new ArrayList<>(); + + final QMUISwipeAction mAction1; + final QMUISwipeAction mAction2; + + public Adapter(Context context){ + QMUISwipeAction.ActionBuilder builder = new QMUISwipeAction.ActionBuilder() + .textSize(QMUIDisplayHelper.sp2px(context, 14)) + .textColor(Color.WHITE) + .paddingStartEnd(QMUIDisplayHelper.dp2px(getContext(), 14)); + + mAction1 = builder + .backgroundColor(Color.RED) + .icon(ContextCompat.getDrawable(context, R.drawable.icon_quick_action_delete_line)) + .orientation(QMUISwipeAction.ActionBuilder.VERTICAL) + .reverseDrawOrder(false) + .build(); + mAction2 = builder + .backgroundColor(Color.BLUE) + .icon(ContextCompat.getDrawable(context, R.drawable.icon_quick_action_share)) + .orientation(QMUISwipeAction.ActionBuilder.VERTICAL) + .reverseDrawOrder(true) + .build(); + } + + public void setData(@Nullable List list) { + mData.clear(); + if(list != null){ + mData.addAll(list); + } + notifyDataSetChanged(); + } + + public void remove(int pos){ + mData.remove(pos); + notifyItemRemoved(pos); + } + + public void add(int pos, String item) { + mData.add(pos, item); + notifyItemInserted(pos); + } + + public void prepend(@NonNull List items){ + mData.addAll(0, items); + notifyDataSetChanged(); + } + + public void append(@NonNull List items){ + mData.addAll(items); + notifyDataSetChanged(); + } + + @NonNull + @Override + public QMUISwipeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.simple_list_item_1, parent, false); + final QMUISwipeViewHolder vh = new QMUISwipeViewHolder(view); + vh.addSwipeAction(mAction1); + vh.addSwipeAction(mAction2); + view.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Toast.makeText(getContext(), + "click position=" + vh.getAdapterPosition(), + Toast.LENGTH_SHORT).show(); + } + }); + return vh; + } + + @Override + public void onBindViewHolder(@NonNull QMUISwipeViewHolder holder, int position) { + TextView textView = holder.itemView.findViewById(R.id.text); + textView.setText(mData.get(position)); + } + + @Override + public int getItemCount() { + return mData.size(); + } + } +} diff --git a/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/swipeAction/QDRVSwipeMutiActionWithIconFragment.java b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/swipeAction/QDRVSwipeMutiActionWithIconFragment.java new file mode 100644 index 000000000..ab2ccec83 --- /dev/null +++ b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/swipeAction/QDRVSwipeMutiActionWithIconFragment.java @@ -0,0 +1,278 @@ +/* + * Tencent is pleased to support the open source community by making QMUI_Android available. + * + * Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the MIT License (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * + * http://opensource.org/licenses/MIT + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.qmuiteam.qmuidemo.fragment.components.swipeAction; + +import android.content.Context; +import android.graphics.Color; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.core.content.ContextCompat; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import com.qmuiteam.qmui.arch.annotation.LatestVisitRecord; +import com.qmuiteam.qmui.recyclerView.QMUIRVItemSwipeAction; +import com.qmuiteam.qmui.recyclerView.QMUISwipeAction; +import com.qmuiteam.qmui.recyclerView.QMUISwipeViewHolder; +import com.qmuiteam.qmui.util.QMUIDisplayHelper; +import com.qmuiteam.qmui.widget.QMUITopBarLayout; +import com.qmuiteam.qmui.widget.pullLayout.QMUIPullLayout; +import com.qmuiteam.qmuidemo.R; +import com.qmuiteam.qmuidemo.base.BaseFragment; +import com.qmuiteam.qmuidemo.lib.Group; +import com.qmuiteam.qmuidemo.lib.annotation.Widget; +import com.qmuiteam.qmuidemo.manager.QDDataManager; +import com.qmuiteam.qmuidemo.model.QDItemDescription; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import butterknife.BindView; +import butterknife.ButterKnife; + +@Widget(group = Group.Other, name = "Swipe Left: Muti Actions With Icon") +public class QDRVSwipeMutiActionWithIconFragment extends BaseFragment { + @BindView(R.id.topbar) + QMUITopBarLayout mTopBar; + @BindView(R.id.pull_layout) + QMUIPullLayout mPullLayout; + @BindView(R.id.recyclerView) + RecyclerView mRecyclerView; + private Adapter mAdapter; + + private QDItemDescription mQDItemDescription; + + @Override + protected View onCreateView() { + View root = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_pull_refresh_and_load_more_test_layout, null); + ButterKnife.bind(this, root); + + QDDataManager QDDataManager = com.qmuiteam.qmuidemo.manager.QDDataManager.getInstance(); + mQDItemDescription = QDDataManager.getDescription(this.getClass()); + initTopBar(); + initData(); + + return root; + } + + private void initTopBar() { + mTopBar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + popBackStack(); + } + }); + + mTopBar.setTitle(mQDItemDescription.getName()); + } + + private void initData() { + + mPullLayout.setActionListener(new QMUIPullLayout.ActionListener() { + @Override + public void onActionTriggered(@NonNull QMUIPullLayout.PullAction pullAction) { + mPullLayout.postDelayed(new Runnable() { + @Override + public void run() { + if (pullAction.getPullEdge() == QMUIPullLayout.PULL_EDGE_TOP) { + onRefreshData(); + } else if (pullAction.getPullEdge() == QMUIPullLayout.PULL_EDGE_BOTTOM) { + onLoadMore(); + } + mPullLayout.finishActionRun(pullAction); + } + }, 3000); + } + }); + + QMUIRVItemSwipeAction swipeAction = new QMUIRVItemSwipeAction(true, new QMUIRVItemSwipeAction.Callback() { + @Override + public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { + mAdapter.remove(viewHolder.getAdapterPosition()); + } + + @Override + public int getSwipeDirection(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) { + return QMUIRVItemSwipeAction.SWIPE_LEFT; + } + + @Override + public void onClickAction(QMUIRVItemSwipeAction swipeAction, RecyclerView.ViewHolder selected, QMUISwipeAction action) { + super.onClickAction(swipeAction, selected, action); + Toast.makeText(getContext(), + "你点击了第 " + selected.getAdapterPosition() + " 个 item 的" + action.getText(), + Toast.LENGTH_SHORT).show(); + if(mAdapter.mAction1 == action){ + mAdapter.remove(selected.getAdapterPosition()); + }else{ + swipeAction.clear(); + } + } + }); + swipeAction.attachToRecyclerView(mRecyclerView); + mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()) { + @Override + public RecyclerView.LayoutParams generateDefaultLayoutParams() { + return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT); + } + }); + + mAdapter = new Adapter(getContext()); + mRecyclerView.setAdapter(mAdapter); + onDataLoaded(); + } + + private void onDataLoaded() { + List data = new ArrayList<>(Arrays.asList("Helps", "Maintain", "Liver", "Health", "Function", "Supports", "Healthy", "Fat", + "Metabolism", "Nuturally", "Bracket", "Refrigerator", "Bathtub", "Wardrobe", "Comb", "Apron", "Carpet", "Bolster", "Pillow", "Cushion")); + Collections.shuffle(data); + mAdapter.setData(data); + } + + private void onRefreshData() { + List data = new ArrayList<>(); + long id = System.currentTimeMillis(); + for (int i = 0; i < 10; i++) { + data.add("onRefreshData-" + id + "-" + i); + } + mAdapter.prepend(data); + mRecyclerView.scrollToPosition(0); + } + + private void onLoadMore() { + List data = new ArrayList<>(); + long id = System.currentTimeMillis(); + for (int i = 0; i < 10; i++) { + data.add("onLoadMore-" + id + "-" + i); + } + mAdapter.append(data); + } + + class Adapter extends RecyclerView.Adapter{ + + private List mData = new ArrayList<>(); + + final QMUISwipeAction mAction1; + final QMUISwipeAction mAction2; + final QMUISwipeAction mAction3; + final QMUISwipeAction mAction4; + + public Adapter(Context context){ + QMUISwipeAction.ActionBuilder builder = new QMUISwipeAction.ActionBuilder() + .textSize(QMUIDisplayHelper.sp2px(context, 14)) + .textColor(Color.WHITE) + .paddingStartEnd(QMUIDisplayHelper.dp2px(getContext(), 14)); + + mAction1 = builder + .text("删除") + .backgroundColor(Color.RED) + .icon(ContextCompat.getDrawable(context, R.drawable.icon_quick_action_delete_line)) + .orientation(QMUISwipeAction.ActionBuilder.VERTICAL) + .reverseDrawOrder(false) + .build(); + mAction2 = builder + .text("查词典") + .backgroundColor(Color.BLUE) + .icon(ContextCompat.getDrawable(context, R.drawable.icon_quick_action_dict)) + .orientation(QMUISwipeAction.ActionBuilder.VERTICAL) + .reverseDrawOrder(true) + .build(); + mAction3 = builder + .text("分享") + .backgroundColor(Color.BLACK) + .icon(ContextCompat.getDrawable(context, R.drawable.icon_quick_action_share)) + .orientation(QMUISwipeAction.ActionBuilder.HORIZONTAL) + .reverseDrawOrder(false) + .build(); + mAction4 = builder + .text("复制") + .backgroundColor(Color.GRAY) + .icon(ContextCompat.getDrawable(context, R.drawable.icon_quick_action_copy)) + .orientation(QMUISwipeAction.ActionBuilder.HORIZONTAL) + .reverseDrawOrder(true) + .build(); + } + + public void setData(@Nullable List list) { + mData.clear(); + if(list != null){ + mData.addAll(list); + } + notifyDataSetChanged(); + } + + public void remove(int pos){ + mData.remove(pos); + notifyItemRemoved(pos); + } + + public void add(int pos, String item) { + mData.add(pos, item); + notifyItemInserted(pos); + } + + public void prepend(@NonNull List items){ + mData.addAll(0, items); + notifyDataSetChanged(); + } + + public void append(@NonNull List items){ + mData.addAll(items); + notifyDataSetChanged(); + } + + @NonNull + @Override + public QMUISwipeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.simple_list_item_1, parent, false); + final QMUISwipeViewHolder vh = new QMUISwipeViewHolder(view); + vh.addSwipeAction(mAction1); + vh.addSwipeAction(mAction2); + vh.addSwipeAction(mAction3); + vh.addSwipeAction(mAction4); + view.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Toast.makeText(getContext(), + "click position=" + vh.getAdapterPosition(), + Toast.LENGTH_SHORT).show(); + } + }); + return vh; + } + + @Override + public void onBindViewHolder(@NonNull QMUISwipeViewHolder holder, int position) { + TextView textView = holder.itemView.findViewById(R.id.text); + textView.setText(mData.get(position)); + } + + @Override + public int getItemCount() { + return mData.size(); + } + } +} diff --git a/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/swipeAction/QDRVSwipeSingleDeleteActionFragment.java b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/swipeAction/QDRVSwipeSingleDeleteActionFragment.java new file mode 100644 index 000000000..f7aee23fd --- /dev/null +++ b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/swipeAction/QDRVSwipeSingleDeleteActionFragment.java @@ -0,0 +1,236 @@ +/* + * Tencent is pleased to support the open source community by making QMUI_Android available. + * + * Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the MIT License (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * + * http://opensource.org/licenses/MIT + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.qmuiteam.qmuidemo.fragment.components.swipeAction; + +import android.content.Context; +import android.graphics.Color; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import com.qmuiteam.qmui.recyclerView.QMUIRVItemSwipeAction; +import com.qmuiteam.qmui.recyclerView.QMUISwipeAction; +import com.qmuiteam.qmui.recyclerView.QMUISwipeViewHolder; +import com.qmuiteam.qmui.util.QMUIDisplayHelper; +import com.qmuiteam.qmui.widget.QMUITopBarLayout; +import com.qmuiteam.qmui.widget.pullLayout.QMUIPullLayout; +import com.qmuiteam.qmuidemo.R; +import com.qmuiteam.qmuidemo.base.BaseFragment; +import com.qmuiteam.qmuidemo.lib.Group; +import com.qmuiteam.qmuidemo.lib.annotation.Widget; +import com.qmuiteam.qmuidemo.manager.QDDataManager; +import com.qmuiteam.qmuidemo.model.QDItemDescription; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import butterknife.BindView; +import butterknife.ButterKnife; + +@Widget(group = Group.Other, name = "Swipe Left: Single Action And Allow Deletion") +public class QDRVSwipeSingleDeleteActionFragment extends BaseFragment { + @BindView(R.id.topbar) + QMUITopBarLayout mTopBar; + @BindView(R.id.pull_layout) + QMUIPullLayout mPullLayout; + @BindView(R.id.recyclerView) + RecyclerView mRecyclerView; + private Adapter mAdapter; + + private QDItemDescription mQDItemDescription; + + @Override + protected View onCreateView() { + View root = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_pull_refresh_and_load_more_test_layout, null); + ButterKnife.bind(this, root); + + QDDataManager QDDataManager = com.qmuiteam.qmuidemo.manager.QDDataManager.getInstance(); + mQDItemDescription = QDDataManager.getDescription(this.getClass()); + initTopBar(); + initData(); + + return root; + } + + private void initTopBar() { + mTopBar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + popBackStack(); + } + }); + + mTopBar.setTitle(mQDItemDescription.getName()); + } + + private void initData() { + + mPullLayout.setActionListener(new QMUIPullLayout.ActionListener() { + @Override + public void onActionTriggered(@NonNull QMUIPullLayout.PullAction pullAction) { + mPullLayout.postDelayed(new Runnable() { + @Override + public void run() { + if (pullAction.getPullEdge() == QMUIPullLayout.PULL_EDGE_TOP) { + onRefreshData(); + } else if (pullAction.getPullEdge() == QMUIPullLayout.PULL_EDGE_BOTTOM) { + onLoadMore(); + } + mPullLayout.finishActionRun(pullAction); + } + }, 3000); + } + }); + + QMUIRVItemSwipeAction swipeAction = new QMUIRVItemSwipeAction(true, new QMUIRVItemSwipeAction.Callback() { + @Override + public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { + mAdapter.remove(viewHolder.getAdapterPosition()); + } + + @Override + public int getSwipeDirection(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) { + return QMUIRVItemSwipeAction.SWIPE_LEFT; + } + + @Override + public void onClickAction(QMUIRVItemSwipeAction swipeAction, RecyclerView.ViewHolder selected, QMUISwipeAction action) { + super.onClickAction(swipeAction, selected, action); + mAdapter.remove(selected.getAdapterPosition()); + } + }); + swipeAction.attachToRecyclerView(mRecyclerView); + mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()) { + @Override + public RecyclerView.LayoutParams generateDefaultLayoutParams() { + return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT); + } + }); + + mAdapter = new Adapter(getContext()); + mRecyclerView.setAdapter(mAdapter); + onDataLoaded(); + } + + private void onDataLoaded() { + List data = new ArrayList<>(Arrays.asList("Helps", "Maintain", "Liver", "Health", "Function", "Supports", "Healthy", "Fat", + "Metabolism", "Nuturally", "Bracket", "Refrigerator", "Bathtub", "Wardrobe", "Comb", "Apron", "Carpet", "Bolster", "Pillow", "Cushion")); + Collections.shuffle(data); + mAdapter.setData(data); + } + + private void onRefreshData() { + List data = new ArrayList<>(); + long id = System.currentTimeMillis(); + for (int i = 0; i < 10; i++) { + data.add("onRefreshData-" + id + "-" + i); + } + mAdapter.prepend(data); + mRecyclerView.scrollToPosition(0); + } + + private void onLoadMore() { + List data = new ArrayList<>(); + long id = System.currentTimeMillis(); + for (int i = 0; i < 10; i++) { + data.add("onLoadMore-" + id + "-" + i); + } + mAdapter.append(data); + } + + class Adapter extends RecyclerView.Adapter { + + private List mData = new ArrayList<>(); + + private final QMUISwipeAction mDeleteAction; + + public Adapter(Context context) { + QMUISwipeAction.ActionBuilder builder = new QMUISwipeAction.ActionBuilder() + .textSize(QMUIDisplayHelper.sp2px(context, 14)) + .textColor(Color.WHITE) + .paddingStartEnd(QMUIDisplayHelper.dp2px(getContext(), 14)); + + mDeleteAction = builder.text("删除").backgroundColor(Color.RED).build(); + } + + public void setData(@Nullable List list) { + mData.clear(); + if (list != null) { + mData.addAll(list); + } + notifyDataSetChanged(); + } + + public void remove(int pos) { + mData.remove(pos); + notifyItemRemoved(pos); + } + + public void add(int pos, String item) { + mData.add(pos, item); + notifyItemInserted(pos); + } + + public void prepend(@NonNull List items) { + mData.addAll(0, items); + notifyDataSetChanged(); + } + + public void append(@NonNull List items) { + mData.addAll(items); + notifyDataSetChanged(); + } + + @NonNull + @Override + public QMUISwipeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.simple_list_item_1, parent, false); + final QMUISwipeViewHolder vh = new QMUISwipeViewHolder(view); + vh.addSwipeAction(mDeleteAction); + view.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Toast.makeText(getContext(), + "click position=" + vh.getAdapterPosition(), + Toast.LENGTH_SHORT).show(); + } + }); + return vh; + } + + @Override + public void onBindViewHolder(@NonNull QMUISwipeViewHolder holder, int position) { + TextView textView = holder.itemView.findViewById(R.id.text); + textView.setText(mData.get(position)); + } + + @Override + public int getItemCount() { + return mData.size(); + } + } +} diff --git a/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/swipeAction/QDRVSwipeUpDeleteFragment.java b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/swipeAction/QDRVSwipeUpDeleteFragment.java new file mode 100644 index 000000000..1431bf556 --- /dev/null +++ b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/fragment/components/swipeAction/QDRVSwipeUpDeleteFragment.java @@ -0,0 +1,154 @@ +/* + * Tencent is pleased to support the open source community by making QMUI_Android available. + * + * Copyright (C) 2017-2018 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the MIT License (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * + * http://opensource.org/licenses/MIT + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.qmuiteam.qmuidemo.fragment.components.swipeAction; + +import android.app.Service; +import android.os.Vibrator; +import android.view.LayoutInflater; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.PagerSnapHelper; +import androidx.recyclerview.widget.RecyclerView; + +import com.qmuiteam.qmui.QMUIInterpolatorStaticHolder; +import com.qmuiteam.qmui.recyclerView.QMUIRVItemSwipeAction; +import com.qmuiteam.qmui.widget.QMUITopBarLayout; +import com.qmuiteam.qmui.widget.pullLayout.QMUIPullLayout; +import com.qmuiteam.qmuidemo.R; +import com.qmuiteam.qmuidemo.adaptor.QDRecyclerViewAdapter; +import com.qmuiteam.qmuidemo.base.BaseFragment; +import com.qmuiteam.qmuidemo.lib.Group; +import com.qmuiteam.qmuidemo.lib.annotation.Widget; +import com.qmuiteam.qmuidemo.manager.QDDataManager; +import com.qmuiteam.qmuidemo.model.QDItemDescription; + +import butterknife.BindView; +import butterknife.ButterKnife; + +@Widget(group = Group.Other, name = "Swipe Up: Long Press To Swipe Delete") +public class QDRVSwipeUpDeleteFragment extends BaseFragment { + @BindView(R.id.topbar) + QMUITopBarLayout mTopBar; + @BindView(R.id.pull_layout) + QMUIPullLayout mPullLayout; + @BindView(R.id.recyclerView) + RecyclerView mRecyclerView; + private QDRecyclerViewAdapter mAdapter; + + private QDItemDescription mQDItemDescription; + + @Override + protected View onCreateView() { + View root = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_pull_horizontal_test_layout, null); + ButterKnife.bind(this, root); + + QDDataManager QDDataManager = com.qmuiteam.qmuidemo.manager.QDDataManager.getInstance(); + mQDItemDescription = QDDataManager.getDescription(this.getClass()); + initTopBar(); + initData(); + + return root; + } + + private void initTopBar() { + mTopBar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + popBackStack(); + } + }); + + mTopBar.setTitle(mQDItemDescription.getName()); + } + + private void initData() { + mPullLayout.setActionListener(new QMUIPullLayout.ActionListener() { + @Override + public void onActionTriggered(QMUIPullLayout.PullAction pullAction) { + mPullLayout.postDelayed(new Runnable() { + @Override + public void run() { + mPullLayout.finishActionRun(pullAction); + } + }, 1000); + } + }); + LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false); + mRecyclerView.setLayoutManager(layoutManager); + new PagerSnapHelper().attachToRecyclerView(mRecyclerView); + mAdapter = new QDRecyclerViewAdapter(); + mAdapter.setItemCount(10); + mRecyclerView.setAdapter(mAdapter); + + QMUIRVItemSwipeAction swipeAction = new QMUIRVItemSwipeAction(true, new QMUIRVItemSwipeAction.Callback() { + @Override + public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { + mAdapter.removeItem(viewHolder.getAdapterPosition()); + } + + @Override + public int getSwipeDirection(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) { + return QMUIRVItemSwipeAction.SWIPE_UP; + } + + @Override + public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) { + return 0.3f; + } + + @Override + public void onSelectedChanged(RecyclerView.ViewHolder selected) { + super.onSelectedChanged(selected); + if (selected != null) { + mTopBar.setTitle("上滑删除"); + selected.itemView.animate() + .scaleX(1.02f) + .scaleY(1.02f) + .setInterpolator(QMUIInterpolatorStaticHolder.ACCELERATE_INTERPOLATOR) + .setDuration(250) + .start(); + + // 震动 + Vibrator vibrator = (Vibrator) getContext().getSystemService(Service.VIBRATOR_SERVICE); + vibrator.vibrate(10); + } else { + mTopBar.setTitle(mQDItemDescription.getName()); + } + } + + @Override + public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { + super.clearView(recyclerView, viewHolder); + View itemView = viewHolder.itemView; + if (itemView.getScaleX() != 1f || itemView.getScaleY() != 1f) { + itemView.animate() + .scaleX(1f) + .scaleY(1f) + .setInterpolator(QMUIInterpolatorStaticHolder.DECELERATE_INTERPOLATOR) + .setDuration(250) + .start(); + } else { + itemView.animate().cancel(); + } + } + }); + swipeAction.setPressTimeToSwipe(300); + swipeAction.attachToRecyclerView(mRecyclerView); + } +} diff --git a/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/manager/QDDataManager.java b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/manager/QDDataManager.java index 267403f80..269fc4c7a 100644 --- a/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/manager/QDDataManager.java +++ b/qmuidemo/src/main/java/com/qmuiteam/qmuidemo/manager/QDDataManager.java @@ -32,7 +32,8 @@ import com.qmuiteam.qmuidemo.fragment.components.QDPullRefreshFragment; import com.qmuiteam.qmuidemo.fragment.components.QDRadiusImageViewFragment; import com.qmuiteam.qmuidemo.fragment.components.QDRecyclerViewDraggableScrollBarFragment; -import com.qmuiteam.qmuidemo.fragment.components.QDRecyclerViewSwipeActionFragment; +import com.qmuiteam.qmuidemo.fragment.components.swipeAction.QDRVSwipeActionFragment; +import com.qmuiteam.qmuidemo.fragment.components.swipeAction.QDRVSwipeMutiActionFragment; import com.qmuiteam.qmuidemo.fragment.components.QDSliderFragment; import com.qmuiteam.qmuidemo.fragment.components.QDSpanTouchFixTextViewFragment; import com.qmuiteam.qmuidemo.fragment.components.QDTabSegmentFragment; @@ -118,7 +119,7 @@ private void initComponentsDesc() { mComponentsNames.add(QDSliderFragment.class); mComponentsNames.add(QDPullFragment.class); mComponentsNames.add(QDRecyclerViewDraggableScrollBarFragment.class); - mComponentsNames.add(QDRecyclerViewSwipeActionFragment.class); + mComponentsNames.add(QDRVSwipeActionFragment.class); } /** diff --git a/qmuidemo/src/main/res/layout/recycler_view_item.xml b/qmuidemo/src/main/res/layout/recycler_view_item.xml index 991835b84..9fae499c2 100755 --- a/qmuidemo/src/main/res/layout/recycler_view_item.xml +++ b/qmuidemo/src/main/res/layout/recycler_view_item.xml @@ -24,8 +24,8 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="24dp" - android:layout_marginLeft="12dp" - android:layout_marginRight="12dp" + android:layout_marginLeft="24dp" + android:layout_marginRight="24dp" android:layout_marginTop="24dp" android:background="@drawable/pager_layout_item_bg" android:gravity="center"