滑动(二级分类)

public class MainActivity extends AppCompatActivity {
@BindView(R.id.viewPager)
ViewPager viewPager;
@BindView(R.id.radioGroup)
RadioGroup radioGroup;
List list = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);
    list.add(new Fragment1());
    list.add(new Fragment2());
    list.add(new Fragment3());
    viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int i) {
            return list.get(i);
        }
        @Override
        public int getCount() {
            return list.size();
        }
    });
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {
        }
        @Override
        public void onPageSelected(int i) {
            switch (i) {
                case 0:
                    radioGroup.check(R.id.but1);
                    break;
                case 1:
                    radioGroup.check(R.id.but2);
                    break;
                case 2:
                    radioGroup.check(R.id.but3);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int i) {
        }
    });
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.but1:
                    viewPager.setCurrentItem(0);
                    break;
                case R.id.but2:
                    viewPager.setCurrentItem(1);
                    break;
                case R.id.but3:
                    viewPager.setCurrentItem(2);
                    break;
            }
        }
    });
}

}

分类

public class Fragment1 extends Fragment implements MainView {
Button pop_but;
RecyclerView pop_recycler;
MainPresenterIm presenterIm;
FirstAdapter firstAdapter;
RecyclerView recycler1, recycler2;
TwoAdapter twoAdapter;
MainAdapter mainAdapter;
String userId = “”;
String sessionId = “”;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final View inflate = inflater.inflate(R.layout.fragment1, container, false);
    //获取控件Id
    pop_but = inflate.findViewById(R.id.pop_but);
    pop_recycler = inflate.findViewById(R.id.pop_recycler);
    presenterIm = new MainPresenterIm(new MainModelIm(), this);
    //点击弹框
    inflate.findViewById(R.id.pop_but).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            View pop = LayoutInflater.from(getActivity()).inflate(R.layout.pop, null, false);
            recycler1 = pop.findViewById(R.id.recycler1);
            recycler2 = pop.findViewById(R.id.recycler2);
            PopupWindow popupWindow = new PopupWindow(pop, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, false);
            popupWindow.setTouchable(true);
            popupWindow.setBackgroundDrawable(new PaintDrawable());
            popupWindow.setOutsideTouchable(true);
            popupWindow.showAsDropDown(pop_but);

            firstAdapter = new FirstAdapter(getActivity());
            twoAdapter = new TwoAdapter(getActivity());
            mainAdapter = new MainAdapter(getActivity());
            recycler1.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, true));
            recycler1.setAdapter(firstAdapter);

            Map map = new HashMap<>();
            presenterIm.doGet1(0, "small/commodity/v1/findFirstCategory", userId, sessionId, map);
            //一级弹框
            firstAdapter.setItemClickListener(new FirstAdapter.onItemClickListener() {
                @Override
                public void onClick(int id) {
                    Map map = new HashMap<>();
                    map.put("firstCategoryId", id + "");
                    presenterIm.doGet1(1, "small/commodity/v1/findSecondCategory", userId, sessionId, map);
                    recycler2.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, true));
                    recycler2.setAdapter(twoAdapter);

                }
            });
            //二级弹框
            //二级弹框传值
            twoAdapter.setItemClickListener(new TwoAdapter.onItemClickListener() {
                @Override
                public void onClick(int id) {
                    Map map1 = new HashMap<>();
                    map1.put("categoryId", id + "");
                    map1.put("page", 1 + "");
                    map1.put("count", 10 + "");
                    presenterIm.doGet1(2, "small/commodity/v1/findCommodityByCategory", userId, sessionId, map1);
                    pop_recycler.setLayoutManager(new GridLayoutManager(getActivity(), 2));
                    pop_recycler.setAdapter(mainAdapter);
                }
            });
        }
    });
    return inflate;
}

@Override
public void success(int type, String data) {
    if (type == 0) {
        FirstBean firstBean = new Gson().fromJson(data, FirstBean.class);
        List result = firstBean.getResult();
        if (result != null) {
            firstAdapter.setList(result);
        }
    } else if (type == 1) {
        TwoBean twoBean = new Gson().fromJson(data, TwoBean.class);
        List result = twoBean.getResult();
        if (result != null) {
            twoAdapter.setList(result);
        }
    } else {
        MainBean mainBean = new Gson().fromJson(data, MainBean.class);
        List result = mainBean.getResult();
        if (result != null) {
            mainAdapter.setList(result);
        }
    }
}

@Override
public void fail(int type, String error) {

}

}

FirstAdapter

public class FirstAdapter extends RecyclerView.Adapter {
private Context context;
private List list;

public FirstAdapter(Context context) {
    this.context = context;
    list = new ArrayList<>();
}


@NonNull
@Override
public FirstViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View inflate = LayoutInflater.from(context).inflate(R.layout.first_item, viewGroup, false);
    return new FirstViewHolder(inflate);
}

@Override
public void onBindViewHolder(@NonNull FirstViewHolder firstViewHolder, final int i) {
    firstViewHolder.item_title.setText(list.get(i).getName());
    //点击条目传值
    firstViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String id = list.get(i).getId();
            itemClickListener.onClick(Integer.parseInt(id));
        }
    });
}


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

public void setList(List list) {
    this.list = list;
    notifyDataSetChanged();

}

public class FirstViewHolder extends RecyclerView.ViewHolder {
    private TextView item_title;

    public FirstViewHolder(@NonNull View itemView) {
        super(itemView);
        item_title = itemView.findViewById(R.id.item_title);
    }
}

//通过接口回调传值
private onItemClickListener itemClickListener;

public void setItemClickListener(onItemClickListener itemClickListener) {
    this.itemClickListener = itemClickListener;
}

public interface onItemClickListener {
    void onClick(int id);
}

}

TwoAdapter

public class TwoAdapter extends RecyclerView.Adapter {
private Context context;
private List list;

public TwoAdapter(Context context) {
    this.context = context;
    list = new ArrayList<>();
}

@NonNull
@Override
public TwoViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View inflate = LayoutInflater.from(context).inflate(R.layout.first_item, viewGroup, false);
    return new TwoViewHolder(inflate);
}
@Override
public void onBindViewHolder(@NonNull TwoViewHolder twoViewHolder, final int i) {
    twoViewHolder.item_title.setText(list.get(i).getName());
    twoViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String id = list.get(i).getId();
            itemClickListener.onClick(Integer.parseInt(id));
        }
    });
}
@Override
public int getItemCount() {
    return list.size();
}

public void setList(List list) {
    this.list = list;
    notifyDataSetChanged();
}

public class TwoViewHolder extends RecyclerView.ViewHolder {
    private TextView item_title;

    public TwoViewHolder(@NonNull View itemView) {
        super(itemView);
        item_title = itemView.findViewById(R.id.item_title);
    }
}

private onItemClickListener itemClickListener;

public void setItemClickListener(onItemClickListener itemClickListener) {
    this.itemClickListener = itemClickListener;
}

public interface onItemClickListener {
    void onClick(int id);
}

}

MainAdapter

public class MainAdapter extends RecyclerView.Adapter {

private Context context;
private List list;

public MainAdapter(Context context) {
    this.context = context;
    list = new ArrayList<>();
}


@NonNull
@Override
public MainViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View inflate = LayoutInflater.from(context).inflate(R.layout.main_item, viewGroup, false);
    return new MainViewHolder(inflate);
}

@Override
public void onBindViewHolder(@NonNull final MainViewHolder mainViewHolder, final int i) {
    Glide.with(context).load(list.get(i).getMasterPic()).into(mainViewHolder.main_item_image);
    mainViewHolder.main_item_title.setText(list.get(i).getCommodityName());
    mainViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //创建动画
            ValueAnimator objectAnimator = ObjectAnimator.ofFloat(mainViewHolder.itemView, "alpha", 1f, 0f);
            objectAnimator.setDuration(2000);
            objectAnimator.start();
            objectAnimator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                }
                @Override
                public void onAnimationEnd(Animator animation) {//结束的时候
                    //移除
                    list.remove(list.get(i));
                    notifyDataSetChanged();
                    //跳转页面进行eventbus传值
                    Intent intent = new Intent(context, DetailsActivity.class);
                    intent.putExtra("commodityId", list.get(i).getCommodityId());
                    context.startActivity(intent);
                }
                @Override
                public void onAnimationCancel(Animator animation) {
                }
                @Override
                public void onAnimationRepeat(Animator animation) {
                }
            });

        }
    });
}

@Override
public int getItemCount() {
    return list.size();
}
public void setList(List list) {
    this.list = list;
    notifyDataSetChanged();
}

public class MainViewHolder extends RecyclerView.ViewHolder {

    private ImageView main_item_image;
    private TextView main_item_title;

    public MainViewHolder(@NonNull View itemView) {
        super(itemView);
        main_item_image = itemView.findViewById(R.id.main_item_image);
        main_item_title = itemView.findViewById(R.id.main_item_title);
    }
}

}

你可能感兴趣的:(滑动(二级分类))