4)Learning diary for Flutter about android

Today I'll give data from service to the ListView.

Before that,the data was local.

To practice mixed development,I'll use native code to complete data requests 

public class HomeActivity extends Activity {
    FlutterEngine flutterEngine;
    GetJsonPlugin getJsonPlugin;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (flutterEngine == null) {
            flutterEngine = new FlutterEngine(this, null);
            flutterEngine.getDartExecutor().executeDartEntrypoint(
                    DartExecutor.DartEntrypoint.createDefault()
            );
        }
        FlutterView flutterView = findViewById(R.id.fv);
        flutterView.attachToFlutterEngine(flutterEngine);
        getJsonPlugin = new GetJsonPlugin();
        getJsonPlugin.initCannel(flutterEngine.getDartExecutor(), (call, result) -> {
            Log.e("執行方法", call.method);
            if (call.method.equals(GetJsonPlugin.methodGetJson)) {
                Log.e("執行方法", call.method + call.argument("page"));
                String page = call.argument("page");
                HashMap<String, String> params = new HashMap<>();
                params.put("page", page);
                new CommonHttp(new CommonHttpCallback() {
                    @Override
                    public void requestSeccess(String json) {
                        Log.e("myjson", json);
                        List arrayList = JSON.parseArray(json);
                        result.success(arrayList);
                    }

                    @Override
                    public void requestFail(String msg) {

                    }

                    @Override
                    public void requestAbnormal(int code) {
                        Log.e("myjson", code + "");
                    }
                }).doRequest("http://192.168.1.171:8080/mytest/test", params);
            }
        });
    }
}
const MethodChannel methodChannel=MethodChannel("MyChannel");
Future<List<MyListData>> getData() async{
  List<Map<dynamic, dynamic>> list=await methodChannel.invokeListMethod("getjson",{"page":"1"});
  List<MyListData> data=[];
    assert(data!=null);
    for(int i=0;i<list.length;i++){
      MyListData myListData=new MyListData(list[i]["title"], list[i]["content"]);
      data.add(myListData);
      print(data[i]);
    }
    return data;
}
Future<List<MyListData>> addData(num page) async{
  List<Map<dynamic, dynamic>> list=await methodChannel.invokeListMethod("getjson",{"page":"$page"});
  List<MyListData> data=[];
  assert(data!=null);
  for(int i=0;i<list.length;i++){
    MyListData myListData=new MyListData(list[i]["title"], list[i]["content"]);
    data.add(myListData);
    print(data[i]);
  }
  return data;
}
class MySecondPageState extends State<MySecondPage> implements ScrollListener {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    widgetList = [];
    List<MyListData> list = [];
    adapter = new MyLoadMoreListAdater.name(list, this);
    getData().then((List<MyListData> list) {
      print("異步執行中");
      print(list.length);
      adapter.data.addAll(list);
    }).whenComplete(() {
      setState(() {
        print("異步執行完畢");
        for (int i = 0; i < adapter.data.length; i++) {
          widgetList.add(adapter.getItem(context, i));
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("second page"),
        ),
        body: getLoadMoreList(context));
  }

  MyLoadMoreListAdater adapter;
  List<Widget> widgetList;
  num page=1;

  getLoadMoreList(BuildContext context) {
//    return ListView(children: widgetList, controller: adapter.getController());
    return ListView.builder(
        itemCount: widgetList.length,
        controller: adapter.getController(),
        itemBuilder: (BuildContext context, int pos) {
          return adapter.getItem(context, pos);
        });
  }

  @override
  void onLoaderMore(MyBaseAdapter data) {
    // TODO: implement onLoaderMore
    print("加載更多");
    List<MyListData> addItem;
    addData(++page).then((List<MyListData> list){
      addItem=list;
      adapter.data.addAll(list);
    }).whenComplete((){
      int temp = widgetList.length;
      setState(() {
        for (int i = temp; i < temp + addItem.length; i++) {
          widgetList.add(adapter.getItem(context, i));
        }
      });
    });
//    setState(() {
//      widgetList = adapter.getItemList(context);
//      print(temp);
//    });
    print("加載end");
  }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章