public class MainActivity extends AppCompatActivity { private XListView xlv; List<Da.StoriesBean> list; private ViewPager vp3; private List<ImageView> viewList; private Da da; private View view; // 线程 Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); } }; private BeanDao dao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 找控件 xlv = (XListView) findViewById(R.id.xlv3); list = new ArrayList<Da.StoriesBean>(); // 实例化dao dao = new BeanDao(this); // 初始化数据 initData(); // 找控件并加载头部布局 view = View.inflate(MainActivity.this, R.layout.vp3_item,null); xlv.addHeaderView(view); // 设置允许下拉 上拉 xlv.setPullRefreshEnable(true); xlv.setPullLoadEnable(true); // 监听事件 xlv.setXListViewListener(new XListView.IXListViewListener() { @Override public void onRefresh() { handler.postDelayed(new Runnable() { @Override public void run() { initData(); xlv.stopRefresh(); } },3000); } @Override public void onLoadMore() { handler.postDelayed(new Runnable() { @Override public void run() { initData(); xlv.stopLoadMore(); } },3000); } }); } private void initData() { // 异步任务 new AsyncTask<String,Integer,String>(){ @Override protected void onPostExecute(String s) { MyAdapter adapter = new MyAdapter(); xlv.setAdapter(adapter); // 把返回的view赋值给初始化数据的方法 view = initViewPager(); // 添加数据库 dao.addSb(da.getStories()); dao.addTb(da.getTop_stories()); super.onPostExecute(s); } @Override protected String doInBackground(String... strings) { getData(); ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(MainActivity.this); ImageLoader.getInstance().init(configuration); return null; } }.execute(); } private View initViewPager() { vp3 = view.findViewById(R.id.vp3); getViewData(); MyPagerAdaPter myPagerAdaPter = new MyPagerAdaPter(); vp3.setAdapter(myPagerAdaPter); final TextView vp3_tv = view.findViewById(R.id.vp3_tv); if(da != null){ // 设置ViewPager上面的文字 vp3_tv.setText(da.getTop_stories().get(0).getTitle()); } vp3.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int i, float positionOffset, int positionOffsetPixels) { // 文字跟着图片变换 vp3_tv.setText(da.getTop_stories().get(i).getTitle()); } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { } }); return view; } private void getViewData() { viewList = new ArrayList<ImageView>(); if(da != null){ // 如果da不等于空 遍历Top 循环添加图片 for (int i = 0; i <da.getTop_stories().size() ; i++) { ImageView iv = new ImageView(MainActivity.this); iv.setScaleType(ImageView.ScaleType.FIT_XY); ImageLoader.getInstance().displayImage(da.getTop_stories().get(i).getImage(),iv); viewList.add(iv); } } } private void getData() { try { // 网络请求数据 URL url = new URL("https:/"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); int code = conn.getResponseCode(); if(code == 200){ Gson gson = new Gson(); da = gson.fromJson(new InputStreamReader(conn.getInputStream(),"UTF-8"),Da.class); list = da.getStories(); } } catch (Exception e) { e.printStackTrace(); } } class MyAdapter 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) { ViewHolder holder; if(view == null){ holder = new ViewHolder(); view = View.inflate(MainActivity.this,R.layout.xlv_item,null); holder.xlv3_image = view.findViewById(R.id.xlv3_image); holder.xlv3_title = view.findViewById(R.id.xlv3_title); view.setTag(holder); }else{ holder = (ViewHolder) view.getTag(); } holder.xlv3_title.setText(list.get(i).getTitle()); ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0),holder.xlv3_image); return view; } } class ViewHolder{ ImageView xlv3_image; TextView xlv3_title; } class MyPagerAdaPter extends PagerAdapter{ @Override public int getCount() { return da.getTop_stories().size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(viewList.get(position)); return viewList.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { // super.destroyItem(container, position, object); container.removeView((View) object); } } }
XlistView 加载头部ViewPager 网络请求数据 存入数据库
最新推荐文章于 2019-06-11 19:32:58 发布