京東分類ページの簡単な実現(二)

7670 ワード

昨日は右のスライドを実現しました左のスライドを変更しました今日は左のクリック後右のスライドを指定した位置に移動する場所があります左のクリック傍受関数がどのように状態の変化を全体に通知するかに注意する必要があります実際には左のadapterが1つのインタフェースを登録して全体を実現することができますこのインタフェースを実現して、adapterを初期化するのは1つのコールバックの対象ですつまりC++の中のフック関数ですがJavaは関数をサポートしていません第1型は関数を1つのオブジェクトに包装して、それから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 leftDatas = new ArrayList<>();
    private List  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=0&&movePosition

アダプターはフックオブジェクトを受け取り、傍受実行をクリックすると自分の状態をFragmentに伝える
public class leftAdapter extends RecyclerView.Adapter {
    private List mLeftDatas = new ArrayList<>();
    private Context mContext;
    private GaosuFragment gs;


    public leftAdapter(List 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今日の午後最後に2回目の学校に帰って4年が経ちました.