关于使用齐射库将项目加载到微调器

时间:2016-02-29 05:26:33

标签: android android-fragments android-volley android-tablayout

我正在尝试将项目加载到MainActivity工具栏内的微调器中。在MainActivity内有一个名为HomeFragment的片段。该片段包含SlidingTabLayout,一旦我点击不同的标签,我需要将不同的数据集加载到上面提到的微调器。

  

以下是它的样子:

enter image description here

我在MainActivity中实现了一个方法,将数据添加到微调器:

    public void addItemsToSpinner(final Collection<String> collection) {

        ArrayList<String> list = new ArrayList<>();
        list.addAll(collection);

        CustomSpinnerAdapter spinAdapter = new CustomSpinnerAdapter(getApplicationContext(), list);
        subCategories_spinner.setAdapter(spinAdapter);
        subCategories_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapter, View v, int position, long id) {
                // On selecting a spinner item
                String item = adapter.getItemAtPosition(position).toString();

                // Showing selected spinner item
//                Toast.makeText(getApplicationContext(), "Selected  : " + item,
//                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });

    }

在我的HomeActivity内,我将OnPageChangeLister设置为SlidingTabLayout,如下所示:

mTabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (firstScrolled) {
                    // Here log-cat gives me I/Position: On page scrolled - 0 which means on stating app this line executed. But data not set to the spinner.
                   // Log.i("Position", "On page scrolled - " + position);
                    volleySubCatFilter("women");
                    ((MainActivity) getActivity()).addItemsToSpinner(subs);//debug console subs (ArrayList) size given as 0 here
                    firstScrolled = false;
                }


            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                   // Log.i("Position", "On page selected - " + position);
                    volleySubCatFilter("women");
                   ((MainActivity) getActivity()).addItemsToSpinner(subs);

                }
                if (position == 1) {
                   // Log.i("Position", "On page selected - " + position);
                    volleySubCatFilter("men");
                    ((MainActivity) getActivity()).addItemsToSpinner(subs);

                }
                if (position == 2) {
                   // Log.i("Position", "On page selected - " + position);
                    volleySubCatFilter("household%20goods");
                   ((MainActivity) getActivity()).addItemsToSpinner(subs);

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

使用volleySubCatFilter("category_here");方法我从排样请求中检索数据到服务器,它运行正常。

我遇到的问题是第一次加载应用程序 WOMEN 标签被选中,但没有数据加载到微调器。但是我尝试使用Log.i("Position", "On page scrolled - " + position);并且应用程序一加载它在我的log-cat中给了我I/Position: On page scrolled - 0但是数据没有加载。 退货行((MainActivity) getActivity()).addItemsToSpinner(subs)告诉0项。一旦我按下另一个标签,它就会开始加载属于上一个标签的数据。示例: - 在加载应用程序后,按 MEN 标签,右键属于 WOMEN 标签。然后点击任何标签,开始加载数据属于 WOMEN 标签。

  

如果我简要说明这个问题,数据加载到微调器是一步   后面。

enter image description here

如果有人建议我摆脱这个错误的话,将不胜感激。谢谢。

3 个答案:

答案 0 :(得分:2)

您需要在background thread中加载数据时提供顺序步骤。选择页面时,有两个选项。

<强> 1。启动Synchronous Volley call并在此次调用后更新微调器适配器

RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest request = new JsonObjectRequest(URL, null, future, future);
requestQueue.add(request);

try {
  JSONObject response = future.get(); // this will block (forever)
  ArrayList<String> some_items = response.getStringArray();
  //update the spinner with new items

} catch (InterruptedException e) {
  // exception handling
} catch (ExecutionException e) {
  // exception handling
}

<强> 2。在BroadcastReceiver中注册Activity,然后在sendBroadcast注册LocalBroadcastManagerVolleyResponse。并更新BroadcastReceiver的onReceive中的微调项目

希望这有帮助。

答案 1 :(得分:0)

我不确定这是一个错误或功能,在应用启动的情况下,最初 pagechangelistener 未被调用。切换选项卡后会调用它。我希望您在代码中进行一些更改:

在Activity中的方法中编写您想要执行的功能,然后在onPageSelected方法中调用它。

mTabs.setOnPageChangeListener(new OnPageChangeListener() {
    @Override
    public void onPageSelected(int index) {
        if (position == 0) {
                   // Log.i("Position", "On page selected - " + position);
                    volleySubCatFilter("women");
                   ((MainActivity) getActivity()).addItemsToSpinner(subs);

                }
                if (position == 1) {
                   // Log.i("Position", "On page selected - " + position);
                    volleySubCatFilter("men");
                    ((MainActivity) getActivity()).addItemsToSpinner(subs);

                }
                if (position == 2) {
                   // Log.i("Position", "On page selected - " + position);
                    volleySubCatFilter("household%20goods");
                   ((MainActivity) getActivity()).addItemsToSpinner(subs);

                }
    }
    ...
}

然后在致电后立即

setCurrentItem(index);

在Activity中,添加以下if语句

if(index == 0) {
    volleySubCatFilter("women");
                       ((MainActivity) getActivity()).addItemsToSpinner(subs);
}

答案 2 :(得分:0)

请完成以下步骤:

  1. 创建界面:
  2. public interface Updateable { public void update(); }

    1. 在ViewPager Fragments中实现上述界面。

    2. 在PagerAdapter课程中,覆盖getItemPosition(Object object)

    3. `public int getItemPosition(Object object){

          if (object instanceof ManFragment) {
              ManFragment f = (ManFragment) object;
              f.update();
          } else if (object instanceof WomanFragment) {
              WomanFragment f = (WomanFragment) object;
              f.update();
          }
          else if (object instanceof HouseFragment) {
              HouseFragment f = (HouseFragment) object;
              f.update();
          }
      
          return super.getItemPosition(object);
      }`
      
      1. 现在在片段的update()方法中,提供更新工具栏中微调器数据的逻辑。可以通过在片段的OncreateView中提供invalidateOptionMenu()更新方法和setHasOptionsMenu(true)来完成。工具栏微调器数据更新可以通过实现onPrepareOptionsMenu(Menu menu)覆盖方法来完成。

      2. 在标签页滚动逻辑中,通知适配器:

      3. @Override public void onPageScrollStateChanged(int state) { yourAdapter.notifyDataSetChanged(); }