2016-09-02 6 views
0

私のViewPagerに問題があります。
ナビゲーションビューからフラグメントを開くと、私の 'ListView'は1つのタブでしか動作しません。
2つ以上のタブを配置すると、間違ったリストビューが2番目のタブにのみ表示されます。ここで ViewPagerがListViewでコアctryを動作しない

は私のフラグメント ViewPagerです:

public class JournaFragment extends Fragment{ 

private SlidingTabLayout mSlidingTabLayout; 
private ViewPager mViewPager; 

JournalDate journalDate; 
ArrayList<JournalDate> monthArray ; 
JournalSwipeAdapter monthAdapter; 

public JournaFragment(){} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.fragment_journal, null); 

    mViewPager = (ViewPager) v.findViewById(R.id.viewpager); 
    journalDate = new JournalDate(); 
    monthArray = journalDate.getMounthForTable(); 
    monthAdapter = new JournalSwipeAdapter(ParentActivity.getContext(),monthArray); 
    mViewPager.setAdapter(monthAdapter); 



    mSlidingTabLayout = (SlidingTabLayout) v.findViewById(R.id.sliding_tabs); 
    mSlidingTabLayout.setViewPager(mViewPager); 

    //System.out.println(" CurrentItem :" + mViewPager.getCurrentItem()); 
    monthAdapter.getLessonsForJournal(mViewPager.getCurrentItem()); 

    monthAdapter.notifyDataSetChanged(); 

    return v; 
} 




@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 

} 

とアダプタがPagerAdapterを拡張:

public class JournalSwipeAdapter extends PagerAdapter { 

private Context ctx; 
private LayoutInflater layoutInflater; 
ArrayList<JournalDate> arrayList; 

ProgressBar pg; 
TextView title; 
TextView noJournal; 
TextView indexPage; 

ListView dayListView; 
ArrayList<JournalFragmentDayItem> dayItemArrayList; 
JournalFragmentDayItem dayItem; 
JournalFragmentDayAdapter dayAdapter; 

public JournalSwipeAdapter(Context ctx, ArrayList<JournalDate> arrayList) { 
    this.ctx = ctx; 
    this.arrayList = arrayList; 
} 

@Override 
public int getCount() { 
    if (arrayList == null) { 
     return 0; 

    } else 
     return arrayList.size(); 

} 

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


public JournalDate getItem(int position) { 
    return arrayList.get(position); 
} 

@Override 
public int getItemPosition(Object object) { 
    return super.getItemPosition(object); 
} 

@Override 
public CharSequence getPageTitle(int position) { 
    switch (arrayList.get(position).calendar) { 
     case (0): 
      arrayList.get(position).month = "Январь"; 
      break; 
     case (1): 
      arrayList.get(position).month = "Февраль"; 
      break; 
     case (2): 
      arrayList.get(position).month = "Март"; 
      break; 
     case (3): 
      arrayList.get(position).month = "Апрель"; 
      break; 
     case (4): 
      arrayList.get(position).month = "Май"; 
      break; 
     case (5): 
      arrayList.get(position).month = "Июнь"; 
      break; 
     case (6): 
      arrayList.get(position).month = "Июль"; 
      break; 
     case (7): 
      arrayList.get(position).month = "Август"; 
      break; 
     case (8): 
      arrayList.get(position).month = "Сентябрь"; 
      break; 
     case (9): 
      arrayList.get(position).month = "Октябрь"; 
      break; 
     case (10): 
      arrayList.get(position).month = "Ноябрь"; 
      break; 
     case (11): 
      arrayList.get(position).month = "Декабрь"; 
      break; 
    } 
    return arrayList.get(position).month; 
} 

@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    // Inflate a new layout from our resources 
    layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = layoutInflater.inflate(R.layout.fragment_jourlan_swipe_layout, container, false); 
    // Add the newly created View to the ViewPager 
    container.addView(view); 

    // Retrieve a TextView from the inflated View, and update it's text 
    title = (TextView) view.findViewById(R.id.item_title); 
    pg = (ProgressBar) view.findViewById(R.id.progressBar); 
    dayListView = (ListView) view.findViewById(R.id.fragment_journal_swipe_list); 
    noJournal = (TextView)view.findViewById(R.id.fragment_journal_swipe_nojournal); 
    indexPage = (TextView)view.findViewById(R.id.item_title); 
    view.setTag("myView"+position); 
    noJournal.setVisibility(View.GONE); 

    // Return the View 
    return view; 
} 


public void getLessonsForJournal(final int position) { 
    HtmlRequest htmlRequest; 
    RequestQueue requestQueue; 
    // System.out.println(" Position: "+ position); 
    Map<String, String> params = new HashMap<>(); 
    params.put("studentId", "5"); 
    params.put("year", String.valueOf(arrayList.get(position).year)); 
    params.put("mounth", String.valueOf(arrayList.get(position).calendar + 1)); 

    htmlRequest = new HtmlRequest(Request.Method.POST, Links.getJournalLessons, params, "UTF-8", 
      new Response.Listener<String>() { 

       @Override 
       public void onResponse(String response) { 

        // System.out.println(" " +response); 
        if (response.equals("false")) { 
         // System.out.println("Failed Lessons "); 

        } else { 

         arrayList.get(position).responseForLessons = response; 
         notifyDataSetChanged(); 

         dayItem = new JournalFragmentDayItem(); 
         dayItemArrayList = dayItem.getDayArray(arrayList.get(position).responseForLessons); 
         dayAdapter = new JournalFragmentDayAdapter(ParentActivity.getContext(), dayItemArrayList); 
         dayListView.setAdapter(dayAdapter); 

         pg.setVisibility(View.GONE); 
         dayListView.setVisibility(View.VISIBLE); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Log.e("eror", error.getMessage() + ""); 
        Toast.makeText(ParentActivity.getContext(), "" + error.getMessage(), Toast.LENGTH_LONG).show(); 
       } 
      }); 

    htmlRequest.setTag("TAG"); 
    requestQueue = Volley.newRequestQueue(ParentActivity.getContext()); 
    requestQueue.add(htmlRequest); 


} 


/** 
* Destroy the item from the ViewPager. In our case this is simply 
* removing the View. 
*/ 
@Override 
public void destroyItem(ViewGroup container, int position, Object object) { 
    container.removeView((View) object); 
} 
} 

答えて

0

この理由はViewPagerAdapter内部で使用すると、すべてのページ(ListView、などのために同じ要素を使用していることです...)。このようにして、最後にインスタンス化されたページのみが表示されます。

ファンクションpublic Object instantiateItem(ViewGroup container, int position)内で再利用しないでください。あなたにできる

@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    // Inflate a new layout from our resources 
    layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = layoutInflater.inflate(R.layout.fragment_jourlan_swipe_layout, container, false); 
    // Add the newly created View to the ViewPager 
    container.addView(view); 

    // Retrieve a TextView from the inflated View, and update it's text 
    TextView title = (TextView) view.findViewById(R.id.item_title); 
    ProgressBar pg = (ProgressBar) view.findViewById(R.id.progressBar); 
    ListView dayListView = (ListView) view.findViewById(R.id.fragment_journal_swipe_list); 
    TextView noJournal = (TextView) view.findViewById(R.id.fragment_journal_swipe_nojournal); 
    TextView indexPage = (TextView) view.findViewById(R.id.item_title); 
    view.setTag("myView" + position); 
    noJournal.setVisibility(View.GONE); 

    // Return the View 
    return view; 
} 
+0

更新されたクラスのフィールド:

private Context ctx; private LayoutInflater layoutInflater; ArrayList<JournalDate> arrayList; ArrayList<JournalFragmentDayItem> dayItemArrayList; JournalFragmentDayItem dayItem; JournalFragmentDayAdapter dayAdapter; 

更新方法public Object instantiateItem(ViewGroup container, int position)

EDIT

ガイドラインは、あなたが行うべき変更内容を理解するためにどのような例を示しますか?私は本当に初めてpageadapterを使用します – O6e36ashenII

+0

解決策を試しましたか? –

+0

はい。私はそれが良い方法だと思う。私はこの解決策にいくつかのバグを持っています。それを解決する時間が必要です – O6e36ashenII

関連する問題