Android开发之学习

 

  • 实现效果有:关掉状态栏和标题栏显示打开app和UI实时刷新等待时间,获取虎扑体育运动实时信息;

  • 首先:关掉标题栏和状态栏:

  • theme有多种NoactionBar,这种白天关闭标题栏也就是DayNight;

  • 其次:开展开机的UI的实时刷新,实现开机界面5、4、3、2、1的效果:

直接贴代码:

package com.example.news;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

public class ForeActivity extends AppCompatActivity {
    private ImageView imageView;
    private TextView textView;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            if (msg.what == 1) {
                Log.i("Thead", "begin");
                int i = msg.arg1;
                textView.setText(i + "");
                Log.i("Thead", "All over");
            }
            if (msg.what == 0) {
                Intent intent = new Intent(ForeActivity.this, MainActivity.class);
                startActivity(intent);
                finish();

            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fore);
        imageView = findViewById(R.id.imageView);
        textView = findViewById(R.id.textView);
        //得到当前界面的装饰视图
        View decorView = getWindow().getDecorView();
//        SYSTEM_UI_FLAG_FULLSCREEN表示全屏的意思,也就是会将状态栏隐藏
        //设置系统UI元素的可见性
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        //隐藏标题栏
        sendRequest();

    }

    public void sendRequest() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 5; i >= 0; i--) {
                    Log.i("Thead", i + "次");
                    try {
                        Thread.sleep(1000);
                        Message message = new Message();
                        if (i!=0){

                            message.what = 1;
                        }else{
                            message.what = 0;
                        }
                        message.arg1 = i;
                        handler.sendMessage(message);
                    } catch (Exception e) {
                        Log.i("Thead", i + "over");
                    }
                }

            }
        }).start();
    }
}

最后是虎扑体育的数据提取:

直接贴代码了:

代码规范,直接设置对象来封装数据:如news(标题、url 、时间)

贴代码:

package com.example.news.empity;

import java.util.Date;

/**
 * 
 * Project_Name: News
 * Package_Name: com.example.news.empity
 * Date: 2019/3/2
 * Time: 9:38
 */
public class News {
    //新闻标题、新闻链接、新闻时间
    private String head;
    private String url;
    private String data;

    public String getHead() {
        return head;
    }

    public String getUrl() {
        return url;
    }

    public String getData() {
        return data;
    }

    public void setHead(String head) {
        this.head = head;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public void setData(String data) {
        this.data = data;
    }

    public News(String head, String url, String data) {
        this.head = head;
        this.url = url;
        this.data = data;
    }
}

 然后就是News的适配器因为是放置到listView显示

package com.example.news.empity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.news.R;

import java.util.List;

/**
 * Created by 
 * Project_Name: News
 * Package_Name: com.example.news.empity
 * Date: 2019/3/2
 * Time: 10:05
 */
public class NewsAdapter extends BaseAdapter {
    private List<News> list;
    private View view;
    private Context context;
    private ViewHolder viewHolder;
    public NewsAdapter(Context context,List<News> list) {
        this.list = list;
        this.context = context;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView ==null){
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.news_item,null);
            viewHolder.newsTitle=view.findViewById(R.id.news_title);
            viewHolder.newsTime=view.findViewById(R.id.news_time);
            view.setTag(viewHolder);
        }else {
            view=convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.newsTitle.setText(list.get(position).getHead());
        viewHolder.newsTime.setText("来自"+list.get(position).getData());
        return view;
    }
    class ViewHolder{
        TextView newsTitle;
        TextView newsTime;
    }
}

 然后开干:

package com.example.news;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.news.empity.News;
import com.example.news.empity.NewsAdapter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<News> news_List ;
    private Handler handler;
    private ListView listView;
    private NewsAdapter newsAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.showList);
        news_List = new ArrayList<>();
        getNews();
        Log.i("create",news_List.size()+"数据大小");
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
                    newsAdapter = new NewsAdapter(MainActivity.this, news_List);
                    listView.setAdapter(newsAdapter);
                    Log.i("create",news_List.size()+"数据大小");
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            News news = news_List.get(position);
                            Intent intent = new Intent(MainActivity.this, WebActivity.class);
                            intent.putExtra("news_url", news.getUrl());
                            startActivity(intent);
                        }
                    });
                }
            }
        };
    }

    public void getNews() {
        Log.i("message", "getNews begin");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 1; i < 20; i++) {
                        //加载20页的数据
                        Document document = Jsoup.connect("https://voice.hupu.com/nba/" + Integer.toString(i)).get();
                        Elements headLink = document.select("div.list-hd");
                        Elements time = document.select("div.otherInfo");
                        for (int j = 0; j < headLink.size(); j++) {
                            //for循环拿到数据
                            String title = headLink.get(j).select("a").text();
                           // Log.i("message", "getNews " + title);
                            String link = headLink.get(j).select("a").attr("href");
                           // Log.i("message", "getNews " + link);
                            String data = time.get(j).select("span.other-left").select("a").text();
                            //Log.i("message", "getNews " + data);
                            News news = new News(title, link, data);//封装成New对象
                            //Log.i("message", "News对象显示 " + news.toString());
                            news_List.add(news);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Message msg = new Message();
                msg.what = 1;
                handler.sendMessage(msg);

            }
        }).start();
    }
}

显示的效果如上:如有错误的地方,望指正~

-感谢无名大佬的代码指导!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值