:
package com.creatiosoft.rssfeed.adaptor;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
import com.creatiosoft.rssfeed.utils.RssItem;
import com.viewpagerindicator.IconPagerAdapter;
public class NewsFeedsAdapter extends FragmentPagerAdapter implements
IconPagerAdapter {
int[] icon = null;
String[] content = null;
String[] URLs = null;
Context cont;
public NewsFeedsAdapter(FragmentManager fm, Context context) {
super(fm);
Log.i("jk", "constructor");
this.cont = context;
RssItem newsFeedAppliaction = (RssItem) cont;
/*
* Retrieving the values of the Icons and contents from the application
* class in utils package
*/
icon = newsFeedAppliaction.getICONS();
content = newsFeedAppliaction.getCONTENT();
URLs = newsFeedAppliaction.getURL();
}
/** instantiate a new fragment class */
@Override
public Fragment getItem(int position) {
Log.i("yt", "hello" + position);
return TestFragment.newInstance(position % content.length, cont);
}
@Override
public CharSequence getPageTitle(int position) {
return content[position % content.length].toUpperCase();
}
public int getIconResId(int index) {
return icon[index];
}
/** return the no of views on the basis of array items */
@Override
public int getCount() {
Log.i("hi", "length" + content.length);
return content.length;
}
}
私はこのコードを使用してアダプタを呼び出しています。これはひどい。あなたが今いる状況にあなたを置きます。あなたがしていることは、あなたが指定した日数を超えて生き残る新しい年鑑アイテムを作成することです。しかし、悪いAndroidシステムがあなたのフラグメントに再度話して、あなたのアダプターからそれを要求する必要があると判断すると、あなたはそれを「いいえ、私はしたくない!その代わりに古いフラグメントと全く同じ新しいフラグメントを与えます。
これを修正するには、フラグメントをすべて前もって膨張させてから、適切なフラグメントを返します。または、もしあなたがむしろ、しないでください。しかし、作成したインスタンスを記録しておくと、既に作成されたフラグメントを返すことができます。
私はv4のソースを精査していませんが、実際にアイテムが実際に取得されたか、システムが以前のアイテムが必要だった状態になったかを確認するために行われますrefereshed。
結論として、作成されたインスタンス - 、とりわけをフラグメント、ビュー、アクティビティとして重いオブジェクトに保持します。
class Adapter extends MyWhateverAdapter {
Fragment[] myLovelyFragments;
public Adapter() {
// Instantiate your fragments and place them into myLovelyFragments
}
public int getFragmentCount() {
return myLovelyFragments.length;
}
public Fragment getItem(int position) {
return myLovelyFragments[position];
}
}
可能であれば、サンプルコードを教えてください。 – yokees
@ Rishabh.CreatioSoftあなたは行きます。 :) – AedonEtLIRA
しかし私は1つのことを教えてくれます。私は0と1の両方をgetItemに返します.PagerAdapterCall.Soでは、1つの位置にフラグメントオブジェクトがあります。 – yokees