我正在尝试将项目加载到MainActivity
工具栏内的微调器中。在MainActivity
内有一个名为HomeFragment
的片段。该片段包含SlidingTabLayout
,一旦我点击不同的标签,我需要将不同的数据集加载到上面提到的微调器。
以下是它的样子:
我在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 标签。
如果我简要说明这个问题,数据加载到微调器是一步 后面。
如果有人建议我摆脱这个错误的话,将不胜感激。谢谢。
答案 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
注册LocalBroadcastManager
到VolleyResponse
。并更新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)
请完成以下步骤:
public interface Updateable {
public void update();
}
在ViewPager Fragments中实现上述界面。
在PagerAdapter课程中,覆盖getItemPosition(Object object)
`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);
}`
现在在片段的update()方法中,提供更新工具栏中微调器数据的逻辑。可以通过在片段的OncreateView中提供invalidateOptionMenu()
更新方法和setHasOptionsMenu(true)
来完成。工具栏微调器数据更新可以通过实现onPrepareOptionsMenu(Menu menu)
覆盖方法来完成。
在标签页滚动逻辑中,通知适配器:
@Override
public void onPageScrollStateChanged(int state) {
yourAdapter.notifyDataSetChanged();
}