首页的架构以及权限等

xiaoxiao2025-11-09  4

 

<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"/> implementation 'com.google.code.gson:gson:2.8.5' implementation 'com.squareup.okhttp3:okhttp:3.11.0' implementation 'com.github.bumptech.glide:glide:4.8.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0' implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0' androidTestImplementation 'com.android.support.test:runner:1.0.2' implementation 'com.android.support:recyclerview-v7:27.1.1'

 

//model

//HomeModel 

public class HomeModel { public void getData(String url, INetCallBack callBack, Type type) { HttpUtils.getInstance().get(url, callBack, type); } }

//presenter

//HomePresenter

public class HomePresenter { private IView iv; private HomeModel model; public void attach(IView iv) { this.iv = iv; model = new HomeModel(); } public void getBanner() { String url = "http://www.zhaoapi.cn/ad/getAd"; Type type = new TypeToken<MessageBean<List<Banner>>>() { }.getType(); model.getData(url, new INetCallBack() { @Override public void success(Object obj) { MessageBean<List<Banner>> data = (MessageBean<List<Banner>>) obj; iv.getBanner(data); } @Override public void failed(Exception e) { iv.failed(e); } }, type); } public void getProduct(){ String url = "http://www.zhaoapi.cn/product/getCarts?uid=71"; Type type = new TypeToken<MessageBean<List<Shopper>>>() { }.getType(); model.getData(url, new INetCallBack() { @Override public void success(Object obj) { MessageBean<List<Shopper>> data = (MessageBean<List<Shopper>>) obj; iv.getProduct(data); } @Override public void failed(Exception e) { iv.failed(e); } }, type); } public void detach() { if (iv != null) { iv = null; } } }

//IVew

public interface IView { void failed(Exception e); void getBanner(MessageBean<List<Banner>> banners); void getProduct(MessageBean<List<Shopper>> products); }

 

//HomeFragment

public class HomeFragment extends Fragment implements IView { private ViewPager vpBanner; private RecyclerView rvProduct; private List<Banner> bannerList; private BannerAdapter bannerAdapter; private List<Product> productList; private ProductAdapter productAdapter; private HomePresenter presenter; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_home, container, false); vpBanner = v.findViewById(R.id.vp_banner); rvProduct = v.findViewById(R.id.rv_product); return v; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); bannerList = new ArrayList<>(); bannerAdapter = new BannerAdapter(getActivity(), bannerList); vpBanner.setAdapter(bannerAdapter); productList = new ArrayList<>(); RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); rvProduct.setLayoutManager(layoutManager); productAdapter = new ProductAdapter(getActivity(), productList); rvProduct.setAdapter(productAdapter); presenter = new HomePresenter(); presenter.attach(this); presenter.getBanner(); presenter.getProduct(); } @Override public void failed(Exception e) { Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show(); } @Override public void getBanner(MessageBean<List<Banner>> banners) { if (banners != null) { List<Banner> data = banners.getData(); if (data != null) { bannerList.clear(); bannerList.addAll(data); bannerAdapter.notifyDataSetChanged(); } } } @Override public void getProduct(MessageBean<List<Shopper>> products) { List<Shopper> shoppers = products.getData(); if (shoppers != null) { productList.clear(); // 遍历商家的所有商品 for (Shopper shopper : shoppers) { List<Product> list = shopper.getList(); if (list != null) { productList.addAll(list); } } productAdapter.notifyDataSetChanged(); } } @Override public void onDestroy() { super.onDestroy(); if (presenter != null) { presenter.detach(); } } }

main

public class MainActivity extends AppCompatActivity implements View.OnClickListener { private HomeFragment hf; private MineFragment mf; private TextView txtHome; private TextView txtMine; private FragmentManager manager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtHome = findViewById(R.id.txt_home); txtMine = findViewById(R.id.txt_mine); hf = new HomeFragment(); mf = new MineFragment(); manager = getSupportFragmentManager(); manager.beginTransaction().add(R.id.content, hf) .add(R.id.content, mf) .hide(mf) .commit(); txtHome.setOnClickListener(this); txtMine.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.txt_home: manager.beginTransaction() .show(hf) .hide(mf) .commit(); break; case R.id.txt_mine: manager.beginTransaction().hide(hf) .show(mf) .commit(); break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data); } }

//xml

//item_product

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/img_product" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/txt_title" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>

//fragment_home

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bg_search" android:orientation="horizontal"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:src="@mipmap/ic_launcher" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" /> </LinearLayout> <android.support.v4.view.ViewPager android:id="@+id/vp_banner" android:layout_width="match_parent" android:layout_height="150dp"></android.support.v4.view.ViewPager> <android.support.v7.widget.RecyclerView android:id="@+id/rv_product" android:layout_width="match_parent" android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView> </LinearLayout>

//main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/ll_bottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" android:padding="10dp"> <TextView android:id="@+id/txt_home" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="首页" /> <TextView android:id="@+id/txt_mine" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="我的" /> </LinearLayout> <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/ll_bottom"></FrameLayout> </RelativeLayout>

 

 

 

转载请注明原文地址: https://www.6miu.com/read-5039326.html

最新回复(0)