2016-09-02 5 views
0

조각에있는 ViewPager에 문제가 있습니다.
탐색보기에서 단편을 열면 'ListView'는 하나의 탭에서만 작동합니다.
두 개 이상의 탭을 넣으면 잘못된 목록보기가 두 번째 탭에만 나타납니다. 여기 ViewPager가 ListView에서 corectry를 작동하지 않습니다.

내 조각 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)

편집

가이드 라인은해야한다 변화를 이해하는 데 도움 어떤 예를 보여 주겠습니까? 나는 진짜로 처음으로 pageadapter를 사용한다 – O6e36ashenII

+0

당신은 내 편집 된 답을 해본 적이 있습니까? –

+0

예. 나는 그것이 좋은 방법이라고 생각한다. 이 솔루션에는 몇 가지 버그가 있습니다. 그것을 해결할 시간이 필요합니다. – O6e36ashenII

관련 문제