PagerSlidingTabStrip-使用列表片段的自定义视图显示突然行为

时间:2015-04-14 21:49:27

标签: android android-fragments android-listview android-listfragment pagerslidingtabstrip

我正在使用PagerSlidingTabStrip创建滑动标签。我成功地在我的项目中使用了这个库。

基于选中/单击的选项卡,应用程序应向服务器发送获取请求以获取数据并填充列表视图。每次选择选项卡时,都会对服务器进行http调用,并且列表视图将被更新。

我只修改示例中的SuperAwesomeCardFragment.java

public class SuperAwesomeCardFragment extends ListFragment {

private ListView listView;
private ArrayList<User> arrayOfUsers = new ArrayList<User>();
private UsersAdapter useradapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
JSONObject jsonobject;
JSONArray ja;

private static final String ARG_POSITION = "position";
private int position;

public static SuperAwesomeCardFragment newInstance(int position) {
    SuperAwesomeCardFragment f = new SuperAwesomeCardFragment();
    Bundle b = new Bundle();
    b.putInt(ARG_POSITION, position);
    f.setArguments(b);
    return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    position = getArguments().getInt(ARG_POSITION);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    new DownloadJSON().execute();
    View rootView = inflater.inflate(R.layout.fragment,
            container, false);
    listView = (ListView) rootView.findViewById(android.R.id.list);
    useradapter = new UsersAdapter(getActivity(), android.R.id.list, arrayOfUsers);
    listView.setAdapter(useradapter);
    //Return the View for the fragment's UI
    return rootView;
}

// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(getActivity());
        // Set progressdialog title
        mProgressDialog.setTitle("Connecting to server");
        // Set progressdialog message
        mProgressDialog.setMessage("Serving...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        String statusStr;
        switch (position) {
            case 0:
                statusStr = "UNREAD";
                break;
            case 1:
                statusStr = "PROCCC";
                break;
            case 2:
                statusStr = "COMPLETE";
                break;
            default:
                statusStr = "UNKNOWN";
        }
        arraylist = new ArrayList<HashMap<String, String>>();
        jsonobject = JSONfunctions
                .getJSONfromURL("http://example.com/?id=" + statusStr);
        try {
            // Locate the array name in JSON
            ja = jsonobject.getJSONArray("all_orders");
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void args) {
        arrayOfUsers.clear();
        arrayOfUsers.addAll(User.fromJson(ja));
        System.out.println(arrayOfUsers.toString());
        listView.setAdapter(useradapter);
        // Close the progressbar
        mProgressDialog.dismiss();
        useradapter.notifyDataSetChanged();
    }
}

} 没有触及任何其他文件。

这是我创建的自定义适配器(UserAdapter.java):

public class UsersAdapter extends ArrayAdapter<User> {
private  Context context;
private List<User> objects;
public UsersAdapter(Context context, int textViewResourceId, List<User> objects) {
    super(context, textViewResourceId, objects);
    // TODO Auto-generated constructor stub
    this.objects=objects;
    this.context = context;
}

private  class ViewHolder {
    TextView name;
    TextView home;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder; // view lookup cache stored in tag
    // Get the data item for this position
    User user = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        viewHolder = new ViewHolder();
        convertView = mInflater.inflate(R.layout.list_row, null);
        viewHolder.name = (TextView) convertView.findViewById(R.id.tvName);
        viewHolder.home = (TextView) convertView.findViewById(R.id.tvHome);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    // Populate the data into the template view using tYiuhe data object
    viewHolder.name.setText(user.ord_name);
    viewHolder.home.setText(user.ord_id);
    // Return the completed view to render on screen
    return convertView;
}

我能够成功运行应用程序(没有看到Log Cat中的任何错误或异常)。但是,这种行为是非常突然的:

  1. 第一次加载应用程序:当我按Tab 1和Tab 2时,它会获取数据并填充列表视图(预期)。 但是,当我按下最后一个选项卡时,它不会从服务器刷新/获取数据(表3)。
  2. 点击完三个标签后,标签1和标签3都没有获取数据,也没有加载列表。片段是空白的。只有选项卡2显示列表视图,但不刷新服务器中的数据。 标签总数等于3。
  3. 每次重复行为2,无论我尝试什么组合。

    我对Android开发很陌生,所以无法调试此行为。

    感谢任何帮助。

    UPD: 通过覆盖setUserVisibleHint,我能够带来所需的行为:

    public void setUserVisibleHint(boolean isVisibleToUser) {
            super.setUserVisibleHint(isVisibleToUser);
    
            if (isVisibleToUser) {
                new DownloadJSON().execute();
                Log.d("MyFragment", "Fragment is visible.");
            }
            else {
    
                Log.d("MyFragment", "Fragment is not visible.");
            }
        }
    

    这是规定的方式吗?

1 个答案:

答案 0 :(得分:1)

我看了一下你的代码,但我发现了一些错误:

首先改变这个:

    @Override
    protected void onPostExecute(Void args) {
    arrayOfUsers.clear();
    arrayOfUsers.addAll(User.fromJson(ja));
    if(useradapter!=null){
    useradapter.notifyDataSetChanged();
    }
        // Close the progressbar
    mProgressDialog.dismiss();
    }

next:像这样创建用户数组:

private ArrayList<User> arrayOfUsers=new ArrayLit<User>();

改变这个:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment,
            container, false);
    listView = (ListView) rootView.findViewById(android.R.id.list);
    if(userAdapter==null){
    userAdapter=new UsersAdapter(getActivity(), android.R.id.list,arrayOfUsers);
    listView.setAdapter(userAdapter);
    }
    return rootView;
}