Android开发笔记

2018-04-09 Android, 技术 2条评论

嘛..最近学习了下 Android Studio 的安卓编程。做了一个获取字幕的项目练手..就记录一下

Gayhub地址:https://github.com/BianTan/SubHD (项目暂未完工)

( 使用了第三方库:PullRefreshLayout )
activity_main.xml

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="org.biantan.subhd.SearchActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.mintmedical.refresh.PullRefreshLayout
            android:id="@+id/swipeRefreshLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </android.support.v7.widget.RecyclerView>
        </com.mintmedical.refresh.PullRefreshLayout>
    </LinearLayout>
</RelativeLayout>

MainActivity.java

package org.biantan.subhd;

import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.mintmedical.refresh.PullRefreshLayout;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    private List fruitList = new ArrayList<>();
    FruitAdapter adapter = new FruitAdapter(this, fruitList);
    private ProgressDialog dialog;

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

        //显示提示正在加载窗口
        dialog = new ProgressDialog(this);
        dialog.setMessage("正在获取请稍后...");
        dialog.setCancelable(false);
        dialog.show();
        //初始化 抓取数据
        initFruits();
        xjsx(); //下拉刷新  上拉加载

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view); //绑定RecyclerView控件
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }

    private void xjsx(){
        final PullRefreshLayout layout = (PullRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() { //下拉
            @Override
            public void onRefresh() {
                layout.postDelayed(new Runnable() { //延迟1s执行任务
                    @Override
                    public void run() {
                        layout.setRefreshing(false);  //停止旋转
                    }
                }, 1000);
            }
        });
        layout.setOnLoadListener(new PullRefreshLayout.OnLoadListener() {  //上拉
            @Override
            public void onLoad() {
                initFruits();//抓取数据
            }
        });
    }

    /**
     * 加载数据线程
     **/
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
        /**获取网络数据操作
         *...code...
         * Fruit zr = new Fruit(xxx,xxx); //传入数据
         * fruitList.add(zr);  //添加
         **/
        }
    };

    /**
     * 将数据填充到RecyclerView中
     **/
    private void show() {
        final PullRefreshLayout layout = (PullRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        layout.setLoading(false);
        //adapter.notifyDataSetChanged();
        adapter.notifyItemRemoved(adapter.getItemCount());
    }

    /**
     * 初始化 加载数据
     **/
    private void initFruits() {
        if (isConnectIsNomarl()) { //判断网络是否连接
            new Thread(runnable).start();  // 启动子线程获取数据
        } else {
            dialog.dismiss();
        }
    }

    /**
     * 判断网络是否连接
     **/
    private boolean isConnectIsNomarl() {
        ConnectivityManager connectivityManager = (ConnectivityManager) this.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info != null && info.isAvailable()) {
            String intentName = info.getTypeName();
            Log.d("Intent", "当前网络名称:" + intentName);
            return true;
        } else {
            Log.d("Intent", "没有可用网络");
            return false;
        }
    }

}

Fruit.java

package org.biantan.subhd;

public class Fruit {

    private String xxx;

    public Fruit(String xxx) {
        this.xxx= xxx;
    }

    public String getxxx() {
        return xxx;
    }

}

FruitAdapter.java

package org.biantan.subhd;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {

    private Context mContext;
    private List<Fruit> mFruitList;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View fruitView;
        TextView fruitxxx;

        public ViewHolder(View view) {
            super(view);
            fruitView = view;
            fruitxxx = (TextView) view.findViewById(R.id.textView);  //绑定控件
        }
    }

    public FruitAdapter(Context context, List<Fruit> fruitList) {
        mContext = context;
        mFruitList = fruitList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        //点击事件
        holder.fruitsteam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*
                 * 点击事件的code
                 */
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {  //这里处理UI设置啥的
        Fruit fruit = mFruitList.get(position);
        holder.fruitxxx.setText(fruit.getxxx());
    }

    @Override
    public int getItemCount() {
        return mFruitList.size();
    }
}

有2人发表了评论咯

  1. 仰望Android大佬∠( ᐛ 」∠)_

    miiiku

    2018/4/14

    @Ta
    #1

发表评论