咨询_xListView,多条目,滑动,异步任务类,圆角,本地缓存,数据库

MainActivity

package com.example.administrator.myapplication;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;

import com.example.administrator.frag.Frag_01;
import com.example.administrator.frag.Frag_02;

import java.util.ArrayList;

public class MainActivity extends FragmentActivity {

    private ViewPager viewPager;
    private RadioGroup radioGroup;
    private ArrayList<Fragment> list = new ArrayList<Fragment>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.ViewPager1);
        radioGroup = findViewById(R.id.radioGroup1);
        list.add(new Frag_01());
        list.add(new Frag_02());
        radioGroup.check(radioGroup.getChildAt(0).getId());
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                radioGroup.check(radioGroup.getChildAt(position).getId());

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.radio0: viewPager.setCurrentItem(0);break;
                    case R.id.radio1: viewPager.setCurrentItem(1);break;
                }
            }
        });
    }
}

Frag_01

package com.example.administrator.frag;

import android.content.ContentValues;
import android.database.Cursor;
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.Toast;

import com.bwie.xlistviewlibrary.utils.NetWordUtils;
import com.bwie.xlistviewlibrary.view.XListView;
import com.example.administrator.adapter.MyAdapter;
import com.example.administrator.bean.Json;
import com.example.administrator.myapplication.R;
import com.example.administrator.sql.Dao;
import com.example.administrator.task.MyAsyncTask;
import com.google.gson.Gson;

import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

public class Frag_01 extends Fragment {
    private String urlString="http://api.expoon.com/AppNews/getNewsList/type/1/p/";
    private XListView xListView;
    private int page;
    private ArrayList<Json.DataBean> list = new ArrayList<Json.DataBean>();
    private MyAdapter adapter;
    private Dao dao;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag1, container, false);
        xListView = view.findViewById(R.id.xlistview1);
        dao = new Dao(getActivity());
        xListView.setPullLoadEnable(true);
        adapter = new MyAdapter(list, getActivity());
        xListView.setAdapter(adapter);
        initData(page);
        xListView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                list.clear();
                initData(page);
            }
            @Override
            public void onLoadMore() {
                page++;
                initData(page);
            }
        });
        return view;
    }

    private void initData(int page) {
        boolean urlConnection = NetWordUtils.getUrlConnection(getActivity());
        if (urlConnection) {
          String s = urlString + page;
            new MyAsyncTask(list,adapter,xListView,dao,getActivity()).execute(s);
        }else {
            Toast.makeText(getActivity(), "没网", Toast.LENGTH_SHORT).show();
            Cursor a2 = dao.query("A2", null, null, null, null, null, null);
            if (a2.moveToFirst()) {
                do {
                    String news_id = a2.getString(a2.getColumnIndex("news_id"));
                    String news_title = a2.getString(a2.getColumnIndex("news_title"));
                    String news_summary = a2.getString(a2.getColumnIndex("news_summary"));
                    String pic_url = a2.getString(a2.getColumnIndex("pic_url"));
                    list.add(new Json.DataBean(news_id,news_title,news_summary,pic_url));
                }while (a2.moveToNext());
            }
            a2.close();
            adapter = new MyAdapter(list, getActivity());
            xListView.setAdapter(adapter);
        }
    }
}

MyAdapter

package com.example.administrator.adapter;

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

import com.example.administrator.bean.Json;
import com.example.administrator.holder.Holder0;
import com.example.administrator.myapplication.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;

public class MyAdapter extends BaseAdapter {
    private ArrayList<Json.DataBean> list = new ArrayList<Json.DataBean>();
    private Context context;
    private Holder0 holder0;

    public MyAdapter(ArrayList<Json.DataBean> 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 getItemViewType(int position) {
        return position % 2;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int itemViewType = getItemViewType(position);
        switch (itemViewType){
            case  0 :
                holder0 = new Holder0();
                if (convertView == null) {
                    convertView = View.inflate(context,R.layout.item,null);
                    holder0.imageView1 = convertView.findViewById(R.id.image1);
                    holder0.textView1 = convertView.findViewById(R.id.text1);
                    holder0.textView2 = convertView.findViewById(R.id.text2);
                    convertView.setTag(holder0);
                }else {
                    holder0 = (Holder0) convertView.getTag();
                }
                ImageLoader instance = ImageLoader.getInstance();
                DisplayImageOptions options = new DisplayImageOptions.Builder()
                       .showImageForEmptyUri(R.mipmap.ic_launcher)
                        .showImageOnFail(R.mipmap.ic_launcher)
                        .showImageOnLoading(R.mipmap.ic_launcher)
                        .displayer(new RoundedBitmapDisplayer(360))
                        .build();
                instance.displayImage(list.get(position).getPic_url(), holder0.imageView1,options);
                holder0.textView1.setText(list.get(position).getNews_summary());
                holder0.textView2.setText(list.get(position).getNews_title());
                break;
            case  1 :
                if (convertView == null) {
                    convertView = View.inflate(context,R.layout.item,null);
                    holder0.textView1 = convertView.findViewById(R.id.text1);
                    holder0.textView2 = convertView.findViewById(R.id.text2);
                    convertView.setTag(holder0);
                }else {
                    holder0 = (Holder0) convertView.getTag();
                }
                holder0.textView1.setText(list.get(position).getNews_summary());
                holder0.textView2.setText(list.get(position).getNews_title());
                break;
        }
        return convertView;
    }
}

MyAsyncTask

package com.example.administrator.task;

import android.content.ContentValues;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

import com.bwie.xlistviewlibrary.utils.NetWordUtils;
import com.bwie.xlistviewlibrary.view.XListView;
import com.example.administrator.adapter.MyAdapter;
import com.example.administrator.bean.Json;
import com.example.administrator.sql.Dao;
import com.google.gson.Gson;

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

public class MyAsyncTask extends AsyncTask<String,Void,String>{
    private ArrayList<Json.DataBean> list = new ArrayList<Json.DataBean>();
    private MyAdapter adapter;
    private XListView xListView;
    private Dao dao;
    private String news_summary;
    private String news_title;
    private String pic_url;
    private String news_id;
    private Context context;

    public MyAsyncTask(ArrayList<Json.DataBean> list, MyAdapter adapter, XListView xListView, Dao dao,Context context) {
        this.list = list;
        this.adapter = adapter;
        this.xListView = xListView;
        this.dao = dao;
        this.context=context;
    }

    @Override
    protected String doInBackground(String... strings) {
        return NetWordUtils.getNetjson(strings[0]);
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Gson gson = new Gson();
        Json json = gson.fromJson(s, Json.class);
        List<Json.DataBean> data = json.getData();
        for (int i = 0; i < data.size(); i++) {
            news_title = data.get(i).getNews_title();
            pic_url = data.get(i).getPic_url();
            news_id = data.get(i).getNews_id();
            news_summary = data.get(i).getNews_summary();
            add();
        }

        list.addAll(data);
        adapter.notifyDataSetChanged();
        init();
    }

    private void add() {
        dao.delete("A2","news_title=?",new String[]{news_title});
        ContentValues values = new ContentValues();
        values.put("pic_url",pic_url);
        values.put("news_summary",news_summary);
        values.put("news_id",news_id);
        values.put("news_title",news_title);
        long a2 = dao.add("A2", null, values);
        Toast.makeText(context, "添加成功", Toast.LENGTH_SHORT).show();
    }

    private void init() {

        xListView.setRefreshTime("刚刚");
        xListView.stopRefresh();
        xListView.stopLoadMore();
    }
}

MyApp

package com.example.administrator.app;

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

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.io.File;

public class MyApp extends Application {

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值