-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
适配BaseRecyclerViewAdapterHelper
- Loading branch information
AItsuki
committed
Jul 13, 2017
1 parent
0c43bf0
commit 9c630b5
Showing
15 changed files
with
400 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ buildscript { | |
allprojects { | ||
repositories { | ||
jcenter() | ||
maven { url "https://jitpack.io" } | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
demo/src/main/java/com/aitsuki/swipedemo/CymChadActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.aitsuki.swipedemo; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.support.annotation.Nullable; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.support.v7.widget.DividerItemDecoration; | ||
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.View; | ||
|
||
import com.aitsuki.swipe.SwipeItemLayout; | ||
import com.aitsuki.swipedemo.data.Repository; | ||
import com.aitsuki.swipedemo.entity.Data; | ||
import com.aitsuki.swipedemo.entity.Type; | ||
import com.aitsuki.swipedemo.util.ToastUtil; | ||
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter; | ||
import com.chad.library.adapter.base.BaseViewHolder; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Created by AItsuki on 2017/7/11. | ||
* 使用CymChad的adapter库 | ||
* <a href = "https://github.com/CymChad/BaseRecyclerViewAdapterHelper"> | ||
* BaseRecyclerViewAdapterHelper | ||
* <a/> | ||
*/ | ||
public class CymChadActivity extends AppCompatActivity { | ||
|
||
public static Intent getCallingIntent(Context context) { | ||
return new Intent(context, CymChadActivity.class); | ||
} | ||
|
||
ItemTouchListener mItemTouchListener = new ItemTouchListener() { | ||
@Override | ||
public void onItemClick(String str) { | ||
ToastUtil.show(str); | ||
} | ||
|
||
@Override | ||
public void onLeftMenuClick(String str) { | ||
ToastUtil.show(str); | ||
} | ||
|
||
@Override | ||
public void onRightMenuClick(String str) { | ||
ToastUtil.show(str); | ||
} | ||
}; | ||
|
||
@Override | ||
protected void onCreate(@Nullable Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_swipe_menu_recyclerview); | ||
|
||
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); | ||
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); | ||
recyclerView.setLayoutManager(linearLayoutManager); | ||
DividerItemDecoration dividerItemDecoration = | ||
new DividerItemDecoration(this, DividerItemDecoration.VERTICAL); | ||
recyclerView.addItemDecoration(dividerItemDecoration); | ||
|
||
DemoAdapter demoAdapter = new DemoAdapter(new Repository().fakeDate(), mItemTouchListener); | ||
recyclerView.setAdapter(demoAdapter); | ||
} | ||
|
||
private class DemoAdapter extends BaseMultiItemQuickAdapter<Data, BaseViewHolder> { | ||
|
||
private ItemTouchListener mItemTouchListener; | ||
|
||
DemoAdapter(List<Data> data, ItemTouchListener touchListener) { | ||
super(data); | ||
mItemTouchListener = touchListener; | ||
addItemType(Type.LEFT_MENU, R.layout.item_left_menu); | ||
addItemType(Type.RIGHT_MENU, R.layout.item_right_menu); | ||
addItemType(Type.LEFT_AND_RIGHT_MENU, R.layout.item_left_and_right_menu); | ||
addItemType(Type.LEFT_LONG_MENU, R.layout.item_left_long_menu); | ||
addItemType(Type.RIGHT_LONG_MENU, R.layout.item_right_long_menu); | ||
addItemType(Type.LEFT_AND_RIGHT_LONG_MENU, R.layout.item_left_and_right_long_menu); | ||
addItemType(Type.DISABLE_SWIPE_MENU, R.layout.item_disable_swipe_menu); | ||
} | ||
|
||
@Override | ||
protected void convert(BaseViewHolder helper, Data data) { | ||
|
||
helper.setText(R.id.tv_content, data.content + " " + helper.getAdapterPosition()); | ||
final SwipeItemLayout swipeLayout = (SwipeItemLayout) helper.itemView; | ||
swipeLayout.setSwipeEnable(helper.getItemViewType() != Type.DISABLE_SWIPE_MENU); | ||
|
||
// 不使用helper设置点击事件是因为child点击的时候无法获取到ItemView……,无法关闭菜单 | ||
// 希望BaseRecyclerViewAdapterHelper这个库的作者以后会加上…… | ||
|
||
helper.itemView.setOnClickListener(v -> mItemTouchListener.onItemClick(data.content)); | ||
final View leftMenu = helper.getView(R.id.left_menu); | ||
if (leftMenu != null) { | ||
leftMenu.setOnClickListener(v -> { | ||
mItemTouchListener.onLeftMenuClick("left " + helper.getAdapterPosition()); | ||
swipeLayout.close(); | ||
}); | ||
} | ||
|
||
final View rightMenu = helper.getView(R.id.right_menu); | ||
if (rightMenu != null) { | ||
rightMenu.setOnClickListener(v -> { | ||
mItemTouchListener.onLeftMenuClick("right " + helper.getAdapterPosition()); | ||
swipeLayout.close(); | ||
}); | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
demo/src/main/java/com/aitsuki/swipedemo/ItemTouchListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.aitsuki.swipedemo; | ||
|
||
/** | ||
* Created by AItsuki on 2017/7/11. | ||
* ItemTouchListener | ||
*/ | ||
interface ItemTouchListener { | ||
void onItemClick(String str); | ||
|
||
void onLeftMenuClick(String str); | ||
|
||
void onRightMenuClick(String str); | ||
} |
145 changes: 7 additions & 138 deletions
145
demo/src/main/java/com/aitsuki/swipedemo/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,159 +1,28 @@ | ||
package com.aitsuki.swipedemo; | ||
|
||
import android.content.Intent; | ||
import android.os.Bundle; | ||
import android.support.annotation.LayoutRes; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.support.v7.widget.DividerItemDecoration; | ||
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.TextView; | ||
|
||
import com.aitsuki.swipe.SwipeItemLayout; | ||
import com.aitsuki.swipedemo.data.Repository; | ||
import com.aitsuki.swipedemo.entity.Data; | ||
import com.aitsuki.swipedemo.entity.Type; | ||
import com.aitsuki.swipedemo.util.ToastUtil; | ||
|
||
import java.util.List; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
ToastUtil.init(getApplication()); | ||
|
||
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); | ||
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); | ||
recyclerView.setLayoutManager(linearLayoutManager); | ||
DividerItemDecoration dividerItemDecoration = | ||
new DividerItemDecoration(this, DividerItemDecoration.VERTICAL); | ||
recyclerView.addItemDecoration(dividerItemDecoration); | ||
|
||
DemoAdapter adapter = new DemoAdapter(new Repository().fakeDate(), mItemTouchListener); | ||
recyclerView.setAdapter(adapter); | ||
} | ||
|
||
ItemTouchListener mItemTouchListener = new ItemTouchListener() { | ||
@Override | ||
public void onItemClick(String str) { | ||
ToastUtil.show(str); | ||
} | ||
|
||
@Override | ||
public void onLeftMenuClick(String str) { | ||
ToastUtil.show(str); | ||
} | ||
|
||
@Override | ||
public void onRightMenuClick(String str) { | ||
ToastUtil.show(str); | ||
} | ||
}; | ||
|
||
interface ItemTouchListener { | ||
void onItemClick(String str); | ||
|
||
void onLeftMenuClick(String str); | ||
|
||
void onRightMenuClick(String str); | ||
} | ||
|
||
private static class DemoAdapter extends RecyclerView.Adapter<SimpleViewHolder> { | ||
|
||
private ItemTouchListener mItemTouchListener; | ||
private List<Data> mData; | ||
|
||
DemoAdapter(List<Data> data, ItemTouchListener itemTouchListener) { | ||
this.mData = data; | ||
this.mItemTouchListener = itemTouchListener; | ||
} | ||
|
||
@Override | ||
public int getItemViewType(int position) { | ||
return mData.get(position).type; | ||
} | ||
|
||
|
||
@Override | ||
public int getItemCount() { | ||
return mData.size(); | ||
} | ||
|
||
@Override | ||
public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | ||
|
||
@LayoutRes | ||
int layout; | ||
|
||
switch (viewType) { | ||
case Type.LEFT_MENU: | ||
layout = R.layout.item_left_menu; | ||
break; | ||
case Type.RIGHT_MENU: | ||
layout = R.layout.item_right_menu; | ||
break; | ||
case Type.LEFT_AND_RIGHT_MENU: | ||
layout = R.layout.item_left_and_right_menu; | ||
break; | ||
case Type.LEFT_LONG_MENU: | ||
layout = R.layout.item_left_long_menu; | ||
break; | ||
case Type.RIGHT_LONG_MENU: | ||
layout = R.layout.item_right_long_menu; | ||
break; | ||
case Type.LEFT_AND_RIGHT_LONG_MENU: | ||
layout = R.layout.item_left_and_right_long_menu; | ||
break; | ||
default: | ||
layout = R.layout.item_left_menu; | ||
break; | ||
} | ||
View rootView = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false); | ||
return new SimpleViewHolder(rootView); | ||
} | ||
|
||
@Override | ||
public void onBindViewHolder(final SimpleViewHolder holder, int position) { | ||
holder.mContent.setText(mData.get(position).content.concat(" " + position)); | ||
if (mItemTouchListener != null) { | ||
holder.itemView.setOnClickListener(v -> mItemTouchListener.onItemClick(holder.mContent.getText().toString())); | ||
|
||
if (holder.mLeftMenu != null) { | ||
holder.mLeftMenu.setOnClickListener(v -> { | ||
mItemTouchListener.onLeftMenuClick("left " + holder.getAdapterPosition()); | ||
holder.mSwipeItemLayout.close(); | ||
}); | ||
} | ||
|
||
if (holder.mRightMenu != null) { | ||
holder.mRightMenu.setOnClickListener(v -> { | ||
mItemTouchListener.onRightMenuClick("right " + holder.getAdapterPosition()); | ||
holder.mSwipeItemLayout.close(); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
public void onNativeAdapterClick(View view) { | ||
Intent callingIntent = NativeRecyclerViewAdapterActivity.getCallingIntent(this); | ||
startActivity(callingIntent); | ||
} | ||
|
||
private static class SimpleViewHolder extends RecyclerView.ViewHolder { | ||
|
||
private final View mLeftMenu; | ||
private final View mRightMenu; | ||
private final TextView mContent; | ||
private final SwipeItemLayout mSwipeItemLayout; | ||
|
||
SimpleViewHolder(View itemView) { | ||
super(itemView); | ||
mSwipeItemLayout = (SwipeItemLayout) itemView.findViewById(R.id.swipe_layout); | ||
mContent = (TextView) itemView.findViewById(R.id.tv_content); | ||
mLeftMenu = itemView.findViewById(R.id.left_menu); | ||
mRightMenu = itemView.findViewById(R.id.right_menu); | ||
} | ||
public void onBaseRecyclerViewAdapterHelperClick(View view) { | ||
Intent callingIntent = CymChadActivity.getCallingIntent(this); | ||
startActivity(callingIntent); | ||
} | ||
} |
Oops, something went wrong.