Newer
Older
DownApp / app / src / main / java / me / bell / downapp / activity / MainActivity.java
package me.bell.downapp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;

import me.bell.downapp.R;
import me.bell.downapp.adapter.CateAdapter;
import me.bell.downapp.bean.CateBean;
import me.bell.downapp.bean.CateData;
import me.bell.downapp.presenter.HttpPresenter;
import me.bell.downapp.presenter.IHttpView;
import me.bell.downapp.view.ScrollGridView;

public class MainActivity extends AppCompatActivity implements IHttpView, View.OnClickListener {
    private Context mContext;


    private EditText keyEdit;
    private Button searchBtn;
    private ScrollGridView appGridView, gameGridView;

    private HttpPresenter presenter;

    private String AppCateUrl = "http://mapp.qzone.qq" + "" +
            ".com/cgi-bin/mapp/mapp_catelist_qq?type=app&platform=touch&network_type=unknown&resolution=360x640";

    private String GameCateUrl = "http://mapp.qzone.qq" + "" +
            ".com/cgi-bin/mapp/mapp_catelist_qq?type=game&platform=touch&network_type=unknown&resolution=360x640";

    private ArrayList<CateData> appLists = new ArrayList<>();
    private ArrayList<CateData> gameLists = new ArrayList<>();
    private CateAdapter adapterApp, adapterGame;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = MainActivity.this;

        initUI();

        presenter = new HttpPresenter(this);
        presenter.getCate(AppCateUrl, "app");
        presenter.getCate(GameCateUrl, "game");




    }

    private void initUI() {
        keyEdit = findViewById(R.id.search_edit);
        keyEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_DONE){
                    doSearch();
                }
                return true;
            }
        });
        searchBtn = findViewById(R.id.search_btn);
        searchBtn.setOnClickListener(this);

        appGridView = findViewById(R.id.app_grid_view);
        gameGridView = findViewById(R.id.game_grid_view);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //搜索
            case R.id.search_btn:
                doSearch();
                break;

        }
    }

    /**
     * 搜索跳转
     */
    private void doSearch(){
        String key = keyEdit.getText().toString().trim();
        if (TextUtils.isEmpty(key)){
            Toast.makeText(mContext, "请输入关键词", Toast.LENGTH_SHORT).show();
        } else {
            Intent intent = new Intent(mContext, SearchActivity.class);
            intent.putExtra("keyword", key);
            mContext.startActivity(intent);
        }
    }

    @Override
    public void onHttpResult(String action, String httpResult) {
        if (!"error".equals(httpResult) && httpResult.endsWith(";")) {
            //获取返回字符串
            httpResult = httpResult.substring(0, httpResult.length() - 1);
            CateBean cate = JSONObject.parseObject(httpResult, CateBean.class);
            if (cate.getCode() != 0) {
                //应用
                if ("app".equals(action)){
                    presenter.getCate(AppCateUrl, "app");
                    //游戏
                } else if ("game".equals(action)){
                    presenter.getCate(GameCateUrl, "game");
                }
            } else {
                //应用
                if ("app".equals(action)){
                    Log.e("cate", cate.getCateList().get(0).getCategoryName());
                    appLists = cate.getCateList();
                    mHandler.sendEmptyMessage(1);
                    //游戏
                } else if ("game".equals(action)){
                    Log.e("game", cate.getCateList().get(0).getCategoryName());
                    gameLists = cate.getCateList();
                    mHandler.sendEmptyMessage(2);
                }
            }

        } else {
            //应用
            if ("app".equals(action)){
                presenter.getCate(AppCateUrl, "app");
                //游戏
            } else if ("game".equals(action)){
                presenter.getCate(GameCateUrl, "game");
            }
        }
    }


    MHandler mHandler = new MHandler();
    class MHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    adapterApp = new CateAdapter(mContext, appLists);
                    appGridView.setAdapter(adapterApp);
                    break;

                case 2:
                    adapterGame = new CateAdapter(mContext, gameLists);
                    gameGridView.setAdapter(adapterGame);
                    break;
            }
        }
    }

}