1.fragmentone.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <com.example.yuemoni2.MyView android:id="@+id/myview" android:layout_width="match_parent" android:layout_height="wrap_content" ></com.example.yuemoni2.MyView> <android.support.v7.widget.RecyclerView android:id="@+id/search_rec" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout>
2.oneitem
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/search_img" android:layout_width="120dp" android:layout_height="120dp" /> <TextView android:id="@+id/search_name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/search_price" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
3.search_top
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/search_back" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="返回" /> <EditText android:id="@+id/search_edit" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <Button android:id="@+id/search_btn" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="搜索" /> </LinearLayout>
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<SearchBean> search = apiService.getSearch(keyword, page, count); search.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableSubscriber<SearchBean>() { @Override public void onNext(SearchBean searchBean) { List<SearchBean.ResultBean> 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<SearchBean.ResultBean> 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<SearchBean.ResultBean> data) { iSearchView.getSearchView(data); } }); } }
6.SearchAdapter
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> { private Context context; private List<SearchBean.ResultBean> list; public SearchAdapter(Context context, List<SearchBean.ResultBean> 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<SearchBean.ResultBean> data) { SearchAdapter searchAdapter = new SearchAdapter(getActivity(), data); searchRec.setAdapter(searchAdapter); } @Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); } }
8.详情
1.detailsactivity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".DetailsActivity" android:orientation="vertical" > <com.stx.xhb.xbanner.XBanner android:id="@+id/xBanner" android:layout_width="match_parent" android:layout_height="200dp"> </com.stx.xhb.xbanner.XBanner> <TextView android:id="@+id/details_price" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/details_name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/details_weight" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" ></WebView> </LinearLayout>
2.DetailsModel
public class DetailsModel { public void getDetailsModel(String commodityId){ ApiService apiService = RetrofitUtils.getInstance().doPost(Api.Details_Url,ApiService.class); Flowable<DetailsBean> details = apiService.getDetails(commodityId); details.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableSubscriber<DetailsBean>() { @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<String> 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 = "<script type=\"text/javascript\">" + "var imgs=document.getElementsByTagName('img');" + "for(var i = 0; i<imgs.length; i++){" + "imgs[i].style.width='100%';" + "imgs[i].style.height='auto';" + "}" + "</script>"; String details = data.getDetails(); webview.loadDataWithBaseURL(null, details + s2 + "<html><body>", "text/html", "utf-8", null); } }
5.DetailsView
public interface IDetailsView { void getDetailsView(DetailsBean.ResultBean data); }