我有一个主要活动的应用程序托管由标签控制的2个列表片段(由actionbarsherlock提供)。
两个片段在代码中更不相同,并且动态地从服务器加载数据。一个显示最新内容,第二个显示数据库中的所有内容,实现无限滚动。
如果仅按以下顺序使用,则一切正常: latestFragment - > allFragment
但是当我从latestFragment回归allFragment时 allFragment - > latestFragment
无尽的滚动停止工作!!
我是Android开发的新手,研究了一些零碎的东西确实设法将它们放在一起,但它卡在这里。我简直无法绕开。然后无论我做什么滚动都不会工作,直到我关闭应用程序并重新开始。
MainActivity.java
public class MainActivity extends SlidingFragmentActivity implements OnQueryTextListener
{
protected static DisplayImageOptions options;
protected static ImageLoader imageLoader;
ArrayList latest;
ArrayList generes;
ActionBar.Tab latestTab;
ActionBar.Tab AllTab;
LatestNewsFragment latestNews;
AllNewsFragment allNews;
MenuItem search;
SearchView searchView;
ProgressBar pBar;
SharedPreferences settings;
protected CategoryListFragment mFrag;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
pBar = (ProgressBar) this.findViewById(R.id.pBar);
pBar.setVisibility(View.GONE);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this.getApplicationContext())
.build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.poster)
.delayBeforeLoading(2000)
.showImageForEmptyUri(R.drawable.poster)
.showImageOnFail(R.drawable.poster)
.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.NONE)
.cacheOnDisc()
.cacheInMemory()
.build();
Bundle extras = getIntent().getExtras();
latest = extras.getParcelableArrayList("latest");
generes = extras.getParcelableArrayList("generes");
disclaimer = extras.getString("disclaimer");
/* Tabs */
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
/* Latest list fragment */
latestNews = new LatestNewsFragment();
latestNews.setImagLoader(imageLoader);
latestNews.setImageDisplayOptions(options);
latestNews.setData(latest);
latestTab = getSupportActionBar().newTab().setText("Latest");
latestTab.setTabListener(new TabListener(latestNews));
getSupportActionBar().addTab(latestTab);
/* All news list fragment */
allNews = new AllNewsFragment();
allNews.setImagLoader(imageLoader);
allNews.setImageDisplayOptions(options);
AllTab = getSupportActionBar().newTab().setText("Browse All");
AllTab.setTabListener(new TabListener(allNews));
getSupportActionBar().addTab(AllTab);
/* Sliding menu */
// set the Behind View
setBehindContentView(R.layout.menu_frame);
if (savedInstanceState == null) {
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
mFrag = new CategoryListFragment();
mFrag.setData(generes);
t.replace(R.id.menu_frame, mFrag);
t.commit();
} else {
mFrag = (CategoryListFragment)this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
}
// customize the SlidingMenu
SlidingMenu sm = getSlidingMenu();
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setShadowDrawable(R.drawable.shadow);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setFadeDegree(0.35f);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
settings = getSharedPreferences("com.newsspace.mobile.android", Context.MODE_PRIVATE);
boolean hasAcceptedPolicy = settings.getBoolean("disclaimeraccept", false);
if(!hasAcceptedPolicy)
showDisclaimer(this, disclaimer);
}
void showDisclaimer(final Activity context, String message)
{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
// Setting Dialog Title
alertDialog.setTitle(getText(R.string.disclaimer));
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting Icon to Dialog
//alertDialog.setIcon(R.drawable.delete);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton(getText(R.string.disclaimer_agree), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Editor editor = settings.edit();
editor.putBoolean("disclaimeraccept", true);
editor.commit();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton(getText(R.string.disclaimer_disagree), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Editor editor = settings.edit();
editor.putBoolean("disclaimeraccept", false);
editor.commit();
context.finish();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.action_menu, menu);
search = menu.findItem(R.id.search);
SearchView searchView = (SearchView) search.getActionView();
searchView.setOnQueryTextListener(this);
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
searchView.setSearchableInfo(info);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
case R.id.refresh:
refreshCurrentFragment();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
{
// NO OP
}
void refreshCurrentFragment()
{
}
@Override
public void hideProgressbar() {
// TODO Auto-generated method stub
pBar.setVisibility(View.GONE);
}
@Override
public void showProgressbar() {
// TODO Auto-generated method stub
pBar.setVisibility(View.VISIBLE);
}
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
search.collapseActionView();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent){
if(Intent.ACTION_SEARCH.equals(intent.getAction())){
String searchQuery = intent.getStringExtra(SearchManager.QUERY);
}
}
}
ActionBar.Tab latestTab;
ActionBar.Tab AllTab;
LatestNewsFragment latestNews;
AllNewsFragment allNews;
MenuItem search;
SearchView searchView;
ProgressBar pBar;
SharedPreferences settings;
protected CategoryListFragment mFrag;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
pBar = (ProgressBar) this.findViewById(R.id.pBar);
pBar.setVisibility(View.GONE);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this.getApplicationContext())
.build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.poster)
.delayBeforeLoading(2000)
.showImageForEmptyUri(R.drawable.poster)
.showImageOnFail(R.drawable.poster)
.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.NONE)
.cacheOnDisc()
.cacheInMemory()
.build();
Bundle extras = getIntent().getExtras();
latest = extras.getParcelableArrayList("latest");
generes = extras.getParcelableArrayList("generes");
disclaimer = extras.getString("disclaimer");
/* Tabs */
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
/* Latest list fragment */
latestNews = new LatestNewsFragment();
latestNews.setImagLoader(imageLoader);
latestNews.setImageDisplayOptions(options);
latestNews.setData(latest);
latestTab = getSupportActionBar().newTab().setText("Latest");
latestTab.setTabListener(new TabListener(latestNews));
getSupportActionBar().addTab(latestTab);
/* All news list fragment */
allNews = new AllNewsFragment();
allNews.setImagLoader(imageLoader);
allNews.setImageDisplayOptions(options);
AllTab = getSupportActionBar().newTab().setText("Browse All");
AllTab.setTabListener(new TabListener(allNews));
getSupportActionBar().addTab(AllTab);
/* Sliding menu */
// set the Behind View
setBehindContentView(R.layout.menu_frame);
if (savedInstanceState == null) {
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
mFrag = new CategoryListFragment();
mFrag.setData(generes);
t.replace(R.id.menu_frame, mFrag);
t.commit();
} else {
mFrag = (CategoryListFragment)this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
}
// customize the SlidingMenu
SlidingMenu sm = getSlidingMenu();
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setShadowDrawable(R.drawable.shadow);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setFadeDegree(0.35f);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
settings = getSharedPreferences("com.newsspace.mobile.android", Context.MODE_PRIVATE);
boolean hasAcceptedPolicy = settings.getBoolean("disclaimeraccept", false);
if(!hasAcceptedPolicy)
showDisclaimer(this, disclaimer);
}
void showDisclaimer(final Activity context, String message)
{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
// Setting Dialog Title
alertDialog.setTitle(getText(R.string.disclaimer));
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting Icon to Dialog
//alertDialog.setIcon(R.drawable.delete);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton(getText(R.string.disclaimer_agree), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Editor editor = settings.edit();
editor.putBoolean("disclaimeraccept", true);
editor.commit();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton(getText(R.string.disclaimer_disagree), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Editor editor = settings.edit();
editor.putBoolean("disclaimeraccept", false);
editor.commit();
context.finish();
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.action_menu, menu);
search = menu.findItem(R.id.search);
SearchView searchView = (SearchView) search.getActionView();
searchView.setOnQueryTextListener(this);
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
searchView.setSearchableInfo(info);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
case R.id.refresh:
refreshCurrentFragment();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
{
// NO OP
}
void refreshCurrentFragment()
{
}
@Override
public void hideProgressbar() {
// TODO Auto-generated method stub
pBar.setVisibility(View.GONE);
}
@Override
public void showProgressbar() {
// TODO Auto-generated method stub
pBar.setVisibility(View.VISIBLE);
}
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
search.collapseActionView();
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent){
if(Intent.ACTION_SEARCH.equals(intent.getAction())){
String searchQuery = intent.getStringExtra(SearchManager.QUERY);
}
}
}
public class TabListener实现ActionBar.TabListener {
TabListener.java
}
AllNewsFragment.java
Fragment fragment;
public TabListener(Fragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
ft.remove(fragment);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
此外,除了无限滚动之外,latestNews Fragment与AllNewsFragment完全相同。我可以在这里张贴是必要的。但我觉得这很好。请帮我找到这个问题的答案。
答案 0 :(得分:0)
希望你现在有了解决方案。但无论如何,我通过在init
if(!activityReady)
到onActivityCreated
块之后的行来实现它
ListView list = (ListView) this.rootview;
list.setOnScrollListener(this);