我的ViewPager
片段中存在问题
当我从导航视图中打开片段时,我的“ListView”仅在我有一个标签时起作用
当我放置2个或更多选项卡时,错误的列表视图仅出现在第二个选项卡中。
这是我的片段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 :(得分:0)
原因是在ViewPagerAdapter
内部,您对每个页面使用相同的元素(ListView
等等)。这样只显示最后一个实例化的页面。
尽量不要在函数public Object instantiateItem(ViewGroup container, int position)
中重用它们,并在方法中创建它们。
修改强>
帮助您了解应该做出哪些更改的指南:
您更新的课程字段:
private Context ctx;
private LayoutInflater layoutInflater;
ArrayList<JournalDate> arrayList;
ArrayList<JournalFragmentDayItem> dayItemArrayList;
JournalFragmentDayItem dayItem;
JournalFragmentDayAdapter dayAdapter;
更新方法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;
}