搜索商品

1.fragmentone.xml



  
    

    

 

 

2.oneitem




    
    

3.search_top



 

4.Searchmodel

public class SearchModel {
    public void getSearchModel(String keyword,String page,String count){
        ApiService apiService = RetrofitUtils.getInstance().doPost(Api.Search_Url,ApiService.class);
        Flowable search = apiService.getSearch(keyword, page, count);
        search.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber() {
                    @Override
                    public void onNext(SearchBean searchBean) {
                        List result = searchBean.getResult();
                        if(searchModelListener!=null){
                            searchModelListener.onSearchModel(result);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
    public interface onSearchModelListener{
        void onSearchModel(List data);
    }
    onSearchModelListener searchModelListener;

    public void setSearchModelListener(onSearchModelListener searchModelListener) {
        this.searchModelListener = searchModelListener;
    }
}

5.SearchPresenter

public class SearchPresenter {
    private ISearchView iSearchView;
    private final SearchModel searchModel;

    public SearchPresenter(ISearchView iSearchView) {
        this.iSearchView = iSearchView;
        searchModel = new SearchModel();
    }
    public void getPresenter(String keyword,String page,String count){
        searchModel.getSearchModel(keyword, page, count);
        searchModel.setSearchModelListener(new SearchModel.onSearchModelListener() {
            @Override
            public void onSearchModel(List data) {
                iSearchView.getSearchView(data);
            }
        });
    }
}

6.SearchAdapter

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

    public SearchAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public SearchAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.oneitem, viewGroup, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull SearchAdapter.ViewHolder viewHolder, final int i) {
        final AbstractDraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri(list.get(i).getMasterPic())
                .build();
        viewHolder.search_img.setController(controller);
        viewHolder.search_name.setText(list.get(i).getCommodityName());
        viewHolder.search_price.setText(list.get(i).getPrice()+"");
        //点击条目进入商品详情
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, DetailsActivity.class);
                intent.putExtra("id",list.get(i).getCommodityId());
                context.startActivity(intent);
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        private SimpleDraweeView search_img;
        private TextView search_name,search_price;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            search_img=itemView.findViewById(R.id.search_img);
            search_name=itemView.findViewById(R.id.search_name);
            search_price=itemView.findViewById(R.id.search_price);
        }
    }

}

7.FragmentOne

public class FragmentOne extends Fragment implements ISearchView {
    @BindView(R.id.myview)
    MyView myview;
    @BindView(R.id.search_rec)
    RecyclerView searchRec;
    Unbinder unbinder;
  private String keyword;
  private int page=1;
  private int count=20;
    private SearchPresenter searchPresenter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragmentone, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        searchPresenter = new SearchPresenter(this);
        searchPresenter.getPresenter(keyword,page+"",count+"");
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        searchRec.setLayoutManager(gridLayoutManager);
        myview.setViewListener(new MyView.onViewListener() {
            @Override
            public void onView(String s) {
                searchPresenter.getPresenter(s,page+"",count+"");
            }
        });
    }

    @Override
    public void getSearchView(List data) {
        SearchAdapter searchAdapter = new SearchAdapter(getActivity(), data);
       searchRec.setAdapter(searchAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}

8.详情

1.detailsactivity.xml



 
 

        
        
        

    

2.DetailsModel

public class DetailsModel {
    public void getDetailsModel(String commodityId){
        ApiService apiService = RetrofitUtils.getInstance().doPost(Api.Details_Url,ApiService.class);
        Flowable details = apiService.getDetails(commodityId);
        details.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber() {
                    @Override
                    public void onNext(DetailsBean detailsBean) {
                        DetailsBean.ResultBean result = detailsBean.getResult();
                        if(detailsModelListener!=null){
                            detailsModelListener.getDetailsModel(result);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    };
    public interface onDetailsModelListener{
        void getDetailsModel(DetailsBean.ResultBean data);
    }
    onDetailsModelListener detailsModelListener;

    public void setDetailsModelListener(onDetailsModelListener detailsModelListener) {
        this.detailsModelListener = detailsModelListener;
    }
}

3.DetailsPresenter

public class DetailsPresenter {
    private IDetailsView iDetailsView;
    private final DetailsModel detailsModel;

    public DetailsPresenter(IDetailsView iDetailsView) {
        this.iDetailsView = iDetailsView;
        detailsModel = new DetailsModel();
    }
    public void getDetailsPresenter(String commodityId){
        detailsModel.getDetailsModel(commodityId);
        detailsModel.setDetailsModelListener(new DetailsModel.onDetailsModelListener() {
            @Override
            public void getDetailsModel(DetailsBean.ResultBean data) {
                iDetailsView.getDetailsView(data);
            }
        });
    }
}

4.DetailsMactivity

public class DetailsActivity extends AppCompatActivity implements IDetailsView {

    @BindView(R.id.xBanner)
    XBanner xBanner;
    @BindView(R.id.details_price)
    TextView detailsPrice;
    @BindView(R.id.details_name)
    TextView detailsName;
    @BindView(R.id.details_weight)
    TextView detailsWeight;
    @BindView(R.id.webview)
    WebView webview;
    private DetailsPresenter detailsPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        ButterKnife.bind(this);
        detailsPresenter = new DetailsPresenter(this);
        Intent intent = getIntent();
        int id = intent.getIntExtra("id", 0);
        if(id!=0){
            detailsPresenter.getDetailsPresenter(id+"");
        }
    }

    @Override
    public void getDetailsView(DetailsBean.ResultBean data) {
        //xBanner循环轮播
        final ArrayList list = new ArrayList<>();
        String picture = data.getPicture();
        String[] split = picture.split(",");
        for (int i = 0; i < split.length; i++) {
            list.add(split[i]);
        }
        xBanner.setData(list, null);
        xBanner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
                Glide.with(DetailsActivity.this).load(list.get(position)).into((ImageView) view);
            }
        });
        detailsPrice.setText("¥" + data.getPrice() + "");
        detailsName.setText(data.getCommodityName());
        detailsWeight.setText("重量" + " " + data.getWeight() + "");
        WebSettings settings = webview.getSettings();
        //webwiew可以执行 脚本
        settings.setJavaScriptEnabled(true);
        //支持缩放
        settings.setBuiltInZoomControls(true);

        String s2 = "";

        String details = data.getDetails();
        webview.loadDataWithBaseURL(null, details + s2 + "", "text/html", "utf-8", null);
    }
}

5.DetailsView

public interface IDetailsView {
    void getDetailsView(DetailsBean.ResultBean data);
}

 

 

你可能感兴趣的:(搜索商品)