Android_网络获取+动画

本文探讨了在Android应用中如何实现网络数据获取,并结合实例讲解了如何将获取的数据动态展示在RecyclerView上,同时介绍了一些基本的UI动画技巧,提升用户体验。

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

/**
 * 1,   (25分) 实现ViewPager + Fragment ,切换页面, 3个Fragment;
 2,   (25分) 第一个Fragment中,  通过xUtils 获取网络数据, 展示到RecyclerView
 地址: http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1
 解析字段:  title ,  pic
 3,   (25分) 第二个Fragment 中, 属性动画, 实现机器人的:  缩放X , 移动Y , 渐			变, 旋转, 集合的效果
 4,  (25分) 第三个Fragment 中, 通过自定义组合控件, 实现TitleView 的效果,如			下图所示
 点击按钮, 弹出吐司
 */
public class Test05Activity extends AppCompatActivity {
    private ViewPager vp;
    private List<Fragment> data;
    private MyAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test05);
        vp= (ViewPager) findViewById(R.id.vp_id);
        //构建ViewPager的数据源
        data=new ArrayList<>();
        data.add(new TestFragment_01());
        data.add(new TestFragment_02());
        data.add(new TestFragment_03());
        //设置适配器
        adapter=new MyAdapter(getSupportFragmentManager());
        vp.setAdapter(adapter);
    }
    //自定义适配器
    class MyAdapter extends FragmentPagerAdapter{

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return data.get(position);
        }

        @Override
        public int getCount() {
            return data.size();
        }
    }
}
public class RvAdapter extends RecyclerView.Adapter<RvAdapter.ViewHolder> {
    private List<CookBook.DataBean> data;
    private Context context;

    public RvAdapter(Context context, List<CookBook.DataBean> data) {
        this.data = data;
        this.context=context;
    }

    public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
        //加载每个条目显示的页面
        View view= LayoutInflater.from(context).inflate(R.layout.item_rv,parent);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }



    public void onBindViewHolder(ViewHolder holder,int position){
        //赋值的操作
        holder.tv.setText(data.get(position).getTitle());
        //加载网络的图片
        x.image().bind(holder.iv,data.get(position).getPic());
    }
    public int getItemCount(){
        return data.size();
    }
    class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView iv;
        private TextView tv;

        public ViewHolder(View itemView) {
            super(itemView);
            this.iv= (ImageView) itemView.findViewById(R.id.iv_id);
            this.tv= (TextView) itemView.findViewById(R.id.tv_id);
        }
    }
}

public class CustomView extends LinearLayout {
    private ImageView iv;
    private TextView tv;
    private Button btn;
    public CustomView(Context context) {
        super(context);
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //引入页面,和当前的View绑定在一起
        LayoutInflater.from(getContext()).inflate(R.layout.custom_view,this);
        //从引入的页面中,得到控件
        iv= (ImageView) findViewById(R.id.iv_id);
        tv= (TextView) findViewById(R.id.tv_id);
        btn= (Button) findViewById(R.id.btn_id);
        //为控件设置监听器
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "你点击了按钮", Toast.LENGTH_SHORT).show();
            }
        });

    }
}

public class CookBook {
    /**
     * ret : 1
     * data : [{"id":"8289","title":"油焖大虾","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg","collect_num":"1646","food_str":"大虾 葱 生姜 植物油 料酒","num":1646},{"id":"2127","title":"四川回锅肉","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2127.jpg","collect_num":"1581","food_str":"猪肉 青蒜 青椒 红椒 姜片","num":1581},{"id":"30630","title":"超简单芒果布丁","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/31/30630.jpg","collect_num":"1514","food_str":"QQ糖 牛奶 芒果","num":1514},{"id":"9073","title":"家常红烧鱼","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9073.jpg","collect_num":"1416","food_str":"鲜鱼 姜 葱 蒜 花椒","num":1416},{"id":"10097","title":"家常煎豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10097.jpg","collect_num":"1400","food_str":"豆腐 新鲜红椒 青椒 葱花 油","num":1400},{"id":"10509","title":"水煮肉片","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10509.jpg","collect_num":"1334","food_str":"瘦猪肉 生菜 豆瓣酱 干辣椒 花椒","num":1334},{"id":"46968","title":"红糖苹果银耳汤","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/47/46968.jpg","collect_num":"1240","food_str":"银耳 苹果 红糖","num":1240},{"id":"10191","title":"麻婆豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10191.jpg","collect_num":"1208","food_str":"豆腐 肉末 生抽 白糖 芝麻油","num":1208},{"id":"2372","title":"皮蛋瘦肉粥","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2372.jpg","collect_num":"1138","food_str":"大米 皮蛋 猪肉 油条 香葱","num":1138},{"id":"2166","title":"蚂蚁上树","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2166.jpg","collect_num":"1134","food_str":"红薯粉 肉 姜 蒜 花椒","num":1134},{"id":"2262","title":"糖醋肉","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2262.jpg","collect_num":"1070","food_str":"猪肉 红椒 黄椒 洋葱 蛋清","num":1070},{"id":"9971","title":"鱼香豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9971.jpg","collect_num":"1001","food_str":"豆腐 木耳 胡萝卜 香葱 番茄酱","num":1001},{"id":"10172","title":"干煸四季豆","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10172.jpg","collect_num":"986","food_str":"四季豆 干辣椒 蒜头 酱油 糖","num":986},{"id":"2685","title":"胡萝卜肉末蒸蛋","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2685.jpg","collect_num":"906","food_str":"胡萝卜 肉 蛋 生抽 盐","num":906},{"id":"9972","title":"虎皮青椒","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9972.jpg","collect_num":"885","food_str":"青辣椒 大蒜 香醋 白糖 生抽","num":885},{"id":"10437","title":"叉烧排骨","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10437.jpg","collect_num":"792","food_str":"排骨 李锦记叉烧酱 植物油 清水 油菜","num":792},{"id":"2892","title":"\u201c五行\u201d彩蔬汤","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2892.jpg","collect_num":"755","food_str":"黑木耳 玉米 牛蒡 胡萝卜 西兰花","num":755},{"id":"2348","title":"麻辣肉丝面","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2348.jpg","collect_num":"751","food_str":"面条 肉丝 淀粉 酱油 辣椒","num":751},{"id":"33783","title":"美人豆浆","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/34/33783.jpg","collect_num":"750","food_str":"黄豆 红豆 绿豆 黑豆 黑米","num":750},{"id":"10044","title":"土豆炖翅根","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10044.jpg","collect_num":"749","food_str":"土豆 翅根 葱 姜 料酒","num":749}]
     */

    private int ret;
    private List<DataBean> 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 : 1646
         * food_str : 大虾 葱 生姜 植物油 料酒
         * num : 1646
         */

        private String id;
        private String title;
        private String pic;
        private String collect_num;
        private String food_str;
        private int 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;
        }
    }
//    private int ret;
//    private List<>

}

public class TestFragment_01 extends Fragment {
    private String url="http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";
    private RecyclerView recyclerView;

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view= inflater.inflate(R.layout.fragment_test_fragment_01, container, false);
        //初始化控件
        recyclerView= (RecyclerView) view.findViewById(R.id.recycleview);
        //设置RecyclerView显示的模式
        LinearLayoutManager manager=new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(manager);
        //通过XUtils获取网络数据,展示到RecycleView中
        /**
         * 1, 导入依赖
         * 2, 在清单文件中设置权限: 网络权限, 读写sd卡的权限
         * 3, 自定义Applcation 的子类, 初始化xUtils, 并且在清单文件的<applicaion  adnroid:name=""/>
         */
        //设置请求的参数
        RequestParams params=new RequestParams(url);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                //解析数据,得到数据源
                CookBook cookBook=new Gson().fromJson(s,CookBook.class);
                List<CookBook.DataBean> dataBeen=cookBook.getData();
                //定义适配器,显示数据
                RvAdapter adapter=new RvAdapter(getContext(),dataBeen);
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });



        return view;
    }

}

public class TestFragment_02 extends Fragment implements View.OnClickListener {
    private Button btn_01;
    private Button btn_02;
    private Button btn_03;
    private Button btn_04;
    private Button btn_05;
    private ImageView imageView;

    public TestFragment_02() {
        // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_test_fragment_02, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        btn_01= (Button) view.findViewById(R.id.btn_01);
        btn_02= (Button) view.findViewById(R.id.btn_02);
        btn_03= (Button) view.findViewById(R.id.btn_03);
        btn_04= (Button) view.findViewById(R.id.btn_04);
        btn_05= (Button) view.findViewById(R.id.btn_05);
        imageView= (ImageView) view.findViewById(R.id.iv_id);

        btn_01.setOnClickListener(this);
        btn_02.setOnClickListener(this);
        btn_03.setOnClickListener(this);
        btn_04.setOnClickListener(this);
        btn_05.setOnClickListener(this);

    }
    //点击按钮,切换动画
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_01:
                //1, 定义动画 -- 指明显为那个控件设置动画
                ObjectAnimator animator=ObjectAnimator.ofFloat(imageView,"alpha",1.0f,0.0f);
                //2,设置持续时间
                animator.setDuration(3000);
                //3,启动动画
                animator.start();
                break;
            case R.id.btn_02:
                //缩放
                ObjectAnimator animator1=ObjectAnimator.ofFloat(imageView,"scaleX",0,4);
                animator1.setDuration(5000);
                animator1.start();
                break;
            case R.id.btn_03:
                //旋转
                ObjectAnimator animator2=ObjectAnimator.ofFloat(imageView,"rotation",0,4);
                animator2.setDuration(5000);
                animator2.start();
                break;
            case R.id.btn_04:
                ObjectAnimator animator3=ObjectAnimator.ofFloat(imageView,"translationY",0,500);
                animator3.setDuration(10000);
                animator3.start();
                break;
            case R.id.btn_05:
                //1,得到属性动画集合
                AnimatorSet animatorSet=new AnimatorSet();
                //2,创建要加入的动画
                ObjectAnimator animator4=ObjectAnimator.ofFloat(imageView,"translationY",0,500,800,600,700);
                ObjectAnimator animator5=ObjectAnimator.ofFloat(imageView,"rotation",0,720);
                //3,把动画加入集合中
                //第一种方式:
//                List<Animator> list=new ArrayList<>();
//                list.add(animator4);
//                list.add(animator5);
//                //一起执行
//                animatorSet.playTogether(list);
//                //依次执行
//                animatorSet.playSequentially(list);
                //第二种方式:
                //播放:
                animatorSet.play(animator4).with(animator5);
                animatorSet.setDuration(5000);
                //设置插值器-- 先慢后快
                animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
                //4,启动动画
                animatorSet.start();
                break;


        }
    }
}

public class TestFragment_03 extends Fragment {
    private CustomView cv;

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_test_fragment_03, container, false);
        return  view;
    }

}

item_rv:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <ImageView
        android:src="@mipmap/ic_launcher"
        android:id="@+id/iv_id"
        android:layout_width="150dp"
        android:layout_height="150dp" />
    <TextView
        android:textSize="25sp"
        android:layout_margin="10dp"
        android:text="名称"
        android:id="@+id/tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

fragment_03:

<FrameLayout 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="com.example.lenovo.test_all.demo05.TestFragment_03">

    <!-- TODO: Update blank fragment layout -->
    <com.example.lenovo.test_all.demo05.CustomView
        android:layout_width="match_parent"
        android:id="@+id/cv_id"
        android:layout_height="100dp"></com.example.lenovo.test_all.demo05.CustomView>

</FrameLayout>

fragment_02

<FrameLayout 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="com.example.lenovo.test_all.demo05.TestFragment_02">

    <!-- TODO: Update blank fragment layout -->
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="透明度"
            android:id="@+id/btn_01"
            android:onClick="onClick"
            android:layout_height="wrap_content" />
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="缩放"
            android:id="@+id/btn_02"
            android:onClick="onClick"
            android:layout_height="wrap_content" />
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="旋转"
            android:id="@+id/btn_03"
            android:onClick="onClick"
            android:layout_height="wrap_content" />
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="位移"
            android:id="@+id/btn_04"
            android:onClick="onClick"
            android:layout_height="wrap_content" />
        <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="集合"
            android:id="@+id/btn_05"
            android:onClick="onClick"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <ImageView
        android:layout_gravity="center"
        android:id="@+id/iv_id"
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</FrameLayout>

fragment_01

<FrameLayout 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="com.example.lenovo.test_all.demo05.TestFragment_01">

    <!-- TODO: Update blank fragment layout -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</FrameLayout>

custom_view

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/iv"
        android:layout_margin="10dp"
        android:src="@mipmap/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_margin="10dp"
        android:text="组合的自定义View"
        android:id="@+id/tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:layout_margin="10dp"
        android:text="点我"
        android:id="@+id/btn_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

activity_test05

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/vp_id"
        ></android.support.v4.view.ViewPager>
</LinearLayout>

网络权限

 <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值