数据库+上拉下拉+轮播+fragment切换+抽基类三周练习

一、布局:

activity_main.xml、fragment_my.xml、fragment_shou.xml、item_layout.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

    <!--主页面布局-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:background="#96CDCD"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:text="新闻"
            android:textColor="#ffffff"
            android:textSize="20sp" />

        <android.support.v4.view.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="9"></android.support.v4.view.ViewPager>

        <android.support.design.widget.TabLayout
            android:id="@+id/tab_layout"

            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"></android.support.design.widget.TabLayout>
    </LinearLayout>
    <!--侧拉布局-->
    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#ccc"
        android:gravity="center_horizontal"
        android:orientation="vertical">

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

    </LinearLayout>


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


















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

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

    <com.youth.banner.Banner
        android:id="@+id/my_banner"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"></com.youth.banner.Banner>

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/p_listview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="6"></com.handmark.pulltorefresh.library.PullToRefreshListView>

</LinearLayout>
<?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_view"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:src="@mipmap/ic_launcher"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/text_title"
            android:textSize="20sp"
            android:text="title"
            android:padding="10dp"
            android:singleLine="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/text_info"
            android:textSize="20sp"
            android:text="info"
            android:padding="10dp"
            android:singleLine="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</LinearLayout>

二、主方法:

 activity:

package com.baway.week3demo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

/**
 * @Auther: lenovo
 * @Date: 2019/2/28 18:59:41
 * @Description:
 */
public abstract class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(initLayout());
        initView();
        initData();
        initListener();
    }

    //监听
    protected abstract void initListener();

    //初始化数据
    protected abstract void initData();

    //初始化控件
    protected abstract void initView();

    //初始化布局
    protected abstract int initLayout();
    //findViewById
    protected <T extends View> T fvbi(int resId){
        return  (T)findViewById(resId);
    }
}







package com.baway.week3demo.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.baway.week3demo.R;
import com.baway.week3demo.adapter.MyFragmentAdapter;
import com.baway.week3demo.fragment.LieFragment;
import com.baway.week3demo.fragment.MyFragment;
import com.baway.week3demo.fragment.ShouFragment;

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

public class MainActivity extends BaseActivity {
    private DrawerLayout drawer_layout;
    private ViewPager view_pager;
    private TabLayout tab_layout;
    private List<Fragment> list = new ArrayList<Fragment>();
    private String[] titles = {"首页","我的","列表"};
    private MyFragmentAdapter adapter;



    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        list.add(new ShouFragment());
        list.add(new MyFragment());
        list.add(new LieFragment());
        //创建适配器
         adapter = new MyFragmentAdapter(getSupportFragmentManager(), list, titles);
         view_pager.setAdapter(adapter);
         //关联TabLayout
        tab_layout.setupWithViewPager(view_pager);

    }

    @Override
    protected void initView() {
        drawer_layout = fvbi(R.id.drawer_layout);
        view_pager = fvbi(R.id.view_pager);
        tab_layout = fvbi(R.id.tab_layout);
        //侧拉可以不设置


    }

    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }
}

 adapter:一个MyAdapter,一个MyFragmentAdapter

package com.baway.week3demo.adapter;

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

import com.baway.week3demo.R;
import com.baway.week3demo.bean.News;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * @Auther: lenovo
 * @Date: 2019/2/28 19:07:37
 * @Description:
 */
public class MyAdapter extends BaseAdapter {
    private List<News> list;
    private Context context;

    public MyAdapter(List<News> list, Context context) {
        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) {

        ViewHolder holder;
        if (convertView == null){
            convertView = View.inflate(context,R.layout.item_layout,null);
            holder = new ViewHolder();
            holder.img_view = convertView.findViewById(R.id.img_view);
            holder.text_info = convertView.findViewById(R.id.text_info);
            holder.text_title = convertView.findViewById(R.id.text_title);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.text_title.setText(list.get(position).getNews_title());
        holder.text_info.setText(list.get(position).getNews_summary());
        Glide.with(context).load(list.get(position).getPic_url()).into(holder.img_view);
        return convertView;
    }
    class ViewHolder{
        private ImageView img_view;
        private TextView text_title,text_info;
    }
}












package com.baway.week3demo.adapter;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**
 * @Auther: lenovo
 * @Date: 2019/2/28 19:09:43
 * @Description:
 */
public class MyFragmentAdapter extends FragmentPagerAdapter {
    private List<Fragment> list;
    private String[] titles;

    public MyFragmentAdapter(FragmentManager fm, List<Fragment> list, String[] titles) {
        super(fm);
        this.list = list;
        this.titles = titles;
    }



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

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

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}

dao:

package com.baway.week3demo.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.baway.week3demo.helper.MyHelper;

/**
 * @Auther: lenovo
 * @Date: 2019/2/28 19:12:50
 * @Description:
 */
public class SqlDao {
    private final MyHelper helper;
    private String table = "news";

    public SqlDao(Context mContext){
        helper = new MyHelper(mContext);
    }
    //添加数据库

    public void insertData(String url,String json){
        SQLiteDatabase database = helper.getWritableDatabase();
        //先删除数据
        database.delete(table,"url=?",new String[]{url});
        //添加数据
        ContentValues values = new ContentValues();
        values.put("url",url);
        values.put("json",json);
        database.insert(table,null,values);

    }
    //查询数据库
    public String selectData(String url){
        String json = "";
        SQLiteDatabase database = helper.getWritableDatabase();
        Cursor cursor = database.query(table,null,"url=?",new String[]{url},null,null,null);
        while (cursor.moveToNext()){
            json = cursor.getString(cursor.getColumnIndex("json"));

        }
        return json;
    }
}

 helper:

package com.baway.week3demo.helper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * @Auther: lenovo
 * @Date: 2019/2/28 19:34:30
 * @Description:
 */
public class MyHelper extends SQLiteOpenHelper {
    public MyHelper(Context context){
        super(context,"bw.db",null,1);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table news(url varchar(100),json varchar(800))");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

 util:

package com.baway.week3demo.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;

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

/**
 * @Auther: lenovo
 * @Date: 2019/2/28 19:35:32
 * @Description:
 */
public class HttpUtil {
    //判断网络
    public static boolean isNetConnected(Context context){
        if (context != null){
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo info = manager.getActiveNetworkInfo();
            if (info != null){
                return info.isConnected();
            }
        }
        return false;
    }
    //请求数据
    public static String getNetData(String mUrl){
        try{
            URL url = new URL(mUrl);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");
            int code = connection.getResponseCode();
            if (code == 200){
                InputStream stream = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
                StringBuilder builder = new StringBuilder();
                String str = "";
                while ((str = reader.readLine()) != null){
                    builder.append(str);

                }
                return builder.toString();
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }
    //创建回调接口
    public interface MyCallBack{
        void getData(String str);


    }
    public static void MyTask(String url,final MyCallBack callBack){
        new AsyncTask<String,Void,String>(){
            @Override
            protected String doInBackground(String... strings) {
                return HttpUtil.getNetData(strings[0]);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                callBack.getData(s);
            }
        }.execute(url);
    }
}

三、网络权限:

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

 四、依赖:

banner依赖;pulltorefresh_library;design依赖;glide依赖;gson依赖。

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值