package com.example.shangcheng.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
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.AbsListView;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.shangcheng.R;
import com.example.shangcheng.adapter.LiftAdapter;
import com.example.shangcheng.adapter.RightAdapter;
import com.example.shangcheng.bean.FoodBean;
import com.example.shangcheng.bean.FoodIndexBean;
import java.util.ArrayList;
import java.util.List;
import de.halfbit.pinnedsection.PinnedSectionListView;
/**
* Created by Hello on 2017/2/14.
*/
public class GoodsFragment extends Fragment {
private List<FoodBean> mFoodBeanList = new ArrayList<>();
private ArrayList<FoodIndexBean> mLeftRecData = new ArrayList<>();
private RecyclerView mRecyclerview;
private PinnedSectionListView mRightListview;
private RightAdapter mRightAdapter;
private LiftAdapter mLeftAdapter;
private int mFirstVisibleItem;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.goods_fragment_layout, null);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initView();
Bundle bundle = getArguments();
String id = bundle.getString("id");
AVQuery<AVObject> query = new AVQuery<>("Product_category");
AVObject store = AVObject.createWithoutData("Store", id);
query.whereEqualTo("store", store);
query.include("products");
query.findInBackground(new FindCallback<AVObject>() {
@Override
public void done(List<AVObject> list, AVException e) {
for (AVObject a : list
) {
List<AVObject> products = (List<AVObject>) a.get("products");
FoodIndexBean foodIndexBean = new FoodIndexBean();
String name = (String) a.get("name");
foodIndexBean.indexName = name;
mLeftRecData.add(foodIndexBean);
for (AVObject avObject : products) {
FoodBean foodBean = new FoodBean();
foodBean.foodSort = avObject.getString("category_name");
foodBean.foodName = avObject.getString("name");
String price = avObject.getString("price");
AVFile image = (AVFile) avObject.get("image");
foodBean.img = image;
foodBean.foodPrice = price;
mFoodBeanList.add(foodBean);
}
}
refreshUI(mFoodBeanList, mLeftRecData);
}
/
});
}
private void initView() {
mRecyclerview = (RecyclerView) getView().findViewById(R.id.left_recyview);
mRightListview = (PinnedSectionListView) getView().findViewById(R.id.right_listview);
mRightAdapter = new RightAdapter(getActivity(), mFoodBeanList);
mLeftAdapter = new LiftAdapter(mLeftRecData);
mRecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerview.setAdapter(mLeftAdapter);
mRightListview.setAdapter(mRightAdapter);
mRecyclerview.addOnItemTouchListener(new OnItemClickListener() {
@Override
public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
FoodIndexBean item = mLeftAdapter.getItem(position);
mRightListview.setSelection(item.index);
changeLeftRecSelect(item);
}
});
mRightListview.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (mFoodBeanList == null || mFoodBeanList.size() == 0) return;
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
// LogUtils.d("符合滑动选中条件,第一个可见条目:" + mFirstVisibleItem);
FoodBean foodBean = mFoodBeanList.get(mFirstVisibleItem);
FoodIndexBean foodIndexBean = new FoodIndexBean();
foodIndexBean.indexName = foodBean.foodSort;
changeLeftRecSelect(foodIndexBean);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
mFirstVisibleItem = firstVisibleItem;
}
});
}
private void refreshUI(List<FoodBean> mFoodBeanList, List<FoodIndexBean> mLeftRecData) {
for (int i = 0; i < mLeftRecData.size(); i++) {
FoodIndexBean foodIndexBean = mLeftRecData.get(i);
foodIndexBean.index = i;
}
for (int i = 0; i < mLeftRecData.size(); i++
) {
FoodIndexBean f = mLeftRecData.get(i);
FoodBean foodBean = new FoodBean();
foodBean.pin = f.indexName;
foodBean.foodSort = f.indexName;
for (int j = 0; j < mFoodBeanList.size(); j++) {
FoodBean foodBean1 = mFoodBeanList.get(j);
if (foodBean1.foodSort.equals(foodBean.foodSort)) {
mFoodBeanList.add(j, foodBean);
break;
}
}
}
mLeftAdapter.notifyDataSetChanged();
mRightAdapter.notifyDataSetChanged();
}
private void changeLeftRecSelect(FoodIndexBean bean) {
for (FoodIndexBean bookIndexBean : mLeftRecData) {
bookIndexBean.isSelected = bookIndexBean.equals(bean);
}
mLeftAdapter.notifyDataSetChanged();
}
}
商品左右分类
最新推荐文章于 2023-02-01 19:58:43 发布