Day11_Fragment&ViewPager_作业

作业

一、MainActivity的代码

(1) 布局xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/view_page"
        android:layout_above="@id/layout_xx"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>

    <TextView
        android:visibility="gone"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:background="#ff0"
        android:textColor="#000"
        android:textSize="20sp"
        android:id="@+id/text_time"
        android:text="倒计时:"
        android:layout_width="125dp"
        android:layout_height="55dp" />

    <View
        android:id="@+id/view_1"
        android:layout_centerInParent="true"
        android:layout_width="0dp"
        android:layout_height="0dp"/>

    <Button
        android:visibility="gone"
        android:background="#12E0AF"
        android:id="@+id/button_time"
        android:layout_below="@id/view_1"
        android:layout_centerInParent="true"
        android:layout_marginTop="150dp"
        android:text="立即体验"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/layout_xx"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <RadioGroup
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <RadioButton
                android:textColor="#000"
                android:textSize="20sp"
                android:text="一"
                android:gravity="center"
                android:button="@null"
                android:layout_weight="1"
                android:id="@+id/radio_one"
                android:layout_width="0dp"
                android:layout_height="match_parent" />

            <RadioButton
                android:textColor="#000"
                android:textSize="20sp"
                android:text="二"
                android:gravity="center"
                android:button="@null"
                android:id="@+id/radio_two"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent" />

        </RadioGroup>

    </LinearLayout>

</RelativeLayout>
(2) Activity代码
public class MainActivity extends AppCompatActivity {

    //下载地址
    private String path = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1";
    //寻找控件
    private ViewPager viewPager;
    private Button button_time;
    private TextView textView_time;
    //数据源
    private List<BlankFragment> list = new ArrayList<>();
    private List<JavaBean.DataBean> dataList = new ArrayList<>();
    //计数专用
    private int index = 0;
    private int sendTime = 5;
    //fragment管理者
    private FragmentManager manager;
    //两个计时器
    private Timer timer1;
    private Timer timer;
    //主线程
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    viewPager.setCurrentItem(index);
                    ++index;
                    if(index == list.size()){
                        index = 0;
                    }

                    break;

                case 2:
                    //给textView赋值
                    textView_time.setText("倒计时:"+sendTime+"秒");
                    //秒数倒计时
                    sendTime --;
                    //秒数为-1时跳转
                    if(sendTime == -1){
                        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                        startActivity(intent);
                        timer1.cancel();
                        finish();
                    }
                    break;
            }

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        initData();

    }

    private void initData() {
        //异步任务的执行
        new MyAsyncTask(dataList,list,viewPager,manager).execute(path);
    }

    private void initView() {
        //控件赋值
        manager = getSupportFragmentManager();
        viewPager = findViewById(R.id.view_page);
        textView_time = findViewById(R.id.text_time);
        button_time = findViewById(R.id.button_time);

        //时间器
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(1);
            }
        },0,1000);

        //viewPager的监听器
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            //监听
            @Override
            public void onPageSelected(int i) {
                if(i == list.size()-1){
                    //寻找main.xml中的布局id
                    textView_time.setVisibility(View.VISIBLE);
                    button_time.setVisibility(View.VISIBLE);
                    //执行到这步的时候将timer取消执行
                    timer.cancel();
                    //timer1执行,用于倒计时跳转
                    timer1 = new Timer();
                    timer1.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            handler.sendEmptyMessage(2);
                        }
                    },0,1000);


                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        //点击直接跳转
        button_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);
                timer1.cancel();
                finish();
            }
        });
    }
}

二、异步任务中(很重要)

public class MyAsyncTask extends AsyncTask<String, Void, List<JavaBean.DataBean>> {

    private List<JavaBean.DataBean> totalList;//字符串以及图片数据源
    private List<BlankFragment> list;//碎片数据源
    private ViewPager viewPager;//viewPager的视图
    private FragmentManager manager;//管理者

    public MyAsyncTask(List<JavaBean.DataBean> totalList, List<BlankFragment> list, ViewPager viewPager, FragmentManager manager) {
        this.totalList = totalList;
        this.list = list;
        this.viewPager = viewPager;
        this.manager = manager;
    }

    @Override
    protected List<JavaBean.DataBean> doInBackground(String... strings) {

        //通过HttpUtils获取网络数据
        List<JavaBean.DataBean> dataBeans = HttpUtils.LoadString(strings[0]);

        if (dataBeans!= null && dataBeans.size() > 0) {
            //返回到post中
            return dataBeans;
        }
        
        return null;
    }

    @Override
    protected void onPostExecute(List<JavaBean.DataBean> dataBeans) {

        if(dataBeans != null && dataBeans.size() > 0){
            totalList.addAll(dataBeans);
            
            for (int i = 0; i < 4; i++) {
                //为碎片用bundle传值
                BlankFragment fragment = new BlankFragment();
                Bundle bundle = new Bundle();
                bundle.putString("title",totalList.get(i).getTitle());
                bundle.putString("icon",totalList.get(i).getPic());
//                        bundle.putString("title","我是"+i+"个");
                fragment.setArguments(bundle);

                list.add(fragment);
            }

            //viewPager设置适配器
            viewPager.setAdapter(new FragmentStatePagerAdapter(manager) {
                //碎片
                @Override
                public Fragment getItem(int i) {
                    return list.get(i);
                }
                //视图个数
                @Override
                public int getCount() {
                    return list.size();
                }
            });
        }

    }
}

三、碎片中

(1) 具体功能代码
public class BlankFragment extends Fragment {

    private TextView textView_title;
    private ImageView imageView_pic;

    public BlankFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View inflate = inflater.inflate(R.layout.fragment_blank, container, false);

        textView_title = inflate.findViewById(R.id.text_title);
        imageView_pic = inflate.findViewById(R.id.iamge_pic);

        Bundle bundle = getArguments();

        String title = bundle.getString("title");
        String icon = bundle.getString("icon");

        textView_title.setText(title);
        Glide.with(getActivity()).load(icon).into(imageView_pic);
        return inflate;
    }
}
(2) 碎片布局页面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment.BlankFragment">

    <!-- TODO: Update blank fragment layout -->

    <TextView
        android:id="@+id/text_title"
        android:layout_marginTop="100dp"
        android:layout_centerHorizontal="true"
        android:textSize="25sp"
        android:textColor="#000"
        android:text="我是标题"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/iamge_pic"
        android:layout_marginTop="100dp"
        android:layout_centerInParent="true"
        android:layout_below="@id/text_title"
        android:scaleType="fitXY"
        android:src="@mipmap/ic_launcher"
        android:layout_width="175dp"
        android:layout_height="175dp" />

</RelativeLayout>

四、其他功能类

(1) HttpUtils

package com.example.day_06_07_homework.enity;

import android.util.Log;

import com.example.day_06_07_homework.MyBean.JavaBean;
import com.google.gson.Gson;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class HttpUtils {


    public static List<JavaBean.DataBean> LoadString(String s){

        InputStream is = null;
        ByteArrayOutputStream bos = null;
        HttpURLConnection connection;
        try {
            URL url = new URL(s);
            connection = (HttpURLConnection) url.openConnection();

            Log.i("TAG", "doInBackground: ->"+connection.getResponseCode());
            if(connection.getResponseCode() == 200){
                is = connection.getInputStream();
                bos = new ByteArrayOutputStream();
                int len = 0;
                byte[] b = new byte[1024];
                while((len = is.read(b)) != -1){
                    bos.write(b, 0, len);
                }
                JavaBean javaBean = new Gson().fromJson(bos.toString(), JavaBean.class);
                return javaBean.getData();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

(2) JavaBean类
package com.example.day_06_07_homework.MyBean;

import java.util.List;

public class JavaBean {

    /**
     * ret : 1
     * data : [{"id":"8289","title":"油焖大虾","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg","collect_num":"1669","food_str":"大虾 葱 生姜 植物油 料酒","num":1669},{"id":"2127","title":"四川回锅肉","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2127.jpg","collect_num":"1591","food_str":"猪肉 青蒜 青椒 红椒 姜片","num":1591},{"id":"30630","title":"超简单芒果布丁","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/31/30630.jpg","collect_num":"1544","food_str":"QQ糖 牛奶 芒果","num":1544},{"id":"9073","title":"家常红烧鱼","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9073.jpg","collect_num":"1425","food_str":"鲜鱼 姜 葱 蒜 花椒","num":1425},{"id":"10097","title":"家常煎豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10097.jpg","collect_num":"1419","food_str":"豆腐 新鲜红椒 青椒 葱花 油","num":1419},{"id":"10509","title":"水煮肉片","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10509.jpg","collect_num":"1341","food_str":"瘦猪肉 生菜 豆瓣酱 干辣椒 花椒","num":1341},{"id":"46968","title":"红糖苹果银耳汤","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/47/46968.jpg","collect_num":"1252","food_str":"银耳 苹果 红糖","num":1252},{"id":"10191","title":"麻婆豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10191.jpg","collect_num":"1221","food_str":"豆腐 肉末 生抽 白糖 芝麻油","num":1221},{"id":"2372","title":"皮蛋瘦肉粥","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2372.jpg","collect_num":"1151","food_str":"大米 皮蛋 猪肉 油条 香葱","num":1151},{"id":"2166","title":"蚂蚁上树","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2166.jpg","collect_num":"1144","food_str":"红薯粉 肉 姜 蒜 花椒","num":1144}]
     */

    private int ret;
    private List<DataBean> data;

    @Override
    public String toString() {
        return "JavaBean{" +
                "ret=" + ret +
                ", data=" + data +
                '}';
    }

    public int getRet() {
        return ret;
    }

    public void setRet(int ret) {
        this.ret = ret;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * id : 8289
         * title : 油焖大虾
         * pic : http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg
         * collect_num : 1669
         * food_str : 大虾 葱 生姜 植物油 料酒
         * num : 1669
         */

        private String id;
        private String title;
        private String pic;
        private String collect_num;
        private String food_str;
        private int num;

        @Override
        public String toString() {
            return "DataBean{" +
                    "id='" + id + '\'' +
                    ", title='" + title + '\'' +
                    ", pic='" + pic + '\'' +
                    ", collect_num='" + collect_num + '\'' +
                    ", food_str='" + food_str + '\'' +
                    ", num=" + num +
                    '}';
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getPic() {
            return pic;
        }

        public void setPic(String pic) {
            this.pic = pic;
        }

        public String getCollect_num() {
            return collect_num;
        }

        public void setCollect_num(String collect_num) {
            this.collect_num = collect_num;
        }

        public String getFood_str() {
            return food_str;
        }

        public void setFood_str(String food_str) {
            this.food_str = food_str;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }
    }
}

第二个Activity什么都没有 直接跳就OK了
本资源集提供了针对小型无人机六自由度非线性动力学模型的MATLAB仿真环境,适用于多个版本(如2014a、2019b、2024b)。该模型完整描述了飞行器在三维空间中的六个独立运动状态:绕三个坐标轴的旋转(滚转、俯仰、偏航)与沿三个坐标轴的平移(前后、左右、升降)。建模过程严格依据牛顿-欧拉方程,综合考虑了重力、气动力、推进力及其产生的力矩对机体运动的影响,涉及矢量运算与常微分方程求解等数学方法。 代码采用模块化与参数化设计,使用者可便捷地调整飞行器的结构参数(包括几何尺寸、质量特性、惯性张量等)以匹配不同机型。程序结构清晰,关键步骤配有详细说明,便于理解模型构建逻辑与仿真流程。随附的示例数据集可直接加载运行,用户可通过修改参数观察飞行状态的动态响应,从而深化对无人机非线性动力学特性的认识。 本材料主要面向具备一定数学与编程基础的高校学生,尤其适合计算机、电子信息工程、自动化及相关专业人员在课程项目、专题研究或毕业设计中使用。通过该仿真环境,学习者能够将理论知识与数值实践相结合,掌握无人机系统建模、仿真与分析的基本技能,为后续从事飞行器控制、系统仿真等领域的研究或开发工作奠定基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值