qxtx007
阿昱
采纳率100%
2016-06-21 02:00

ViewPager+Fragment+ListView启动后第一个页面是空白,怎么改进解决?

已采纳

ViewPager+Fragment(4个)+ListView启动后第一个页面是空白。
首先说明的是当listview里面的adapter不用callback的方法获取json的时候,一切都是正常的。
当4个fragment中只有一个listview的时候,用callback方法也是正常的,但是两个fragement中都有listview的时候,比如fragment1 & fragment2中各有一个listview,然后每个都callback就不行了,但是滑动一下,有时就可以了。
先说目录结构:MainActivity ,fragmentone,fragmentone2...
上代码:
package com.example.viewpagerdemo;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import com.example.viewpagerdemo.myadapter.MyFragmentPagerAdapter;

public class MainActivity extends FragmentActivity implements OnClickListener, OnPageChangeListener {

private ViewPager mViewPager;
private Button bt_list1, bt_list2, bt_list3, bt_list4;
private ArrayList<Fragment> list = null;
private Context mContext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.mainwin);
    mContext=MainActivity.this;
    initView();
    initEvent();
}

private void initView() {

    mViewPager = (ViewPager) findViewById(R.id.vpager);
    bt_list1 = (Button) findViewById(R.id.button1);
    bt_list2 = (Button) findViewById(R.id.button2);
    bt_list3 = (Button) findViewById(R.id.button3);
    bt_list4 = (Button) findViewById(R.id.button4);
    fragmentone fg1 = new fragmentone();
    fragmenttwo fg2 = new fragmenttwo();
    fragmentthree fg3 = new fragmentthree();
    fragmentfour fg4 = new fragmentfour();
    list = new ArrayList<Fragment>();
    list.add(fg1);
    list.add(fg2);
    list.add(fg3);
    list.add(fg4);
    mViewPager.setAdapter(new MyFragmentPagerAdapter(
            getSupportFragmentManager(), list));

}

private void initEvent() {
    mViewPager.setCurrentItem(0);
    bt_list1.setOnClickListener(this);
    bt_list2.setOnClickListener(this);
    bt_list3.setOnClickListener(this);
    bt_list4.setOnClickListener(this);
    mViewPager.setOnPageChangeListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.button1:
        mViewPager.setCurrentItem(0);
        Toast.makeText(mContext, "当前选择:1",Toast.LENGTH_SHORT).show();
        break;

    case R.id.button2:
        mViewPager.setCurrentItem(1);
        Toast.makeText(mContext, "当前选择:2",Toast.LENGTH_SHORT).show();
        break;
    case R.id.button3:
        mViewPager.setCurrentItem(2);
        Toast.makeText(mContext, "当前选择:3",Toast.LENGTH_SHORT).show();
        break;
    case R.id.button4:
        mViewPager.setCurrentItem(3);
        Toast.makeText(mContext, "当前选择:4",Toast.LENGTH_SHORT).show();
        break;

    }
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    mViewPager.setCurrentItem(1);
    mViewPager.setCurrentItem(0);
    super.onResume();
}

@Override
public void onPageScrollStateChanged(int state) {
    // TODO Auto-generated method stub
    if (state==2)
    {
        Toast.makeText(mContext, "当前选择:"+(mViewPager.getCurrentItem()+1),Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub

}

@Override
public void onPageSelected(int arg0) {
    // TODO Auto-generated method stub
    //Toast.makeText(mContext, "当前选择:"+mViewPager.getCurrentItem(),Toast.LENGTH_SHORT);

}

}

fragmentone:
package com.example.viewpagerdemo;

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

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.example.viewpagerdemo.myCallBack.HttpUtil;
import com.example.viewpagerdemo.myCallBack.ResultCallback;
import com.example.viewpagerdemo.myadapter.ListAdapterOne;
import com.example.viewpagerdemo.mybean.BaseBean;
import com.example.viewpagerdemo.mybean.BeanOneArrayList;
import com.example.viewpagerdemo.mybean.ListBeanOne;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**

  • fragment 1
  • @author dy
  • */
    public class fragmentone extends Fragment {
    private Context mContext;
    private View viewOne;
    private ListView mListViewOne;
    private List allDataOne;
    private ListAdapterOne oneAdapter;
    private ResultCallback callback = new ResultCallback() {

    @Override
    public void getReslt(String result) {
        // TODO Auto-generated method stub
        BaseBean b = HttpUtil.getData(mContext, result, "", "");
        if (b.getResponsecode() == 1) {
            Type type = new TypeToken<BeanOneArrayList>() {
            }.getType();
    
            Gson gson = new Gson();
            BeanOneArrayList json = gson.fromJson(result, type);
            allDataOne = json.getData();
    
            if (allDataOne.size() > 0) {
                oneAdapter = new ListAdapterOne(mContext, allDataOne);
                mListViewOne.setAdapter(oneAdapter);
                /*
                 * Message message = new Message(); message.what=1;
                 * mHandler.sendMessage(message);
                 */
            } else {
                Log.i("tag", "No Data");
            }
        }
    
    }
    

    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

    if (viewOne != null) {
        ViewGroup parentViewGroup = (ViewGroup) viewOne.getParent();
        if (parentViewGroup != null) {
            parentViewGroup.removeAllViewsInLayout();
        }
    } else {
        viewOne = inflater.inflate(R.layout.fg1, container, false);
    }
    initView();
    initEvent();
    return viewOne;
    

    }

    private void initView() {
    mContext = this.getActivity();
    mListViewOne = (ListView) viewOne.findViewById(R.id.listView1);
    }

    private void initEvent() {
    String JsonURL = AppConst.ListURL1;
    List p = new ArrayList();
    p.add(new BasicNameValuePair("action", "1"));
    HttpUtil.doPost(JsonURL, p, callback);
    }

}

fragmenttwo:
package com.example.viewpagerdemo;

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

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.viewpagerdemo.myCallBack.HttpUtil;
import com.example.viewpagerdemo.myCallBack.ResultCallback;
import com.example.viewpagerdemo.myadapter.ListAdapterOne;
import com.example.viewpagerdemo.myadapter.ListAdapterTwo;
import com.example.viewpagerdemo.mybean.BaseBean;
import com.example.viewpagerdemo.mybean.BeanTwoArrayList;
import com.example.viewpagerdemo.mybean.ListBeanTwo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**

  • fragment 2
  • @author dy
  • */
    public class fragmenttwo extends Fragment {
    private Context mContext;
    private View viewTwo;
    private ListView mListViewTwo;
    private List allDataTwo;
    private ListAdapterTwo twoAdapter;

    private ResultCallback callback = new ResultCallback() {

    @Override
    public void getReslt(String result) {
        // TODO Auto-generated method stub
        BaseBean b = HttpUtil.getData(mContext, result, "", "");
    
        if (b.getResponsecode() == 1) {
            Type type = new TypeToken<BeanTwoArrayList>() {
            }.getType();
    
            Gson gson = new Gson();
            BeanTwoArrayList json = gson.fromJson(result, type);
            allDataTwo = json.getData();
    
            if (allDataTwo.size() > 0) {
                twoAdapter = new ListAdapterTwo(mContext, allDataTwo);
                mListViewTwo.setAdapter(twoAdapter);
                /*
                 * Message message = new Message(); message.what=1;
                 * mHandler.sendMessage(message);
                 */
            } else {
                Log.i("tag", "No Data");
            }
        }
    
    }
    

    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

    if (viewTwo != null) {
        ViewGroup parentViewGroup = (ViewGroup) viewTwo.getParent();
        if (parentViewGroup != null) {
            parentViewGroup.removeAllViewsInLayout();
        }
    } else {
        viewTwo = inflater.inflate(R.layout.fg2, container, false);
    }
    initView();
    initEvent();
    return viewTwo;
    

    }

    private void initView() {
    mContext = this.getActivity();
    mListViewTwo = (ListView) viewTwo.findViewById(R.id.listView2);
    }

    private void initEvent() {
    String JsonURL = AppConst.ListURL1;
    List p = new ArrayList();
    p.add(new BasicNameValuePair("action", "2"));
    HttpUtil.doPost(JsonURL, p, callback);
    }

}
MyFragmentPagerAdapter:
package com.example.viewpagerdemo.myadapter;

import java.util.ArrayList;

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

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

private ArrayList<Fragment> list;

public MyFragmentPagerAdapter(FragmentManager fm) {
    super(fm);
}

public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
    super(fm);
    this.list = list;
}

@Override
public Fragment getItem(int arg0) {
    // TODO Auto-generated method stub
    return list.get(arg0);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

}


其他的代码都是类似的,就不发了。


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • qxtx007 阿昱 5年前

    经过努力已经解决了,callback写的有点问题。

    点赞 评论 复制链接分享
  • bdmh bdmh 5年前

    肯定是你没有控制好,回调应该去操作哪个listview

    点赞 评论 复制链接分享