倒计时

需要导入的依赖:

 implementation 'com.android.support:design:28+'
    implementation 'com.github.userswlwork:pull-to-refresh:1.0.0'
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation project(':xlistviewlibrary')
    implementation 'com.recker.flybanner:flybanner:1.3'
     androidTestImplementation 'com.android.support.test:runner:1.0.2'

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注意在工作空间的依赖

在这个文件下

 
 
  • 1

allprojects {
repositories {
google()
jcenter()
maven {url “https://jitpack.io”}
}
}


 
 

    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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@mipmap/a"
    
    tools:context=".MainActivity"&gt;
    
    &lt;TextView
        android:id="@+id/mTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:text="5秒"
        android:textColor="#fff"
        android:textSize="30sp" /&gt;
    

    </RelativeLayout

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    MainActivity.java

    import android.content.Intent;
    import android.os.Handler;
    import android.os.Message;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;
    

    public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private int time =5;
    private Handler handler =new Handler(){
    @Override
    public void handleMessage(Message msg) {
    super.handleMessage(msg);
    time–;
    textView.setText(time+“秒”);
    if (time==0){

               startActivity(new Intent(MainActivity.this,TwoActivity.class));
               finish();
               handler.removeMessages(0);
          }
        handler.sendEmptyMessageDelayed(0,1000);
    }
    

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

          textView = findViewById(R.id.mTime);
    
           handler.sendEmptyMessageDelayed(0,1000);
    }
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    activity_two.xml布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:id="@+id/drawer"
        android:orientation="vertical"
        android:layout_height="match_parent"
        tools:context=".TwoActivity">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
        &lt;android.support.v4.view.ViewPager
            android:id="@+id/viewPage"
            android:layout_weight="7"
            android:layout_width="match_parent"
            android:layout_height="0dp"&gt;
    
        &lt;/android.support.v4.view.ViewPager&gt;
        &lt;RadioGroup
            android:id="@+id/group"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="horizontal"
            android:layout_weight="1"&gt;
    
            &lt;RadioButton
                android:id="@+id/btu1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:button="@null"
                android:text="首页"
                android:background="@drawable/selector"
                android:gravity="center"
                android:padding="10dp"
                android:layout_weight="1"/&gt;
            &lt;RadioButton
                android:id="@+id/btu2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:button="@null"
                android:text="其他"
                android:background="@drawable/selector"
                android:gravity="center"
                android:padding="10dp"
                android:layout_weight="1"/&gt;
            &lt;RadioButton
                android:id="@+id/btu3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:button="@null"
                android:text="我的"
                android:background="@drawable/selector"
                android:gravity="center"
                android:padding="10dp"
                android:layout_weight="1"/&gt;
        &lt;/RadioGroup&gt;
    &lt;/LinearLayout&gt;
    &lt;LinearLayout
        android:orientation="vertical"
        android:layout_gravity="start"
        android:background="@mipmap/m"
        android:layout_width="match_parent"
        android:layout_height="match_parent"&gt;
    
        &lt;ImageView
            android:id="@+id/imageview"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@mipmap/ic_launcher"/&gt;
    
        &lt;ListView
            android:id="@+id/list_text"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:background="#00f"&gt;
    
        &lt;/ListView&gt;
    &lt;/LinearLayout&gt;
    

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

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    TwoActivity.java

    package bawei.com.yuekaodemo1;
    

    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.AppCompatActivity;
    import android.view.Gravity;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.RadioGroup;
    import android.widget.Toast;

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

    import bawei.com.yuekaodemo1.fragment.FragmnetOne;
    import bawei.com.yuekaodemo1.fragment.FragmnetThree;
    import bawei.com.yuekaodemo1.fragment.FragmnetTwo;

    public class TwoActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private RadioGroup group;
    private ArrayList<Fragment> list = new ArrayList<>();
    private DrawerLayout drawerLayout;
    private ListView listView;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
    
        drawerLayout = findViewById(R.id.drawer);
        listView = findViewById(R.id.list_text);
        imageView = findViewById(R.id.imageview);
    
        viewPager = findViewById(R.id.viewPage);
        group = findViewById(R.id.group);
        group.check(group.getChildAt(0).getId());
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View view, float v) {
    
            }
    
            @Override
            public void onDrawerOpened(@NonNull View view) {
                Toast.makeText(TwoActivity.this,"已被打开",Toast.LENGTH_LONG).show();
            }
    
            @Override
            public void onDrawerClosed(@NonNull View view) {
                Toast.makeText(TwoActivity.this,"已被关闭",Toast.LENGTH_LONG).show();
            }
    
            @Override
            public void onDrawerStateChanged(int i) {
    
            }
        });
    
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                drawerLayout.closeDrawer(Gravity.START);
            }
        });
    
                    list.add(new FragmnetOne());
                    list.add(new FragmnetTwo());
                    list.add(new FragmnetThree());
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return list.get(i);
            }
    
            @Override
            public int getCount() {
                return list.size();
            }
        });
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
    
                switch (checkedId){
                    case R.id.btu1:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.btu2:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.btu3:
                        viewPager.setCurrentItem(2);
                        break;
                }
            }
        });
    
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
    
            }
    
            @Override
            public void onPageSelected(int i) {
                group.check(group.getChildAt(i).getId());
            }
    
            @Override
            public void onPageScrollStateChanged(int i) {
    
            }
        });
    
    
    
        final List&lt;String&gt; list_text = new ArrayList&lt;&gt;();
        list_text.add("首页");
        list_text.add("我的");
        list_text.add("视频");
        list_text.add("新闻");
        list_text.add("热点");
    
        listView.setAdapter(new ArrayAdapter&lt;String&gt;(this,android.R.layout.simple_list_item_1,list_text));
    
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
    
                viewPager.setCurrentItem(position);
    
                drawerLayout.closeDrawers();
            }
        });
    }
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147

    NetWord.java

    package bawei.com.weichunhao20190119.netword;
    

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

    public class NetWord {
    public static boolean isconnection(Context context){
    if (context!=null){
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null) {
    return networkInfo.isConnected();
    }
    }
    return false;

    }
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    Mapp.java图片框架

    package bawei.com.yuekaodemo1.app;
    

    import android.app.Application;
    import android.os.Environment;

    import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
    import com.nostra13.universalimageloader.core.ImageLoader;
    import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

    import java.io.File;

    public class Mapp extends Application {

    File file = new File(Environment.getExternalStorageDirectory()+"/"+"image");
    @Override
    public void onCreate() {
        super.onCreate();
    
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
                .diskCache(new UnlimitedDiskCache(file))
                .build();
    
           ImageLoader imageLoader = ImageLoader.getInstance();
    
             imageLoader.init(configuration);
           // ImageLoader.getInstance().init(configuration);
    }
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    MySQlite创建数据库

    package bawei.com.yuekaodemo1.sqlite;
    

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

    public class MySQlite extends SQLiteOpenHelper {
    public MySQlite(Context context) {
    super(context, “User.db”, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table user(id integer primary key autoincrement," +
                "url text," +
                "jsonData text)");
    
    }
    
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
    }
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    userDao.java

    package bawei.com.yuekaodemo1.dao;
    

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

    import bawei.com.yuekaodemo1.sqlite.MySQlite;

    public class UserDao {

          private SQLiteDatabase database;
    
          public UserDao(Context context){
    
              MySQlite mySQlite = new MySQlite(context);
              database = mySQlite.getWritableDatabase();
          }
    //插入的方法
    public void add(String url, String data) {
        //先删除相同url地址的数据
        database.delete("user","url=?",new String[]{url});
        ContentValues values = new ContentValues();
        values.put("url", url);//k值一定是数据库的字段值
        values.put("jsonData", data);
        //返回行号
        database.insert("user", null, values);
    }
    //查询
    public String queryData(String url){
        String data="";
        Cursor cursor = database.query("user", null, "url=?", new String[]{url}, null, null, null);
        while (cursor.moveToNext()){
            //获得jsonData字段的内容
            data= cursor.getString(cursor.getColumnIndex("jsonData"));
        }
        return data;
    }
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    bean.java

    package bawei.com.yuekaodemo1.bean;
    

    import java.util.List;

    public class Goods {

    private int status;
    private String info;
    private List&lt;DataBean&gt; data;
    
    public int getStatus() {
        return status;
    }
    
    public void setStatus(int status) {
        this.status = status;
    }
    
    public String getInfo() {
        return info;
    }
    
    public void setInfo(String info) {
        this.info = info;
    }
    
    public List&lt;DataBean&gt; getData() {
        return data;
    }
    
    public void setData(List&lt;DataBean&gt; data) {
        this.data = data;
    }
    
    public static class DataBean {
        
    
        private String news_id;
        private String news_title;
        private String news_summary;
        private String pic_url;
    
        public DataBean(String data, Object o) {
        }
    
        public String getNews_id() {
            return news_id;
        }
    
        public void setNews_id(String news_id) {
            this.news_id = news_id;
        }
    
        public String getNews_title() {
            return news_title;
        }
    
        public void setNews_title(String news_title) {
            this.news_title = news_title;
        }
    
        public String getNews_summary() {
            return news_summary;
        }
    
        public void setNews_summary(String news_summary) {
            this.news_summary = news_summary;
        }
    
        public String getPic_url() {
            return pic_url;
        }
    
        public void setPic_url(String pic_url) {
            this.pic_url = pic_url;
        }
    }
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    MyAdapter适配器

    package bawei.com.yuekaodemo1.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.nostra13.universalimageloader.core.ImageLoader;

    import java.util.List;

    import bawei.com.yuekaodemo1.R;
    import bawei.com.yuekaodemo1.bean.Goods;

    public class MyAdapter extends BaseAdapter {

    private ImageLoader imageLoader = ImageLoader.getInstance();
    private List&lt;Goods.DataBean&gt; list ;
    private Context context;
    
    public MyAdapter(List&lt;Goods.DataBean&gt; 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 int getViewTypeCount() {
        return 2;
    }
    
    @Override
    public int getItemViewType(int position) {
        return position%2;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int itemViewType = getItemViewType(position);
        switch (itemViewType){
            case 0:
                ViewHolder1 viewHolder1 = new ViewHolder1();
                if (convertView == null){
    
                    convertView = View.inflate(context ,R.layout.mybase1,null);
    
                    viewHolder1.textView =convertView.findViewById(R.id.textView);
                    viewHolder1.imageView = convertView.findViewById(R.id.imageView);
                    convertView.setTag(viewHolder1);
                }else {
                    viewHolder1= (ViewHolder1) convertView.getTag();
                }
    
                viewHolder1.textView.setText(list.get(position).getNews_title());
                imageLoader.displayImage(list.get(position).getPic_url(),viewHolder1.imageView);
                break;
    
    
    
            case 1:
    
                ViewHolder2 viewHolder2 = new ViewHolder2();
                if(convertView == null){
    
                    convertView = View.inflate(context,R.layout.mybase2,null);
    
                    viewHolder2.tv1 = convertView.findViewById(R.id.tv1);
    
                    convertView.setTag(viewHolder2);
                }else{
                    viewHolder2 = (ViewHolder2) convertView.getTag();
                }
                viewHolder2.tv1.setText(list.get(position).getNews_title());
    
                break;
        }
        return convertView;
    }
    class ViewHolder1{
    
        ImageView imageView;
        TextView textView;
    }
    
    class ViewHolder2{
        TextView tv1;
    }
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104

    mybase1.xml布局文件

    LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
              &lt;ImageView
                  android:id="@+id/imageView"
                  android:layout_marginTop="20dp"
                  android:layout_width="100dp"
                  android:layout_height="100dp"
                  android:background="@mipmap/ic_launcher"/&gt;
    
                  &lt;TextView
                      android:id="@+id/textView"
                      android:textColor="@color/colorAccent"
                      android:textSize="22dip"
                      android:singleLine="true"
                      android:ellipsize="marquee"
                      android:focusable="true"
                      android:focusableInTouchMode="true"
                      android:scrollHorizontally="true"
                      android:marqueeRepeatLimit="marquee_forever"
                      android:layout_centerHorizontal="true"
                      android:layout_centerVertical="true"
                      android:layout_marginTop="20dp"
    
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                       android:text="1111111111111"/&gt;
    

    </LinearLayout>

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    mybase2.xm布局文件l

    LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
                TextView
                     android:id="@+id/tv1"
                    android:ellipsize="marquee"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:singleLine="true"
                    android:layout_width="wrap_content"
                    android:textColor="#fff"
                    android:textSize="26sp"
                    android:layout_height="wrap_content" />
    </LinearLayout>
    
     
     
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    FragmnetOne.java

    package bawei.com.yuekaodemo1.fragment;
    

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.design.widget.TabLayout;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;

    import com.andy.library.ChannelActivity;
    import com.andy.library.ChannelBean;
    import com.google.gson.Gson;
    import com.google.gson.reflect.TypeToken;

    import java.lang.reflect.Type;
    import java.util.ArrayList;
    import java.util.List;

    import bawei.com.yuekaodemo1.R;

    import static com.andy.library.ChannelActivity.REQUEST_CODE;
    import static com.andy.library.ChannelActivity.RESULT_JSON_KEY;

    public class FragmnetOne extends Fragment {
    private TabLayout tabLayout;
    private Button button;
    private ViewPager viewPager;
    private View view;
    private String jsonStr="";
    private PagerAdapter pagerAdapter;
    ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
    private ArrayList<ChannelBean> channelBeanlist = new ArrayList<ChannelBean>();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragmentone, container, false);
    tabLayout = view.findViewById(R.id.tab);
    button = view.findViewById(R.id.but);
    viewPager = view.findViewById(R.id.viewpager);

        return view;
    }
    
    
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initview();
        initData();
    }
    private void initview() {
    
    
    
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                String jsonArray = gson.toJson(channelBeanlist);
                Intent intent = new Intent(getActivity(), ChannelActivity.class);
                intent.putExtra(RESULT_JSON_KEY, jsonArray);
                startActivityForResult(intent, REQUEST_CODE);
            }
        });
    
    }
    
    private void initData() {
        pagerAdapter=new PagerAdapter(getChildFragmentManager());
        viewPager.setAdapter( pagerAdapter);
        channelBeanlist=new ArrayList&lt;&gt;();
        channelBeanlist.add(new ChannelBean("新闻",true));
        channelBeanlist.add(new ChannelBean("视频",true));
        channelBeanlist.add(new ChannelBean("焦点",true));
        channelBeanlist.add(new ChannelBean("音乐",false));
        channelBeanlist.add(new ChannelBean("娱乐",false));
        channelBeanlist.add(new ChannelBean("首页",false));
        pagerAdapter.setData(isSelectPD(channelBeanlist));
        //联动
        tabLayout.setupWithViewPager(viewPager);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                String jsonArray = gson.toJson(channelBeanlist);
                Intent intent = new Intent(getActivity(), ChannelActivity.class);
                intent.putExtra(RESULT_JSON_KEY, jsonArray);
                startActivityForResult(intent, REQUEST_CODE);
            }
        });
    }
    public List&lt;ChannelBean&gt; isSelectPD(List&lt;ChannelBean&gt; list){
        List&lt;ChannelBean&gt; isok = new ArrayList&lt;&gt;();
        for (int i = 0;i&lt;list.size();i++){
            if(list.get(i).isSelect()){
                isok.add(list.get(i));
            }
        }
        return isok;
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE &amp;&amp; resultCode == ChannelActivity.RESULT_CODE) {
            //获取tab要展示的内容
            jsonStr = data.getStringExtra(RESULT_JSON_KEY);
            //清空之前的栏目
            tabLayout.removeAllTabs();
            Type type = new TypeToken&lt;ArrayList&lt;ChannelBean&gt;&gt;() {
            }.getType();
            channelBeanlist = new Gson().fromJson(jsonStr, type);
            //传给适配器
            pagerAdapter.setData(isSelectPD(channelBeanlist));
            tabLayout.setupWithViewPager(viewPager);
        }
    }
        private class PagerAdapter extends FragmentPagerAdapter {
            private List&lt;ChannelBean&gt; list = new ArrayList&lt;&gt;();
    
            public PagerAdapter(FragmentManager fm) {
                super(fm);
            }
    
            public void setData(List&lt;ChannelBean&gt; list) {
                this.list = list;
                notifyDataSetChanged();
            }
    
            @Override
            public Fragment getItem(int i) {
                switch (i) {
                    case 0:
                        return new Fragmnet1();
                    case 1:
                        return new Fragmnet2();
                    case 2:
                        return new Fragmnet3();
                    default:
                        return new Fragmnet3();
                }
            }
    
            @Override
            public int getCount() {
                return list.size();
            }
    
            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return list.get(position).getName();
            }
    
    }
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165

    fragmentone.xml

    <?xml version="1.0" encoding="utf-8"?>
    LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="#ff55"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
          &lt;LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"&gt;
    
    
              &lt;android.support.design.widget.TabLayout
                  android:id="@+id/tab"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"&gt;
    
              &lt;/android.support.design.widget.TabLayout&gt;
               &lt;Button
                   android:id="@+id/but"
                   android:layout_width="0dp"
                   android:layout_height="wrap_content"
                   android:layout_weight="1"
                   android:text="+"/&gt;
          &lt;/LinearLayout&gt;
    &lt;android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"&gt;
    
    &lt;/android.support.v4.view.ViewPager&gt;
    

    /LinearLayout>

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    fragmenttwo.xml

    LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="@mipmap/c"
        android:layout_height="match_parent">
        <com.recker.flybanner.FlyBanner
            android:id="@+id/banner"
            android:layout_width="match_parent"
            android:layout_height="450dp">
    
    &lt;/com.recker.flybanner.FlyBanner&gt;
    

    </LinearLayout>

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    FragmnetTwo.java

    package bawei.com.yuekaodemo1.fragment;
    

    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    import com.recker.flybanner.FlyBanner;

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

    import bawei.com.yuekaodemo1.R;

    public class FragmnetTwo extends Fragment {
    private FlyBanner flyBanner;
    //有地址是为了得到地址里面的图片地址(使用HiJson查看)
    private String path = “http://api.expoon.com/AppNews/getNewsList/type/1/p/1”;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragmenttwo, container, false);
        //获取资源ID
        flyBanner = view.findViewById(R.id.banner);
        return view;
    }
    
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    
        //网络的
        List&lt;String&gt; list=new ArrayList&lt;&gt;();
        list.add("http://f.expoon.com/sub/news/2016/01/21/887844_230x162_0.jpg");
        list.add("http://f.expoon.com/sub/news/2016/01/21/580828_230x162_0.jpg");
        list.add("http://f.expoon.com/sub/news/2016/01/21/745921_230x162_0.jpg");
        flyBanner.setImagesUrl(list);
    
        //本地图片
        List&lt;Integer&gt; lists = new ArrayList&lt;&gt;();
        lists.add(R.mipmap.b);
        lists.add(R.mipmap.c);
        lists.add(R.mipmap.d);
        flyBanner.setImages(lists);
    
    
    }
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    fragmentthree.xml

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

    >
    /RelativeLayout>

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    FragmnetThree.java

    package bawei.com.yuekaodemo1.fragment;
    

    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    import bawei.com.yuekaodemo1.R;

    public class FragmnetThree extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragmentthree, container, false);
        return view;
    }
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    fragment1.xml

    <?xml version="1.0" encoding="utf-8"?>
    LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="@mipmap/m"
        android:layout_height="match_parent">
          <com.handmark.pulltorefresh.library.PullToRefreshListView
              android:id="@+id/pv"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    
      &lt;/com.handmark.pulltorefresh.library.PullToRefreshListView&gt;
    

    </LinearLayout

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    Fragmnet1.java

    package bawei.com.yuekaodemo1.fragment;
    import android.content.ContentValues;
    import android.database.Cursor;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ListView;
    import android.widget.Toast;
    

    import com.google.gson.Gson;
    import com.handmark.pulltorefresh.library.PullToRefreshBase;
    import com.handmark.pulltorefresh.library.PullToRefreshListView;

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

    import bawei.com.yuekaodemo1.R;
    import bawei.com.yuekaodemo1.adapter.MyAdapter;
    import bawei.com.yuekaodemo1.bean.Goods;
    import bawei.com.yuekaodemo1.dao.UserDao;
    import bawei.com.yuekaodemo1.netword.NetWord;

    public class Fragmnet1 extends Fragment {
    private String url =“http://api.expoon.com/AppNews/getNewsList/type/1/p/1”;
    private PullToRefreshListView pullToRefreshListView;
    private int page;
    List<Goods.DataBean> list = new ArrayList<>();
    private UserDao dao;
    private MyAdapter myAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1, container, false);
    
        dao = new UserDao(getActivity());
    
        pullToRefreshListView = view.findViewById(R.id.pv);
        return view;
    }
    
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //初始化
        initpv();
        //请求网络
        data(page);
         
    }
    
    private void data(int page) {
        //调用网络判断
        boolean connection = NetWord.isconnection(getActivity());
        if(connection){//有网
            String urls=url+page;
            new MyTask().execute(urls);
        }else {
            //没有网
            Toast.makeText(getActivity(),"请检查网络再试",Toast.LENGTH_LONG).show();
            //根据url地址。从数据库中获取数据
            String jsonData = dao.queryData(url);//调取数据库查询的方法
            if (!"".equals(jsonData)){
                //解析gson
                Gson gson=new Gson();
                Goods myBean = gson.fromJson(jsonData, Goods.class);
                List&lt;Goods.DataBean&gt; data = myBean.getData();
                list.addAll(data);
                getAdapter();
                //关闭上下拉刷新
                pullToRefreshListView.onRefreshComplete();
            }
        }
    }
    class MyTask extends AsyncTask&lt;String ,Void,String&gt; {
    
        @Override
        protected String doInBackground(String... strings) {
            String str="";
            try {
                URL url=new URL(strings[0]);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setReadTimeout(1000);
                urlConnection.setConnectTimeout(1000);
                urlConnection.setRequestMethod("GET");
                if(urlConnection.getResponseCode()==200){
                    InputStream inputStream = urlConnection.getInputStream();
                    str = getdata(inputStream);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return str;
        }
    
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            //将请求下来的json数据缓存在数据库中
            dao.add(url,s);
            Gson gson=new Gson();
            Goods myBean = gson.fromJson(s, Goods.class);
            List&lt;Goods.DataBean&gt; data = myBean.getData();
            list.addAll(data);
             getAdapter();
            //关闭上下拉刷新
            pullToRefreshListView.onRefreshComplete();
        }
    }
    private void initpv() {
        
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2&lt;ListView&gt;() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase&lt;ListView&gt; pullToRefreshBase) {
                list.clear();
                data(page);
            }
    
            @Override
            public void onPullUpToRefresh(PullToRefreshBase&lt;ListView&gt; pullToRefreshBase) {
                page++;
                data(page);
    
            }
        });
    }
    public void getAdapter(){
        myAdapter= new MyAdapter(list,getActivity());
        pullToRefreshListView.setAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();
    }
    public String getdata(InputStream stream){
    
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        String  tamp ;
        try {
            while ((tamp=reader.readLine())!=null){
                stringBuilder.append(tamp);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159

    fragment2.xml

    <?xml version="1.0" encoding="utf-8"?>
    RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="@mipmap/b"
        android:layout_height="match_parent">
        <com.bwie.xlistviewlibrary.view.XListView
            android:id="@+id/xlv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        </com.bwie.xlistviewlibrary.view.XListView>
    

    </RelativeLayout>

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    Fragmnet2.java

    package bawei.com.yuekaodemo1.fragment;
    

    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.bwie.xlistviewlibrary.utils.NetWordUtils;
    import com.bwie.xlistviewlibrary.view.XListView;
    import com.google.gson.Gson;
    import com.nostra13.universalimageloader.core.ImageLoader;

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

    import bawei.com.yuekaodemo1.R;
    import bawei.com.yuekaodemo1.bean.Goods;
    import bawei.com.yuekaodemo1.dao.UserDao;
    import bawei.com.yuekaodemo1.netword.NetWord;

    public class Fragmnet2 extends Fragment {
    String url = “http://api.expoon.com/AppNews/getNewsList/type/1/p/1”;
    int page;
    private XListView xListView;
    List<Goods.DataBean> list = new ArrayList<>(); //大集合
    private MAdapter mAdapter;
    private ImageLoader imageLoaderInstances;
    private UserDao dao;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment2, container, false);
    xListView = view.findViewById(R.id.xlv);
    dao = new UserDao(getActivity());
    xListView.setPullLoadEnable(true);
    imageLoaderInstances = ImageLoader.getInstance();
    mAdapter = new MAdapter();
    xListView.setAdapter(mAdapter);
    initData(page); //请求网络数据

        //设置上下拉的逻辑
        xListView.setXListViewListener(new XListView.IXListViewListener() {
            //下拉刷新
            @Override
            public void onRefresh() {
                page = 0;
                initData(page);
    
            }
    
            //上拉加载更多;
            @Override
            public void onLoadMore() {
                page++;
                initData(page);
            }
        });
        return view;
    }
    
    private void initData(int page) {
    

    // String mUrl = baseUrl + page;
    // new MAsycnTask().execute(mUrl);
    //调用网络判断
    boolean connection = NetWord.isconnection(getActivity());
    if(connection){//有网
    String urls=url+page;
    new MAsycnTask().execute(urls);
    }else {
    //没有网
    Toast.makeText(getActivity(),“请检查网络再试”,Toast.LENGTH_LONG).show();
    //根据url地址。从数据库中获取数据
    String jsonData = dao.queryData(url);//调取数据库查询的方法
    if (!"".equals(jsonData)){
    //解析gson
    Gson gson=new Gson();
    Goods myBean = gson.fromJson(jsonData, Goods.class);
    List<Goods.DataBean> data = myBean.getData();
    list.addAll(data);

            }
        }
    
    }
    
    
    private class MAdapter extends BaseAdapter {
        @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) {
            View viewItem = View.inflate(getActivity(), R.layout.mybase1, null);
            TextView textView = (TextView) viewItem.findViewById(R.id.textView);
            ImageView imageView = (ImageView) viewItem.findViewById(R.id.imageView);
            textView.setText(list.get(i).getNews_title());
            imageLoaderInstances.displayImage(list.get(i).getPic_url(), imageView);
    
    
            return viewItem;
        }
    }
    
    
    class  MAsycnTask extends AsyncTask&lt;String,Void,String&gt; {
    
        @Override
        protected String doInBackground(String... strings) {
            return NetWordUtils.getNetjson(strings[0]);
        }
    
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            Goods goods = gson.fromJson(s, Goods.class);
            List&lt;Goods.DataBean&gt; data = goods.getData();
    
            list.addAll(data);
            mAdapter.notifyDataSetChanged();
            uiComplete();//让刷新头和刷新底部隐藏;
    
    
        }
    }
    
    private void uiComplete() {
        xListView.setRefreshTime("刚刚");
        xListView.stopRefresh();//隐藏刷新头部
        xListView.stopLoadMore();//隐藏刷新脚部
    
    
    }
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156

    fragment3.xml

    <?xml version="1.0" encoding="utf-8"?>
        LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:background="@mipmap/d"
            android:layout_height="match_parent">
            <com.recker.flybanner.FlyBanner
                android:id="@+id/banner"
                android:layout_width="match_parent"
                android:layout_height="250dp">
    
        &lt;/com.recker.flybanner.FlyBanner&gt;
    
    &lt;/LinearLayout&gt;
    

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    Fragmnet3.java

    package bawei.com.yuekaodemo1.fragment;
    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    

    import com.recker.flybanner.FlyBanner;

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

    import bawei.com.yuekaodemo1.R;

    public class Fragmnet3 extends Fragment {
    private FlyBanner flyBanner;
    //有地址是为了得到地址里面的图片地址(使用HiJson查看)
    private String path = “http://api.expoon.com/AppNews/getNewsList/type/1/p/1”;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment3, container, false);

        //获取资源ID
        flyBanner = view.findViewById(R.id.banner);
        return view;
    }
    
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    
        //网络的
        List&lt;String&gt; list=new ArrayList&lt;&gt;();
        list.add("http://f.expoon.com/sub/news/2016/01/19/650175_230x162_0.jpg");
        list.add("http://f.expoon.com/sub/news/2016/01/21/580828_230x162_0.jpg");
        list.add("http://f.expoon.com/sub/news/2016/01/21/745921_230x162_0.jpg");
        flyBanner.setImagesUrl(list);
    
        //本地图片
    

    // List<Integer> lists = new ArrayList<>();
    // lists.add(R.mipmap.b);
    // lists.add(R.mipmap.c);
    // lists.add(R.mipmap.d);
    // flyBanner.setImages(lists);

    }
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    需要加入的权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
        <uses-permission android:name="android.permission.INTERNET"></uses-permission>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    最后不要忘记:
      android:name=".app.Mapp"
    

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

            </div>
    					<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-7b4cdcb592.css" rel="stylesheet">
                </div>
    
    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值