Fragment第二次加载没有数据
2019獨角獸企業重金招聘Python工程師標準>>>
package com.ifonyo.door.fragment;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
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.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ifonyo.door.R;
import com.ifonyo.door.TestCallback;
import com.ifonyo.door.activity.CommunityListActivity;
import com.ifonyo.door.activity.DoorActivity;
import com.ifonyo.door.activity.StopCarActivity;
import com.ifonyo.door.bean.Announcement_List;
import com.ifonyo.door.bean.MessagePage;
import com.ifonyo.door.bean.ViewPagerHome;
import com.ifonyo.door.http.Api;
import com.ifonyo.door.utils.PrefUtils;
import com.squareup.picasso.Picasso;
import java.io.IOException;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
?* Created by Js on 2016/11/9.
?* 主頁的fragment
?*/
public class HomeFragment extends Fragment implements TestCallback {
? ? private View headerview;
? ? private Announcement_List announcementlist;
? ? @Override
? ? public void test(String name) {
? ? ? ? System.out.println(name);
? ? }
? ? private ImageButton ib_pulldown;
? ? private static final int MSG_ANN_ONE = 0x2005;
? ? private Button bt_door;
? ? private View view;
? ? private PullToRefreshListView refreshListView;
? ? private Button bt_stopcar;
? ? private LinearLayout ll_choose_community;
? ? private OkHttpClient okHttpClient;
? ? private ViewPager vp_home;
? ? private static final int MSG_UPDATE_WAY_TWO = 0x2002; ?// 更新文本 方式二用的常量
? ? private static final int MSG_UPDATE_WAY_ONE = 0x2001;
? ? private static final int MSG_HOME_ONE = 0x2003;
? ? private static final int MSG_VP_WAY_TWO = 0x2004;
? ? private Handler mHandler;
? ? private int page = 0;
? ? private int size = 10;
? ? private MessagePage messagePage;
? ? private TextView tv_home;
? ? private ListView listview;
? ? private TextView tv_announcement;
? ? private ViewPagerHome ceshi1;
? ? @Nullable
? ? @Override
? ? public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
? ? ? ? if(view == null){
? ? ? ? ? ? view = View.inflate(getActivity(), R.layout.fragment_home,null);
? ? ? ? }else{
? ? ? ? ? ? ViewGroup parent ?= (ViewGroup) view.getParent();
? ? ? ? ? ? if(parent != null){
? ? ? ? ? ? ? ? parent.removeView(view);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? mHandler = new Handler(){
? ? ? ? ? ? @Override
? ? ? ? ? ? public void handleMessage(Message msg) {
? ? ? ? ? ? ? ? switch (msg.what){
? ? ? ? ? ? ? ? ? ? case MSG_UPDATE_WAY_ONE:
? ? ? ? ? ? ? ? ? ? ? ? refreshListView.setAdapter(new MessageListAdapter());
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case MSG_UPDATE_WAY_TWO:
? ? ? ? ? ? ? ? ? ? ? ? new MessageListAdapter().notifyDataSetChanged();
? ? ? ? ? ? ? ? ? ? ? ? //完成刷新
? ? ? ? ? ? ? ? ? ? ? ? refreshListView.onRefreshComplete();
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case MSG_HOME_ONE:
? ? ? ? ? ? ? ? ? ? ? ? page++;
? ? ? ? ? ? ? ? ? ? ? ? loadListView(page, size);
? ? ? ? ? ? ? ? ? ? ? ? refreshListView.getRefreshableView().setSelection(new MessageListAdapter().getCount());
? ? ? ? ? ? ? ? ? ? ? ? new MessageListAdapter().notifyDataSetChanged();
? ? ? ? ? ? ? ? ? ? ? ? refreshListView.onRefreshComplete();
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case MSG_VP_WAY_TWO:
? ? ? ? ? ? ? ? ? ? ? ? vp_home.setAdapter(new MypageAdapter());
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? case MSG_ANN_ONE:
? ? ? ? ? ? ? ? ? ? ? ? tv_announcement.setText(announcementlist.getContent().get(0).getText());
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? initView();
? ? ? ? initData();
? ? ? ? setRefreshMode();
? ? ? ? refreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onRefresh(PullToRefreshBase<ListView> refreshView) {
? ? ? ? ? ? ? ? if (refreshListView.isHeaderShown()) {
? ? ? ? ? ? ? ? ? ? // 直接調用
? ? ? ? ? ? ? ? ? ? new Thread() {
? ? ? ? ? ? ? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? SystemClock.sleep(2000);
? ? ? ? ? ? ? ? ? ? ? ? ? ? Message msg = Message.obtain();
? ? ? ? ? ? ? ? ? ? ? ? ? ? msg.what = MSG_UPDATE_WAY_TWO;
? ? ? ? ? ? ? ? ? ? ? ? ? ? mHandler.sendMessage(msg);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }.start();
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? new Thread() {
? ? ? ? ? ? ? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? SystemClock.sleep(2000);
? ? ? ? ? ? ? ? ? ? ? ? ? ? Message msg = Message.obtain();
? ? ? ? ? ? ? ? ? ? ? ? ? ? msg.what = MSG_HOME_ONE;
? ? ? ? ? ? ? ? ? ? ? ? ? ? mHandler.sendMessage(msg);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }.start();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? listview = refreshListView.getRefreshableView();
? ? ? ? setListView();
? ? ? ? loadListView(page,size);
? ? ? ? loadAnnouncement();
? ? ? ? return view;
? ? }
? ? private void initData() {
? ? ? ? ll_choose_community.setOnClickListener(new CommunityList());
? ? ? ? String communityname = PrefUtils.getString(getActivity(),"communityname",null);
? ? ? ? if(communityname == null){
? ? ? ? ? ? tv_home.setText("請選擇小區");
? ? ? ? }else{
? ? ? ? ? ? tv_home.setText(communityname);
? ? ? ? }
? ? }
? ? private void setListView() {
? ? ? ? listview.setDivider(new ColorDrawable(Color.BLACK));
? ? ? ? listview.setDividerHeight(1);
? ? ? ? if(headerview == null){
? ? ? ? ? ? headerview = View.inflate(getActivity(),R.layout.listview_addheadview,null);
? ? ? ? ? ? listview.addHeaderView(headerview);
? ? ? ? }
? ? ? ? vp_home = (ViewPager) headerview.findViewById(R.id.vp_home);
? ? ? ? bt_stopcar = (Button) headerview.findViewById(R.id.bt_stopcar);
? ? ? ? bt_door = (Button) headerview.findViewById(R.id.bt_door);
? ? ? ? tv_announcement = (TextView) headerview.findViewById(R.id.tv_announcement);
? ? ? ? initHeadData();
? ? ? ? loadData();
? ? }
? ? private void initHeadData() {
? ? ? ? bt_stopcar.setOnClickListener(new StopCarOnClick());
? ? ? ? bt_door.setOnClickListener(new DoorOnClick());
? ? }
? ? private void loadData() {
? ? ? ? String url = Api.URL_HOME_VP;
? ? ? ? OkHttpClient client = new OkHttpClient();
? ? ? ? RequestBody requestBody = new FormBody.Builder()
? ? ? ? ? ? ? ? .add("type","0")
? ? ? ? ? ? ? ? .build();
? ? ? ? Request request = new Request.Builder()
? ? ? ? ? ? ? ? .url(url)
? ? ? ? ? ? ? ? .post(requestBody)
? ? ? ? ? ? ? ? .build();
? ? ? ? client.newCall(request).enqueue(new Callback() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onFailure(Call call, IOException e) {
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onResponse(Call call, Response response) throws IOException {
? ? ? ? ? ? ? ? Gson gson = new Gson();
? ? ? ? ? ? ? ? ceshi1 = gson.fromJson(response.body().string(), ViewPagerHome.class);
? ? ? ? ? ? ? ? Message msg =Message.obtain();
? ? ? ? ? ? ? ? msg.what= MSG_VP_WAY_TWO;
? ? ? ? ? ? ? ? mHandler.sendMessage(msg);
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? private void setRefreshMode() {
? ? ? ? refreshListView.setMode(PullToRefreshBase.Mode.BOTH);// 設置既可以上拉也可以下拉
? ? }
? ? /**
? ? ?* 加載公告
? ? ?*/
? ? private void loadAnnouncement() {
? ? ? ? String url1 = Api.URL_HOME_Announcement_LIST;
? ? ? ? OkHttpClient client1 = ?new OkHttpClient();
? ? ? ? final RequestBody requestBody = new FormBody.Builder()
? ? ? ? ? ? ? ? .add("communityId","1")
? ? ? ? ? ? ? ? .build();
? ? ? ? Request request = new Request.Builder()
? ? ? ? ? ? ? ? .url(url1)
? ? ? ? ? ? ? ? .post(requestBody)
? ? ? ? ? ? ? ? .build();
? ? ? ? client1.newCall(request).enqueue(new Callback() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onFailure(Call call, IOException e) {
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onResponse(Call call, Response response) throws IOException {
? ? ? ? ? ? ? ? Gson gson = new Gson();
? ? ? ? ? ? ? ? announcementlist = gson.fromJson(response.body().string(),Announcement_List.class);
? ? ? ? ? ? ? ? Message msg = Message.obtain();
? ? ? ? ? ? ? ? msg.what = MSG_ANN_ONE;
? ? ? ? ? ? ? ? mHandler.sendMessage(msg);
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? /**
? ? ?* 加載資訊
? ? ?* @param page
? ? ?* @param size
? ? ?*/
? ? private void loadListView(int page, int size) {
? ? ? ? String url = Api.URL_HOME_MESSAGEPAGE;
? ? ? ? OkHttpClient client1 = ?new OkHttpClient();
? ? ? ? final RequestBody requestBody = new FormBody.Builder()
? ? ? ? ? ? ? ? .add("page",page+"")
? ? ? ? ? ? ? ? .add("size",size+"")
? ? ? ? ? ? ? ? .build();
? ? ? ? Request request = new Request.Builder()
? ? ? ? ? ? ? ? .url(url)
? ? ? ? ? ? ? ? .post(requestBody)
? ? ? ? ? ? ? ? .build();
? ? ? ? client1.newCall(request).enqueue(new Callback() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onFailure(Call call, IOException e) {
? ? ? ? ? ? }
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onResponse(Call call, Response response) throws IOException {
? ? ? ? ? ? ? ? Gson gson = new Gson();
? ? ? ? ? ? ? ? if(messagePage == null){
? ? ? ? ? ? ? ? ? ? messagePage = gson.fromJson(response.body().string(),MessagePage.class);
? ? ? ? ? ? ? ? ? ? Message msg = Message.obtain();
? ? ? ? ? ? ? ? ? ? msg.what = MSG_UPDATE_WAY_ONE;
? ? ? ? ? ? ? ? ? ? mHandler.sendMessage(msg);
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? List<MessagePage.ContentBean> newContent = messagePage.getContent();
? ? ? ? ? ? ? ? ? ? newContent.addAll(gson.fromJson(response.body().string(), MessagePage.class).getContent());
? ? ? ? ? ? ? ? ? ? messagePage.setContent(newContent);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? /**
? ? ?* 初始化布局
? ? ?*/
? ? private void initView() {
? ? ? ? tv_home = (TextView) view.findViewById(R.id.tv_home_xiaoqu);
? ? ? ? refreshListView = (PullToRefreshListView)view.findViewById(R.id.lv_list_home);
? ? ? ? ll_choose_community = (LinearLayout) view.findViewById(R.id.ll_choose_community);
? ? ? ? okHttpClient = new OkHttpClient();
? ? }
? ? class MessageListAdapter extends BaseAdapter{
? ? ? ? @Override
? ? ? ? public int getCount() {
? ? ? ? ? ? return messagePage.getContent().size();
? ? ? ? }
? ? ? ? @Override
? ? ? ? public Object getItem(int position) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? @Override
? ? ? ? public long getItemId(int position) {
? ? ? ? ? ? return 0;
? ? ? ? }
? ? ? ? @Override
? ? ? ? public View getView(int position, View convertView, ViewGroup parent) {
? ? ? ? ? ? ViewHolder holder;
? ? ? ? ? ? if(convertView == null){
? ? ? ? ? ? ? ? convertView = View.inflate(getActivity(),R.layout.item_messagelist,null);
? ? ? ? ? ? ? ? holder = new ViewHolder();
? ? ? ? ? ? ? ? holder.iv_messagelist = (ImageView) convertView.findViewById(R.id.iv_messagelist);
? ? ? ? ? ? ? ? holder.tv_xinxi = (TextView) convertView.findViewById(R.id.tv_xinxi);
? ? ? ? ? ? ? ? holder.tv_zixun = (TextView) convertView.findViewById(R.id.tv_zixun);
? ? ? ? ? ? ? ? convertView.setTag(holder);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? holder = (ViewHolder) convertView.getTag();
? ? ? ? ? ? }
? ? ? ? ? ? Picasso.with(getActivity()).load(messagePage.getContent().get(position).getImg()).into(holder.iv_messagelist);
? ? ? ? ? ? holder.tv_zixun.setText(messagePage.getContent().get(position).getText());
? ? ? ? ? ? holder.tv_xinxi.setText(messagePage.getContent().get(position).getDate()+"");
? ? ? ? ? ? return convertView;
? ? ? ? }
? ? }
? ? class ViewHolder{
? ? ? ? private ImageView iv_messagelist;
? ? ? ? private TextView tv_zixun;
? ? ? ? private TextView tv_xinxi;
? ? }
? ? /**
? ? ?* 輪播圖
? ? ?*/
? ? class MypageAdapter ?extends PagerAdapter {
? ? ? ? @Override
? ? ? ? public int getCount() {
? ? ? ? ? ? return Integer.MAX_VALUE;
? ? ? ? }
? ? ? ? @Override
? ? ? ? public boolean isViewFromObject(View view, Object object) {
? ? ? ? ? ? return object == view;
? ? ? ? }
? ? ? ? @Override
? ? ? ? public void destroyItem(ViewGroup container, int position, Object object) {
? ? ? ? ? ? container.removeView((View) object);
? ? ? ? }
? ? ? ? @Override
? ? ? ? public Object instantiateItem(ViewGroup container, int position) {
? ? ? ? ? ? ImageView imageView = new ImageView(getActivity());
? ? ? ? ? ? imageView.setScaleType(ImageView.ScaleType.FIT_XY);
? ? ? ? ? ? Picasso.with(getActivity()).load(ceshi1.getContent().get(position%ceshi1.getContent().size()).getImg()).into(imageView);
? ? ? ? ? ? container.addView(imageView);
? ? ? ? ? ? return imageView;
? ? ? ? }
? ? }
? ? /**
? ? ?* 小區列表
? ? ?*/
? ? class CommunityList implements View.OnClickListener{
? ? ? ? @Override
? ? ? ? public void onClick(View v) {
? ? ? ? ? ? Intent intent = new Intent(getActivity(), CommunityListActivity.class);
? ? ? ? ? ? startActivity(intent);
? ? ? ? }
? ? }
? ? /**
? ? ?* 停車
? ? ?*/
? ? class StopCarOnClick implements View.OnClickListener{
? ? ? ? @Override
? ? ? ? public void onClick(View v) {
? ? ? ? ? ? Intent intent2 = new Intent(getActivity(), StopCarActivity.class);
? ? ? ? ? ? startActivity(intent2);
? ? ? ? }
? ? }
? ? /**
? ? ?* 門禁
? ? ?*/
? ? class DoorOnClick implements View.OnClickListener{
? ? ? ? @Override
? ? ? ? public void onClick(View v) {
? ? ? ? ? ? Intent intent3 = new Intent(getActivity(), DoorActivity.class);
? ? ? ? ? ? startActivity(intent3);
? ? ? ? }
? ? }
}
轉載于:https://my.oschina.net/handlermsg/blog/828302
總結
以上是生活随笔為你收集整理的Fragment第二次加载没有数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库三才阵——天
- 下一篇: 转 openssl 建立服务器证书