使用列表片段PagerSlidingTabStrip

使用列表片段PagerSlidingTabStrip

本文介绍了使用列表片段PagerSlidingTabStrip-自定义视图显示突然的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用来创建滑动标签。我能够成功在我的项目中使用这个库。

I am using PagerSlidingTabStrip to create a sliding tabs. I was successfully able to use this library in my project.

根据选定/点击选项卡上,应用程序应该发出一个GET请求到服务器获取数据并填充列表视图。在选择每一个选项卡时,HTTP调用将作出服务器列表视图将被更新。

Based on selected/clicked tab, the application should send a get request to server to fetch data and populate the list view. Every-time the tab is selected, http call would be made to server and list view will be updated.

我只是修改样本中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();
    }
}

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

}Didn't touch any other file.

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

Here is the custom adapter(UserAdapter.java) which I have created:

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;
}

我能成功运行应用程序(没有看到登录猫的任何​​错误或例外)。但是,行为是很突然的:

I was able to run the application successfully(Didn't see any error or exceptions in Log Cat). However, the behaviour is quite abrupt:


  1. 加载应用程序第一次:当我preSS选项卡1和表2,则其获取数据并填充列表视图(预期)。
    但是,不要刷新/从服务器上获取数据时,我preSS最后一个选项卡(表3)。

  2. 当我完成了所有的点击三个选项:选项卡1和Tab 3不获取数据,无论是加载列表。该片段是空白。只有表2显示列表视图,但不从服务器刷新数据。
    标签的总数等于3。

2行为重复每次,无论组合我试试。

Behaviour 2 is repeated every time, whatever combination I try.

我很新的Andr​​oid开发,所以无法调试此行为。

I am quite new to Android development, so not able debug this behaviour.

任何帮助是AP preciated。

Any help is appreciated.

UPD:
我能够通过在骑setUserVisibleHint带来所需的行为:

UPD:I was able to bring desired behaviour by over-riding 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.");
        }
    }

这是在prescribed方式

推荐答案

我查了一下你的code,但我发现了一些错误:

I looked a bit your code but I found some errors:

第一个变化是:

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

下一步:创建你的用户数组是这样的:

next: Create your Users Array like this:

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;
}

这篇关于使用列表片段PagerSlidingTabStrip-自定义视图显示突然的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:08