タブで制御される 2 つのリスト フラグメントをホストするメイン アクティビティを持つアプリケーションがあります (actionbarsherlock 提供)。
両方のフラグメントは、コードが同じではなく、サーバーから動的にデータをロードします。1 つは最新のコンテンツを表示し、2 番目はデータベース内のすべてのコンテンツを表示し、エンドレス スクロールが実装されています。
次の順序でのみ使用する場合、すべてがうまく機能します: latestFragment -> allFragment
しかし、latestFragment から allFragment allFragment -> latestFragment に戻ったとき
エンドレススクロールが効かなくなる!!
私はアンドロイド開発に不慣れで、いくつかの断片を研究してすべてをまとめることができましたが、ここで立ち往生しています。私は単に歩き回ることができません。その後、アプリを閉じて再度開始するまで、何をしてもスクロールが機能しません。
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);
}
}
}
TabListener.java
public class TabListener は ActionBar.TabListener を実装します {
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
}
}
AllNewsFragment.java
public class AllNewsFragment extends ListFragment implements OnRefreshListener, OnConnectivityHandler, OnScrollListener {
ArrayList<News> news;
OnNewsSelectedListener listener;
DisplayImageOptions options;
ImageLoader imageLoader;
LazyAdapter adapter;
NewsListQueryData data;
boolean canLoadNewss = true;
int page = 1;
int total = 0;
int category = 0;
String searchterm = "";
View rootview;
int mVisibleThreshold = 5;
int mPreviousTotal = 0;
boolean mLoading = true;
boolean mLastPage = false;
ConnectionManager connectionManager;
boolean isConnected;
boolean failedCall = false;
boolean activityReady = false;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.list, null);
Log.d("APP", "createView");
return rootview;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.d("APP", "onCreate");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
public void setImageDisplayOptions(DisplayImageOptions options){
this.options = options;
}
public void setImagLoader(ImageLoader imageLoader){
this.imageLoader = imageLoader;
}
public void setData(ArrayList<News> news){
this.news = news;
}
public ArrayList<News> getData(){
return this.news;
}
void init()
{
if(news == null)
this.news = new ArrayList<News>();
adapter = new LazyAdapter(news, getActivity(), options, imageLoader);
setListAdapter(adapter);
ListView list = (ListView) this.rootview;
list.setOnScrollListener(this);
this.category = 0;
this.page = 1;
this.searchterm = "";
this.total = 0;
this.mPreviousTotal = 0;
this.mLoading = true;
this.mLastPage = false;
connectionManager = new ConnectionManager(this.getActivity(), this);
isConnected = connectionManager.isConnectedToInternet();
connectionManager.regsiterForNetworkChanges();
loadMoreNewss(category, page, searchterm);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(!activityReady) init();
activityReady = true;
}
@Override
public void onListItemClick(ListView lv, View v, int position, long id) {
News selectedNews = (News) getListAdapter().getItem(position);
if (selectedNews != null)
showNewsDetails(selectedNews);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
listener = (OnNewsSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
void showNewsDetails(News news)
{
listener.newsSelected(news);
}
@Override
public void result(Object args) {
listener.hideProgressbar();
// TODO Auto-generated method stub
if(args instanceof NewsListQueryData){
data = (NewsListQueryData) args;
page = data.page;
category = data.category;
searchterm = data.searchterm;
total = data.total;
appendData(data.news);
canLoadNewss = true;
}
}
@Override
public void fault(Object args) {
// TODO Auto-generated method stub
listener.hideProgressbar();
canLoadNewss = true;
Toast.makeText(this.getActivity(), R.string.loading_reported_failed, Toast.LENGTH_LONG).show();
}
@Override
public void started(Object args) {
// TODO Auto-generated method stub
listener.showProgressbar();
}
@SuppressWarnings("unchecked")
void loadMoreNewss(int category, int page, String searchterm)
{
if(canLoadNewss){
if(isConnected){
new LoadNewsTask(this).execute(Constants.SEARCH_SCRIPT, category, searchterm, page);
canLoadNewss = false;
failedCall = false;
}
else{
failedCall = true;
connectionManager.showProgressDialog();
}
}
}
void appendData(ArrayList<News> news)
{
for(int i=0;i<news.size();i++)
adapter.add(news.get(i));
adapter.notifyDataSetChanged();
}
@Override
public void onScroll(AbsListView view, int firstVisible, int visibleCount, int totalCount) {
if (mLoading) {
if (totalCount > mPreviousTotal) {
mLoading = false;
mPreviousTotal = totalCount;
page++;
// Find your own condition in order to know when you have finished displaying all items
if (page + 1 > total) {
mLastPage = true;
}
}
}
if (!mLastPage && !mLoading && (totalCount - visibleCount) <= (firstVisible + mVisibleThreshold)) {
loadMoreNewss(category, page, searchterm);
mLoading = true;
}
}
@Override
public void onScrollStateChanged(AbsListView arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void connectToInternet() {
// TODO Auto-generated method stub
}
@Override
public void doNotConnectToInternet() {
// TODO Auto-generated method stub
}
@Override
public void connectionStatusChanged() {
/*
// TODO Auto-generated method stub
isConnected = connectionManager.isConnectedToInternet();
if(isConnected){
connectionManager.hideProgressDialog();
if(failedCall) loadMoreNewss(category, page, searchterm);
}else{
connectionManager.showProgressDialog();
}
*/
}
void emptyAdapter()
{
adapter.clear();
adapter.notifyDataSetChanged();
}
@Override
public void refresh()
{
this.page = 1;
this.total = 0;
this.mPreviousTotal = 0;
this.mLoading = true;
this.mLastPage = false;
if(adapter != null)
emptyAdapter();
loadMoreNewss(category, page, searchterm);
}
}
さらに、latestNews Fragment は、エンドレス スクロールを除いて、AllNewsFragment とまったく同じです。私はここに投稿できる必要があります。でもこれくらいでいいのかな。この問題の答えを見つけてください。