有关负载数据的TabLayout问题

时间:2017-11-02 06:25:45

标签: java android android-tablayout

我对android没有经验,我为Tablayout实现了代码,我有六个选项卡,每个选项卡都有ListView,其中数据将来自服务器。我正在使用VolleyResponse来获取数据,但所有标签的数据都是在开放时间。我需要数据应该刷卡,当用户将前往刷卡数据不应再次加载。所以请指导我该怎么办? 下面是我的片段代码。

   private void setupViewPager(ViewPager viewPager) {
    int numberOfPages=7;
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new Matches_Tab(), "Matches");
    adapter.addFragment(new NewMatches_Tab(), "New matches");
    adapter.addFragment(new Similar_Matchs_Tab(), "Similar Matches");
    adapter.addFragment(new ShortlistTab(), "ShortListed");
    adapter.addFragment(new Viewed_My_Profile(), "View My Profile");
    adapter.addFragment(new ShortListedMeTab(), "ShortListed Me");
    adapter.addFragment(new Photo_Request_Received(), "Photo Request Received");
    viewPager.setOffscreenPageLimit(numberOfPages);
    viewPager.setAdapter(adapter);
}

片段类

public class Matches_Tab extends Fragment{
// Session Manager Class
SessionManager session;
String email;
public String JSON_URL;
private ListView listView;
public Matches_Tab() {}


private static Matches_Tab instance;

public static synchronized Matches_Tab getInstance(Bundle data){
    if (instance == null) {
        instance = new Matches_Tab();
        instance.setArguments(data);
    }
    return instance;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TypefaceUtil.overrideFont(getContext(), "SERIF", "Peddana-Regular.ttf");

    // Session class instance
    session = new SessionManager(getActivity());
    // get user data from session
    HashMap<String, String> user = session.getUserDetails();
    email = user.get(SessionManager.KEY_EMAIL);
    JSON_URL = "https://www.maangal.com/maangal_mobile/matches.php?matri_id="+email;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.matches_tab, container, false);
  //  ProgressBar progressbar = (ProgressBar) view.findViewById(R.id.progressBar);
    listView = (ListView) view.findViewById(R.id.listView);
    sendRequest();
    //getArguments();
    return view;
}


private void sendRequest(){
    final ProgressDialog loading = ProgressDialog.show(getActivity(),"Loading Data", "Please wait...",false,false);

    StringRequest stringRequest = new StringRequest(Request.Method.POST,JSON_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    loading.dismiss();
                    showJSON(response);
                    Log.e("Broder Matches s----->",response);
                          }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getContext(),error.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
    int MY_SOCKET_TIMEOUT_MS = 30000;
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(
            MY_SOCKET_TIMEOUT_MS, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    requestQueue.add(stringRequest);
}

protected void showJSON(String json){
    ParseJSON pj = new ParseJSON(json);
    pj.parseJSON();
    Profile_Match_custom_List cl = new Profile_Match_custom_List(getActivity(), ParseJSON.ids,ParseJSON.ages, ParseJSON.heights, ParseJSON.communities,ParseJSON.castes,ParseJSON.educations,ParseJSON.occupations,ParseJSON.incomes,ParseJSON.pics,ParseJSON.locations,ParseJSON.shortlist,ParseJSON.expressinterest);
    listView.setAdapter(cl);
  }
}

4 个答案:

答案 0 :(得分:2)

您可以使用ViewPager.OnPageChangeListener获取页面变为可见时的回调。 检查以下代码。

annotation type not applicable to this kind of declaration

答案 1 :(得分:1)

您可以延长BaseFragment

public abstract class BaseFragment extends Fragment {

protected Activity mActivity;

/**
 * isFragmentVisible
 */
private boolean isFragmentVisible;
/**
 * View in onCreateView is ok
 */
private boolean isPrepared;
/**
 * isFirstLoad
 */
private boolean isFirstLoad = true;
/**
 * isForceLoad
 */
private boolean isForceLoad = false;


/**
 * onAttach Activity
 *
 * @param context
 */
@Override
public void onAttach(Context context) {
    super.onAttach(context);
    this.mActivity = (Activity) context;
}


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

    isFirstLoad = true;
    View view = LayoutInflater.from(mActivity)
            .inflate(getLayoutId(), container, false);
    initView(view, savedInstanceState);
    isPrepared = true;
    lazyLoad();

    return view;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onDestroy() {
    super.onDestroy();
}


/**
 * want to load data ,need set in onCreateView
 * isPrepared = true;
 */
protected void lazyLoad() {
    if (isPrepared() && isFragmentVisible()) {
        if (isForceLoad || isFirstLoad()) {
            isForceLoad = false;
            isFirstLoad = false;
            initData();
        }
    }
}

public boolean isPrepared() {
    return isPrepared;
}

public boolean isFirstLoad() {
    return isFirstLoad;
}

public boolean isFragmentVisible() {
    return isFragmentVisible;
}

/**
 * use with viewpager
 *
 * @param isVisibleToUser is visible for user
 */
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (getUserVisibleHint()) {
        onVisible();
    } else {
        onInvisible();
    }
}

/**
 * if you use show hide to set fragment ,do this
 *
 * @param hidden hidden True if the fragment is now hidden, false if it is not
 *               visible.
 */
@Override
public void onHiddenChanged(boolean hidden) {
    super.onHiddenChanged(hidden);
    if (!hidden) {
        onVisible();
    } else {
        onInvisible();
    }
}

protected void onVisible() {
    isFragmentVisible = true;
    lazyLoad();
}

protected void onInvisible() {
    isFragmentVisible = false;
}

/**
 * set force load
 */
public void setForceLoad(boolean forceLoad) {
    this.isForceLoad = forceLoad;
}


/**
 * getLayoutId ,like : R.layout.fragment
 *
 * @return
 */
protected abstract int getLayoutId();

/**
 * initView
 *
 * @param view
 * @param savedInstanceState
 */
protected abstract void initView(View view, Bundle savedInstanceState);

/**
 * load data here
 */
protected abstract void initData();

}

然后以initData方法加载数据。

@Override
protected void initData() {
   // request here
}

注意

  • 主要方法是setUserVisibleHintonHiddenChanged
  • 添加一些布尔标志以确定数据是否已加载,视图创建等。

答案 2 :(得分:1)

替代解决方案

tabLayout.setupWithViewPager(mViewPager);
tabLayout.setOnTabSelectedListener(
  new TabLayout.ViewPagerOnTabSelectedListener(mViewPager) {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        super.onTabSelected(tab);
        //put your Logic here
    }
});

答案 3 :(得分:0)

如果您正在使用ViewPager并且您正在获取一个API调用中所有选项卡的数据,那么我建议您将viewpager的偏移量设置为等于标签数量。

ViewPager vp = ...;
vp.setOffscreenPageLimit(numberOfTabsHERE);

注意:只有在没有太多数据显示的情况下才使用此项,否则您可能会获得MemoryOutOfException

如果你有很多标签和大量数据。对片段使用Singleton模式是一个不错的选择。

public class MyFragment extends Fragment {
    private static MyFragment instance;

    public static synchronized MyFragment getInstance(Bundle data // if any){
        if (instance == null) {
            instance = new MyFragment();
            instance.setArguments(data);
        }
        return instance;
    }
}
...
onCreateView{.... getArguments()}