首页的架构以及权限等

本文探讨了首页的架构设计,包括HomeModel、HomePresenter及IView组件,详细阐述了它们在首页逻辑中的作用。同时,提到了权限管理,强调了在首页实现中如何处理用户权限的问题。另外,还涉及到了UI布局,如main.xml、item_product和fragment_home等关键文件的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

<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>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值