0

我試圖設置一個標籤條使用滑動切換片段之間切換文檔here.它的工作,直到一個點。 gridview根據需要顯示所有圖像,但片段1和片段2都顯示相同的圖像。看起來,片段2覆蓋了圖像,因爲如果您單擊片段1中的圖像,則會彈出片段1詳細信息屏幕(即使它顯示片段2中的圖像)。FragmentPagerAdapter + BaseAdapter + UIL不更新

基本上,我需要我的ImageAdapter(BaseAdapter)爲每個單獨的片段顯示正確的圖像。如果沒有靜態元素,我不會看到第二個片段如何與第一個片段進行交互。

編輯:我試着改變畢加索和發生同樣的錯誤,所以必須在我的代碼中有東西。

Edit2:我發現這個answer,它讓我重繪網格,當一個片段變得可見,但導致明顯的閃爍,這是顯而易見的圖像是錯誤的。這個問題必須與UIL/Picasso認爲,在單獨的片段中的gridview是同一個對象(它們具有相同的圖像,但具有不同的順序)。

public void setupFragmentSwipes() { 
    mDemoCollectionPagerAdapter = 
      new DemoCollectionPagerAdapter(
        getFragmentManager()); 
    mViewPager = (ViewPager) mRootView.findViewById(R.id.pager); 
    mViewPager.setAdapter(mDemoCollectionPagerAdapter); 
} 

    public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter { 
    String[] array = getResources().getStringArray(R.array.SortOptions); 
    public DemoCollectionPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 
    @Override 
    public Fragment getItem(int i) { 
     Fragment fragment = new FragmentGrid(); 
     Bundle args = new Bundle(); 

     args.putInt("mMode", mMode); 
     args.putInt("mSortAorD", mSortAorD); 
     args.putInt("mSortType", i); 
     fragment.setArguments(args); 
     return fragment 
    } 

    @Override 
    public int getCount() { 
     return array.length; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return array[position]; 
    } 
} 

FragmentGrid

public class FragmentGrid extends Fragment { 
public int mode; 
private ArrayList<Theme> mThemes; 
private GridView listView; 
private static DisplayImageOptions options; 
protected ImageLoader imageLoader = ImageLoader.getInstance(); 
protected int mSavedPosition; 
private int sortType; 
private int sortAorD; 
@Override 
    public View onCreateView(LayoutInflater inflater, 
         ViewGroup container, Bundle savedInstanceState) { 
    // The last two arguments ensure LayoutParams are inflated 
    // properly. 
    View rootView = inflater.inflate(
      R.layout.fragment_collection_object, container, false); 
    Bundle args = getArguments(); 

    mode = args.getInt("mMode", BaseConstants.ViewModes.NORMAL); 

    sortType = args.getInt("mSortType", BaseConstants.Sort.POPULAR); 
    sortAorD = args.getInt("mSortAorD", BaseConstants.Sort.DESC); 
    listView = (GridView) rootView.findViewById(R.id.gridview2); 



    options = new DisplayImageOptions.Builder() 
      .showStubImage(R.drawable.ic_stub) 
      .showImageForEmptyUri(R.drawable.ic_error) 
      .showImageOnFail(R.drawable.ic_error) 
      .cacheOnDisc(true) 
      .imageScaleType(ImageScaleType.EXACTLY) 
      .bitmapConfig(Bitmap.Config.RGB_565) 
      .build(); 



    return rootView; 
} 
@Override 
public void onResume() { 
    super.onResume(); 
    listView.setSelection(mSavedPosition); 
    ThemeManager tm = new ThemeManager(getActivity().getApplicationContext()); 
    mThemes = tm.getModifiedThemeList(mode); 
    mThemes = tm.compare(sortType, sortAorD, checkIfTesting()); 
    listView.setAdapter(new ImageAdapter(mThemes)); 
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      mSavedPosition = position; 
      Intent intent = new Intent(getActivity(), ImagePagerActivity.class); 
      intent.putExtra("mMode", mode); 
      intent.putExtra("mSortAorD", sortAorD); 
      intent.putExtra("mSortType", sortType); 
      intent.putExtra("mPosition", position); 
      startActivity(intent); 
     } 
    }); 

} 
public class ImageAdapter extends BaseAdapter { 
    ArrayList<Theme> imageAdapterThemeList; 
    public ImageAdapter(ArrayList<Theme> themes) { 
     imageAdapterThemeList = themes; 
    } 
    @Override 
    public int getCount() { 
     int result = 0; 
     if (imageAdapterThemeList != null) { 
      result = imageAdapterThemeList.size(); 
     } 
     return result; 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView; 

     if (convertView == null) { 
      imageView = (ImageView) getActivity().getLayoutInflater().inflate(R.layout.item_grid_image, parent, false); 
     } else { 
      imageView = (ImageView) convertView; 
     } 

     Theme theme = imageAdapterThemeList.get(position); 

     imageLoader.displayImage(theme.getImageURL(), imageView, options); 

     return imageView; 
    } 
} 

fragment_collection_object.xml

<GridView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridview2" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center" 
android:horizontalSpacing="4dip" 
android:numColumns="auto_fit" 
android:columnWidth="150dip" 
android:scrollbars="vertical" 
android:stretchMode="columnWidth" 
android:verticalSpacing="4dip" 
android:padding="4dip" /> 

回答

0

好吧,我找到了答案,但我不知道爲什麼。我不得不改變從以下方面:

ArrayList<Theme> imageAdapterThemeList; 
public ImageAdapter(ArrayList<Theme> themes) { 
    imageAdapterThemeList = themes; 
} 

到:

ArrayList<Theme> imageAdapterThemeList = new ArrayList<Theme>(); 

    public ImageAdapter(ArrayList<Theme> themes) { 

     for (int i = 0; i< themes.size() ;i++) { 
      imageAdapterThemeList.add(i, themes.get(i)); 
     } 

    } 

我想,而不是創建一個新的ArrayList我只是指着舊的。這種新方法實際上重新創建它,但我確定它不是非常有效。

+1

「我認爲,而不是創建一個新的ArrayList,我只是指向舊的」 - 這將取決於您對getModifiedThemeList()的實現。鑑於你如何使用這個'ArrayList ',你確實需要數組列表的單獨副本。你可以更容易地使用拷貝構造函數('new ArrayList (themes)')。 – CommonsWare

+0

好點。感謝您的幫助。 – easycheese

0

我遇到的發音很相似,這在不久前的一個問題。事實證明,這是交換頁面時使用的動畫,與傳呼機或適配器完全無關。基本上動畫導致fragment1位於fragment2的頂部,但是完全透明,因此用戶認爲他們正在碰到fragment2,但事件被fragment1接收到,這使得它的行爲非常類似於適配器正在返回錯誤的片段。

簡而言之,如果您正在使用動畫(特別是z-順序變更動畫),請嘗試禁用它們並查看問題是否消失。如果是這樣,你的動畫有問題。

希望有幫助, 好運。

+0

沒有動畫,但好的想法。我知道fragmentpageradapter使片段可見和不可見 – easycheese

1

你的兩個片斷實例之間唯一的區別是,您可以根據網頁上有你的論點mSortTypeBundle設置爲01。您只能用它來設置mThemes,這在您的ImageAdapter中似乎沒有使用。

因此,如果您期待您的兩個ImageAdapter實例返回單獨的結果,則需要注意mSortType或根據頁面的不同而有所不同。

+0

mThemes被創建,然後根據mSortType(通過比較方法)進行排序。 mThemes然後被傳入ImageAdapter並被稱爲imageAdapterThemeList。排序類型0和1導致主題不同,因此每個ImageAdapter應該不同。所顯示的圖像總是似乎來自片段0後創建的片段1.如果從片段0單擊一個,它會顯示正確的信息雖然。 – easycheese

+1

@easycheese:我的歉意 - 很難跟蹤不斷變化的變量名稱。您可以嘗試在'getView()'中記錄'theme.getImageURL()'並查看是否給出了線索​​。 – CommonsWare

+0

你確實讓我在正確的方向思考,我能夠找到問題。我不明白其中的根本原因,也許你可以看看答案並告訴我我錯過了什麼。 – easycheese

相關問題