昨天实现了右边滑动左边跟着改变
今天是 左边点击后右边滑动到指定的位置
有个地方需要注意 左边的点击监听函数 怎么把状态的改变 通知到整体
其实可以左边的adapter 注册一个接口 整体实现这个接口 ,初始化adapter是注册一个 回调对象,即C++里面的钩子函数
不过Java 不支持函数为第一类型 就把函数包装为一个对象 ,然后注入到adapter
具体代码有解释
public class Recycle_doubleFragment extends Fragment implements leftAdapter.GaosuFragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
// 两个recycleView
private RecyclerView leftRecycle;
private RecyclerView rightRecycle;
// 两个Mannager
private LinearLayoutManager leftManager;
private LinearLayoutManager rightManager;
// context
private Context mContext;
//adapter
private leftAdapter ladapter;
private rightAdapter radapter;
// indicator 标识 left的position
private int leftCurrentPosition;
private List<String> leftDatas = new ArrayList<>();
private List<String> rightDatas = new ArrayList<>();
// 要跳转的 目标是否在最后一个可见view 之后
private boolean mSholudScrool;
// 记录目标项的位置
private int ToPosition;
public Recycle_doubleFragment() {
// Required empty public constructor
}
public static Recycle_doubleFragment newInstance(String param1, String param2) {
Recycle_doubleFragment fragment = new Recycle_doubleFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_recycle_double, container, false);
getData();
leftRecycle = view.findViewById(R.id.left_recycle);
rightRecycle = view.findViewById(R.id.right_recycle);
leftManager = new LinearLayoutManager(mContext);
// 这里就是注入钩子对象
ladapter = new leftAdapter(leftDatas,mContext,this);
leftRecycle.setLayoutManager(leftManager);
leftRecycle.setAdapter(ladapter);
radapter = new rightAdapter(rightDatas,mContext);
rightManager = new LinearLayoutManager(mContext);
rightRecycle.setLayoutManager(rightManager);
rightRecycle.setAdapter(radapter);
rightRecycle.addOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if(newState ==RecyclerView.SCROLL_STATE_DRAGGING&& mSholudScrool){
mSholudScrool = false;
recyclerView.smoothScrollToPosition(ToPosition);
}
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int firstCompletelyPosition = rightManager.findFirstCompletelyVisibleItemPosition();
if (firstCompletelyPosition!=-1){
changeLeftPosition(firstCompletelyPosition);
}
}
});
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.mContext = context;
}
@Override
public void onDetach() {
super.onDetach();
}
void getData(){
for (int i = 1; i < 10; i++) {
leftDatas.add("我是第"+i);
}
for (int i = 10; i <100 ; i++) {
rightDatas.add("我是第"+i);
}
}
// 改变 left的位置
void changeLeftPosition(int newPosition){
// 如果没有变化
if(newPosition==leftCurrentPosition){
// nothing to do
}else {
View newView = leftManager.findViewByPosition(newPosition);
View oidView = leftManager.findViewByPosition(leftCurrentPosition);
if (newView!=null){
// 将新的颜色改变
newView.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
// 上一个view的颜色回复原来的颜色
oidView.setBackgroundColor(mContext.getResources().getColor(R.color.colorAccent));
}
// 更新 当前的position
leftCurrentPosition = newPosition;
}
}
// 点击左边的 view 右边的view自动滚动到顶部
// 如果跳转的位置 在第一个可见位置之前 smoothScrollToPosition() 可以直接跳转
// 如果跳转的位置在第一个可见项之前 ,最后一个可见项之后 ,就是这个view就在屏幕上,那么就不会跳转,需要smmothSroolBy
// 如果跳转的位置 在最后一个可见项之后 那么 smoothScroolToPositon 将要跳转的位置滚动到可见位置 ,调用smoothMovetoPOsitioon
void scrollToPosition(final int position){
int firstPosition = rightManager.findFirstVisibleItemPosition();
int lastPosition = rightManager.findLastVisibleItemPosition();
if (position<firstPosition) {
rightRecycle.smoothScrollToPosition(position);
}else if(position<=lastPosition){
// 第二种可呢能
int movePosition = position - firstPosition;
if(movePosition>=0&&movePosition<rightRecycle.getChildCount()){
int top = rightRecycle.getChildAt(movePosition).getTop();
rightRecycle.smoothScrollBy(0,top);
}
}else {
rightRecycle.smoothScrollToPosition(position);
ToPosition = position;
mSholudScrool = true;
}
}
@Override
public void setCurrentPosition(int currentPosition) {
// 改变当前的状态
this.leftCurrentPosition = currentPosition;
// 改变当前的颜色
changeLeftPosition(currentPosition);
// 右边需要滚动到相应的位置
scrollToPosition(currentPosition);
}
}
adaper 接受钩子对象 并在 点击监听执行时把自己的状态告诉Fragment
public class leftAdapter extends RecyclerView.Adapter<leftAdapter.leftHolder> {
private List<String> mLeftDatas = new ArrayList<>();
private Context mContext;
private GaosuFragment gs;
public leftAdapter(List<String> mLeftDatas, Context mContext,GaosuFragment gs) {
this.mLeftDatas = mLeftDatas;
this.mContext = mContext;
this.gs = gs;
}
@NonNull
@Override
public leftHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.left_layout,viewGroup,false);
return new leftHolder(view);
}
@Override
public void onBindViewHolder(@NonNull leftHolder lHolder, int i) {
lHolder.category.setText(mLeftDatas.get(i));
lHolder.itemView.setOnClickListener(leftListener);
lHolder.itemView.setTag(i);
}
@Override
public int getItemCount() {
return mLeftDatas.size();
}
static class leftHolder extends RecyclerView.ViewHolder{
public TextView category;
public leftHolder(@NonNull View itemView) {
super(itemView);
category = itemView.findViewById(R.id.category);
}
}
// 就在这里设置事件
View.OnClickListener leftListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
gs.setCurrentPosition((int) v.getTag());
}
};
public interface GaosuFragment{
void setCurrentPosition(int currentPosition);
}
}
ok
今天下午最后第二次回学校 好不舍 一晃四年过去了