2017-02-05 142 views
1

我需要一些建議。顯示白頁的ViewPager。我搜查了很多,但我找不到解決方案。
我試圖做出簡單的按鈕,但我得到了相同的輸出。我認爲這不是圖像問題。 這是我的代碼。ViewPager沒有顯示任何東西 - android

public class IntroduceArticleFragment extends Fragment { 

    View view; 
    String depart; 
    String path; 
    int num; 

    CustomPagerAdapter customPagerAdapter; 
    ViewPager viewPager; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     view = inflater.inflate(R.layout.fragment_introduce_article, container, false); 
     view.setBackgroundColor(Color.WHITE); 

     depart = getArguments().getString("depart"); 
     create(); 

     //customPagerAdapter = new CustomPagerAdapter(getActivity()); 
     viewPager = (ViewPager)view.findViewById(R.id.pager); 
     viewPager.setAdapter(customPagerAdapter); 


     return view; 
    } 

    void create(){ 
     DBHelper dbHelper = new DBHelper(getActivity()); 
     path = dbHelper.getContentsPath(depart); 
     num = dbHelper.getNum(depart); 

     customPagerAdapter = new CustomPagerAdapter(getActivity()); 
     customPagerAdapter.mResources = new int[num]; 
     for(int i=0;i<num;i++) { 
      String uri = "@drawable/" + path + "_" + Integer.toString(i); 
      int imageResource = getActivity().getResources().getIdentifier(uri, "drawabale", getActivity().getPackageName()); 
      customPagerAdapter.mResources[i] = imageResource; 
     } 
    } 
} 


public class CustomPagerAdapter extends PagerAdapter { 
    Context mContext; 

    public int[] mResources; 

    public CustomPagerAdapter(Context context) { 
     super(); 
     mContext = context; 
    } 

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

    @Override 
    public boolean isViewFromObject(View view, Object object) { 
     return view == ((LinearLayout) object); 
    } 

    @Override 
    public Object instantiateItem(ViewGroup container, int position) { 
     LayoutInflater mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View itemView = mLayoutInflater.inflate(R.layout.pager_item_0, container, false); 

     ImageView imageView = (ImageView) itemView.findViewById(R.id.pagerimageView_0); 
     imageView.setImageResource(mResources[position]); 

     container.addView(itemView); 

     return itemView; 
    } 

    @Override 
    public void destroyItem(ViewGroup container, int position, Object object) { 
     container.removeView((LinearLayout) object); 
    } 

    @Override 
    public void startUpdate(ViewGroup container) { 
     super.startUpdate(container); 
    } 

    @Override 
    public void finishUpdate(ViewGroup container) { 
     super.finishUpdate(container); 
    } 
} 

,這裏是爲ArticleFragment

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" > 

       <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/pager" 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:visibility="visible"> 
       </android.support.v4.view.ViewPager> 


      </LinearLayout> 
     </ScrollView> 
    </LinearLayout> 

XML代碼和這裏的XML代碼

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <Button 
      android:text="Button" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/button" /> 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="fitCenter" 
      android:id="@+id/pagerimageView_0" /> 
    </LinearLayout> 

回答

0

好吧...首先,您必須將ScrollView的小孩的android:layout_height設置爲wrap_content

其次,請考慮您無法將的ViewPager設置爲wrap_content。除非您使用自定義ViewPager。例如,你可以檢查this answer

通過ScrollView設置android:fillViewport爲true,ScrollView的孩子的身高將自動設置爲match_parent直至其高度變得比屏幕更高。

而我不明白爲什麼你設置ViewPager的高度爲0dp,並期望它顯示一些東西!

現在你的佈局應該是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/pager" 
       android:layout_width="match_parent" 
       android:layout_height="match_pranet" 
       android:visibility="visible"> 
      </android.support.v4.view.ViewPager> 


     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 
+0

謝謝。我解決了問題。 – jaechang

0

你已完成了所有的錯誤,使用此代碼

import android.support.v4.app.Fragment; 
    import android.support.v4.app.FragmentManager; 
    import android.support.v4.app.FragmentStatePagerAdapter; 


    public class CustomPagerAdapter extends FragmentStatePagerAdapter { 

     public int[] mResources; 

     public NewsAdapter(FragmentManager fm, int[] mResources) { 
      super(fm); 

      this.mNumOfTabs = mResources.length(); 
      this.mResources = mResources; 
     } 



     @Override 
     public Fragment getItem(int position) { 
      return ContentFragment.newInstance(mResources[position]); 
     } 

     @Override 
     public int getCount() { 
      return mNumOfTabs; 
     } 
    } 



public class ContentFragment extends Fragment { 


    private Context context; 

    private int resource_id; 

    public static ContentFragment newInstance(int resourdid) { 
     Bundle args = new Bundle(); 
     args.putInt("resource_id", resourdid); 
     ContentFragment fragment = new ContentFragment(); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     resource_id = getArguments().getInt("resource_id"); 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(in.newswallet.R.layout.content_row, container, false); 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     initalize(); 
    } 


    private void initalize(){ 
     View itemView = getView(); 
     ImageView imageView = (ImageView) itemView.findViewById(R.id.pagerimageView_0); 
     imageView.setImageResource(resource_id);  
    } 



    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     this.context=context; 
    } 





} 
+1

如果你能提,爲什麼OP「完全搞錯了」,並解釋爲什麼你的解決方案有效,這將是偉大的! –

+0

我知道我處理mResources的方式有一些問題。但我不明白爲什麼需要擴展片段。你能幫助我嗎? – jaechang

0
<android.support.v4.view.ViewPager  xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/pager" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:visibility="visible"> 
      </android.support.v4.view.ViewPager> 

嘗試將機器人改變:layout_height = 「0dp」爲某個值等機器人:layout_height = 「50dp」

給重量到佈局 機器人:layout_weight = 「1」

+0

謝謝。我解決了問題。 – jaechang