问题描述
我试图加载的项目里面的微调我的 MainActivity
toolbar.Inside MainActivity
有一个叫做片段 HomeFragment
。这片段包含 SlidingTabLayout
有一次我点击不同的标签,我需要不同的数据集加载到上面提到的微调。
I am trying to load items to the spinner inside my MainActivity
toolbar.Inside MainActivity
there is a fragment called HomeFragment
. And that fragment contains SlidingTabLayout
and once I click different tabs I need to load different data sets to the above mentioned spinner.
下面是它的样子:
我已经实现在 MainActivity
方法将数据添加到微调:
I have implemented method inside MainActivity
to add data to the spinner :
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
象下面这样:
And inside my HomeActivity
I have set OnPageChangeLister
to SlidingTabLayout
like below :
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)的;
方法我检索凌空向服务器请求数据,并工作正常。
With use of volleySubCatFilter("category_here");
method I retrieve data from volley request to the server and it is working fine.
我的问题是第一次加载应用程序的女性标签被选中,但没有数据加载到spinner.But我试图Log.i(位置 在页面滚动 - +位置);
并尽快应用程序加载它给了我 I /职务:在页面滚动 - 0
在我的日志,cat.But数据犯规负荷。
Debuging行((MainActivity)getActivity())。addItemsToSpinner(潜艇)
0告诉我items.Once preSS另一个选项卡,然后它开始加载属于$ P数据$ pvious标签。例如: - pressing MEN 标签加载的应用程序中获取数据后权属于女性 tab.Then我点击它开始加载数据的任何选项卡上属于 WOMEN 标签。
The problem I have is first time loading the application WOMEN tab is selected and but no data loaded to the spinner.But I tried with Log.i("Position", "On page scrolled - " + position);
and as soon as the app loaded it gives me I/Position: On page scrolled - 0
in my log-cat.But data doesnt load.Debuging line ((MainActivity) getActivity()).addItemsToSpinner(subs)
tells 0 items.Once I press another tab then it starts loading data which belongs to previous tab. Example :- Pressing MEN tab right after app loaded getting data belongs to WOMEN tab.Then I click on any tab it start loading data belongs to WOMEN tab.
如果我介绍这个问题的数据加载到微调是一步
后面。
如果有人给我建议的方式来摆脱这种bug.Thanks提前这将不胜感激。
It would be grateful if anyone suggest me a way to get rid of this bug.Thanks in advance.
推荐答案
您需要提供连续的步骤,你的数据在后台线程加载
。当你选择一个页面,你有两个选择。
You need to provide the sequential steps as your data is being loaded in the background thread
. When you select a page, you have two options.
1。启动是同步凌空通话
这个调用后更新微调适配器
1. Initiate either a Synchronous Volley call
and update the spinner adapter after this 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。注册广播接收器
在活动
然后 sendBroadcast
到 LocalBroadcastManager 在 VolleyResponse
。在广播接收器的更新的onReceive微调项
2. Register a BroadcastReceiver
in your Activity
and then sendBroadcast
through LocalBroadcastManager
on VolleyResponse
. and update the spinner items in onReceive of BroadcastReceiver
希望这有助于。
这篇关于关于使用凌空库加载项微调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!