简单实现京东分类页面

很多app都会有这种页面
简单实现京东分类页面_第1张图片
这种页面该怎么实现 我看了好多网友的实现方法 无一例外 都是DoubleLIstView,作为一个新时代的年轻人,我决定告别ListVIew,使用RecycleView来实现

package Fragments;

import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.LiveData;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnScrollChangeListener;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import adapters.leftAdapter;
import adapters.rightAdapter;
import club.ijava.lixin03.R;

public class Recycle_doubleFragment extends Fragment {

    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;
    private List leftDatas = new ArrayList<>();
    private List  rightDatas = new ArrayList<>();
    private OnFragmentInteractionListener mListener;

    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);
        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() {
        // 最重要的是这里 这里添加一个监听器 ,找到第一个完成的屏幕上显示的item,获取他的position
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int firstCompletelyPosition = rightManager.findFirstCompletelyVisibleItemPosition();
                Log.i("position",firstCompletelyPosition+"scroll");
            }
        });
        return view;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }
    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(Uri uri);
    }

    void getData(){
        for (int i = 1; i < 10; i++) {
            leftDatas.add("我是第"+i);
        }
        for (int i = 10; i <100 ; i++) {
            rightDatas.add("我是第"+i);
        }
    }
}


import club.ijava.lixin03.R;

public class rightAdapter extends RecyclerView.Adapter {
    private List mRightDatas = new ArrayList<>();
    private Context mContext;

    public rightAdapter(List mLeftDatas, Context mContext) {
        this.mRightDatas = mLeftDatas;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public rightAdapter.rightHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View  view =   LayoutInflater.from(mContext).inflate(R.layout.right_layout,viewGroup,false);
        return new rightHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull rightAdapter.rightHolder rHolder, int i) {
        rHolder.goods.setText(mRightDatas.get(i));
        rHolder.itemView.setTag(i);
    }

    @Override
    public int getItemCount() {
        return mRightDatas.size();
    }

    static class rightHolder extends RecyclerView.ViewHolder{
        private TextView goods;
        public rightHolder(@NonNull View itemView) {
            super(itemView);
            goods = itemView.findViewById(R.id.goods);
        }
    }
}

简单实现京东分类页面_第2张图片
效果图
简单实现京东分类页面_第3张图片
拿到他的位置信息 想对他干啥还不容易吗
哈哈

你可能感兴趣的:(android)