PullToRefresh


主函数

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bwie.bean.JsonBean;
import com.google.gson.Gson;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

/**
 * 作者:王钦
 * 日期:2017/10/26
 * 时间:15:44
 * 用途:主界面
 */
public class MainActivity extends AppCompatActivity {

    //创建一个Handler
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                //得到数据
                JsonBean bean = (JsonBean) msg.obj;
                list = bean.getData();
                //Log.i("aaa", list.toString());
                //创建适配器
                myAdapter = new MyAdapter();
                //给ListView设置适配器
                list_view.setAdapter(myAdapter);
            }
        }
    };
    //全局变量
    private PullToRefreshLayout pull;
    private ListView list_view;
    private String path;
    private int shu = 0;
    private List<JsonBean.DataBean> list;
    private MyAdapter myAdapter;
    private DrawerLayout mDrawerLayout = null;

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

        //判断网络
        if (isNetworkAvailable(MainActivity.this)) {
            Toast.makeText(getApplicationContext(), "当前有可用网络!", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "当前没有可用网络!", Toast.LENGTH_LONG).show();
        }

        //初始化控件
        pull = (PullToRefreshLayout) findViewById(R.id.pull);
        list_view = (ListView) findViewById(R.id.list_view);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);

        //DrawerLayout的监听事件
        mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
            /**
             * 当抽屉滑动状态改变的时候被调用
             * 状态值是STATE_IDLE(闲置--0), STATE_DRAGGING(拖拽的--1), STATE_SETTLING(固定--2)中之一。
             * 抽屉打开的时候,点击抽屉,drawer的状态就会变成STATE_DRAGGING,然后变成STATE_IDLE
             */
            @Override
            public void onDrawerStateChanged(int arg0) {
                //Log.i("drawer", "drawer的状态:" + arg0);
            }

            /**
             * 当抽屉被滑动的时候调用此方法
             * arg1 表示 滑动的幅度(0-1)
             */
            @Override
            public void onDrawerSlide(View arg0, float arg1) {
                //Log.i("drawer", arg1 + "");
            }

            /**
             * 当一个抽屉被完全打开的时候被调用
             */
            @Override
            public void onDrawerOpened(View arg0) {
                Log.i("drawer", "抽屉被完全打开了!");
            }

            /**
             * 当一个抽屉完全关闭的时候调用此方法
             */
            @Override
            public void onDrawerClosed(View arg0) {
                Log.i("drawer", "抽屉被完全关闭了!");
            }
        });

        //请求网络
        getData();

        //pulltorefresh的监听事件
        pull.setRefreshListener(new BaseRefreshListener() {

            //下拉刷新
            @Override
            public void refresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //得到接口
                        path = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum=" + shu;
                        //请求网络
                        getData();
                        //刷新适配器
                        myAdapter.notifyDataSetChanged();
                        //停止下拉刷新
                        pull.finishRefresh();
                    }
                    //设置下拉刷新的时间
                }, 3000);
            }

            //上拉加载
            @Override
            public void loadMore() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        shu++;
                        //得到接口
                        path = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum=" + shu;
                        //请求网络
                        getData();
                        //刷新适配器
                        myAdapter.notifyDataSetChanged();
                        //停止上拉加载
                        pull.finishLoadMore();
                    }
                    //设置上拉加载的时间
                }, 3000);
            }
        });

    }

    //适配器
    class MyAdapter extends BaseAdapter {

        private final int lin = 0;
        private final int one = 1;

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            int type = getItemViewType(i);
            ViewHolderOne holderOne;
            ViewHolderTwo holderTwo;

            //多条目展示
            switch (type) {
                case lin:
                    if (view == null) {
                        //初始化视图
                        view = View.inflate(MainActivity.this, R.layout.item, null);
                        //重写ViewHolderOne
                        holderOne = new ViewHolderOne();
                        //初始化控件
                        holderOne.img = view.findViewById(R.id.img);
                        holderOne.text = view.findViewById(R.id.text);
                        //绑定
                        view.setTag(holderOne);
                    }
                    break;
                case one:
                    if (view == null) {
                        //初始化视图
                        view = View.inflate(MainActivity.this, R.layout.item1, null);
                        //重写ViewHolderTwo
                        holderTwo = new ViewHolderTwo();
                        //初始化控件
                        holderTwo.text_01 = view.findViewById(R.id.text_1);
                        //绑定
                        view.setTag(holderTwo);
                    }
                    break;
                default:
                    break;
            }

            switch (type) {
                case lin:
                    holderOne = (ViewHolderOne) view.getTag();
                    //赋值
                    holderOne.text.setText(list.get(i).getTITLE());
                    ImageLoader.getInstance().displayImage((String) list.get(i).getIMAGEURL(), holderOne.img);
                    break;
                case one:
                    holderTwo = (ViewHolderTwo) view.getTag();
                    //赋值
                    holderTwo.text_01.setText(list.get(i).getTITLE());
                    break;
                default:
                    break;
            }

            //返回视图
            return view;
        }

        //得到视图的类型
        @Override
        public int getItemViewType(int position) {

            int i = position % 2;

            if (i == 0) {
                return 0;
            } else {
                return 1;
            }

        }

        //得到视图类型的数量
        @Override
        public int getViewTypeCount() {
            return 2;
        }

        class ViewHolderOne {
            ImageView img;
            TextView text;
        }

        class ViewHolderTwo {
            TextView text_01;
        }

    }

    //请求网络
    public void getData() {
        new Thread() {
            @Override
            public void run() {
                //判断接口地址
                if (path == null) {
                    path = "http://www.93.gov.cn/93app/data.do?channelId=0&startNum=" + shu;
                }
                try {
                    //获取接口地址
                    URL url = new URL(path);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    //设置提交方式
                    connection.setRequestMethod("GET");
                    //设置传递数据的超时时间
                    connection.setReadTimeout(5000);
                    //设置建立连接的超时时间
                    connection.setConnectTimeout(5000);
                    //判断请求码
                    if (connection.getResponseCode() == 200) {
                        InputStream stream = connection.getInputStream();
                        //转码
                        String json = zhuan(stream, "utf-8");
                        //Log.i("aaa", json);
                        //解析
                        Gson gson = new Gson();
                        JsonBean fromJson = gson.fromJson(json, JsonBean.class);
                        Message message = Message.obtain();
                        message.what = 0;
                        message.obj = fromJson;
                        //发送消息
                        handler.sendMessage(message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                super.run();
            }
        }.start();
    }

    //转码
    private String zhuan(InputStream stream, String s) {
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(stream, s);
            BufferedReader reader = new BufferedReader(inputStreamReader);
            String ss = null;
            StringBuilder builder = new StringBuilder();
            while ((ss = reader.readLine()) != null) {
                builder.append(ss);
            }
            inputStreamReader.close();
            return builder.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //检查当前网络是否可用
    public boolean isNetworkAvailable(Activity activity) {
        Context context = activity.getApplicationContext();
        // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivityManager == null) {
            return false;
        } else {
            // 获取NetworkInfo对象
            NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

            if (networkInfo != null && networkInfo.length > 0) {
                for (int i = 0; i < networkInfo.length; i++) {
                    System.out.println(i + "===状态===" + networkInfo[i].getState());
                    System.out.println(i + "===类型===" + networkInfo[i].getTypeName());
                    // 判断当前网络状态是否为连接状态
                    if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

}

主页面布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="180dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="#fff">

            <LinearLayout
                android:id="@+id/linear"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:src="@mipmap/ic_launcher" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="立即登录" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/linear"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="我的消息" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="教学视频" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="我的成绩" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="学车日记" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="文章收藏" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="文章足迹" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="教员中心" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="设置" />

            </LinearLayout>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.jwenfeng.library.pulltorefresh.PullToRefreshLayout
                android:id="@+id/pull"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ListView
                    android:id="@+id/list_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />

            </com.jwenfeng.library.pulltorefresh.PullToRefreshLayout>

        </RelativeLayout>

    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

item

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

    <ImageView
        android:id="@+id/img"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:padding="10dp"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="aaaaaaaaaa" />

</LinearLayout>




item1

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

    <TextView
        android:id="@+id/text_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="aaaaa" />

</LinearLayout>


### 基于Unity3D的ACT游戏的设计与实现 #### 摘要与关键词解析 本项目聚焦于使用Unity3D引擎开发一款2D动作类游戏(ACT),旨在为玩家提供沉浸式的游戏体验以及成就感。游戏开发过程中,作者不仅关注游戏的核心玩法,还深入探讨了如何利用Unity内置的各种工具和技术来提升游戏性能、改善用户体验。 **关键词**: - **Unity**:一个跨平台的综合游戏开发引擎,支持2D和3D游戏开发。 - **ScriptableObject**:Unity中的一种特殊脚本类型,用于存储数据和配置信息,方便在多个场景间共享。 - **游戏开发**:涵盖了游戏设计、编程、美术创作等多个方面的工作。 - **2D游戏**:指采用二维画面的游戏,相比3D游戏,具有更简洁的视觉风格和较低的技术门槛。 - **状态机**:一种常用的编程模式,用于管理游戏对象的状态转换,如角色的动作变化等。 - **Cinemachine**:Unity的一个插件,提供了高级的相机控制系统,能够创建出电影级的摄像机动画效果。 #### 第1章:绪论 在本章中,作者首先阐述了游戏开发的背景及意义。随着科技的进步,数字娱乐已经成为人们生活中不可或缺的一部分,而游戏作为其中的一种形式,更是受到了广泛的关注。游戏不仅能够提供娱乐,还能培养玩家的逻辑思维能力和解决问题的能力。因此,开发高质量的游戏产品显得尤为重要。 随后,作者介绍了本项目的起源和发展过程,包括为何选择Unity作为开发工具,以及项目的目标和预期成果。此外,作者还提到了Unity引擎的特点及其在游戏开发中的优势,比如跨平台兼容性、丰富的资源库、强大的社区支持等。 #### 技术选型与实现细节 1. **C#语言**:Unity主要使用的编程语言是C#,它是一种面向对象的语言,具有良好的可读性和扩展性。在本项目中,C#被用来编写游戏逻辑、实现用户交互等功能。 2. **UGUI和Text Mesh Pro**:UGUI是Unity提供的用户界面系统,可以轻松地创建各种界面元素,如按钮、滑块等。Text Mesh Pro则是一款高级文本渲染插件,能够提高文本的渲染质量和性能,使得游戏中的文字更加清晰易读。 3. **有限状态机**:状态机是一种常见的游戏开发模式,用于管理和控制游戏对象的不同状态。在本项目中,状态机被用来处理游戏角色的动作变化,例如攻击、跳跃、行走等。通过这种方式,可以更加高效地组织代码,提高游戏逻辑的清晰度和可维护性。 4. **ScriptableObject**:这是一种特殊的脚本类型,在Unity中主要用于存储数据和配置信息。通过ScriptableObject,开发者可以在编辑器中直接编辑这些数据,而无需重启游戏。这种机制极大地提高了开发效率,并且使得多人协作变得更加容易。 5. **物理系统**:Unity内置的物理引擎能够模拟真实的物理行为,如重力、碰撞等。在本项目中,物理系统被用来处理角色和环境之间的互动,确保游戏中的物理效果逼真可靠。 #### 测试与优化 为了确保游戏的质量,作者进行了多轮的测试,包括功能测试、性能测试以及玩家体验测试。通过不断地调整和优化,最终实现了游戏在低配置设备上的流畅运行。 **总结**: 通过上述分析可以看出,《基于Unity3D的ACT游戏的设计与实现》项目不仅关注游戏本身的玩法设计,还深入探讨了如何利用先进的技术和工具来提高游戏的品质。从技术选型到具体实现,再到后期的测试与优化,每一个环节都体现了作者的专业水平和对游戏开发的热情。对于想要进入游戏开发领域的初学者来说,该项目提供了一个非常好的学习案例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值