public class FormContentViewFragment extends Fragment implements View.OnClickListener {
private View view;
private View textView;
private TextView headerView;
private TextView fieldInstructionView;
private ImageButton backButton;
private ImageButton forwardButton;
private ProgressBar progressBar;
private ViewPager viewPager;
private int number;
private String value;
private String fieldinstruction;
private List<String> headerList = new ArrayList<String>();
private List<String> fieldInstructionList = new ArrayList<String>();
private LayoutInflater inflater;
private FormContentViewAdapter adapter;
public FormContentViewFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_form_content_view, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
backButton = (ImageButton) view.findViewById(R.id.back_button);
forwardButton = (ImageButton) view.findViewById(R.id.forward_button);
progressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
adapter = new FormContentViewAdapter();
Log.e("onActivityCreated()......","..........before set adapter........");
viewPager = (ViewPager) view.findViewById(R.id.view_pager);
viewPager.setAdapter(adapter);
Log.e("onActivityCreated()......","..........after set adapter........");
getViewContent();
}
public void getViewContent() {
MainActivity.showLoader(getActivity());
final StringRequest stringRequest = new StringRequest(Request.Method.GET, "JSON URL", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("Response", ".............." + response);
try {
JSONObject jsonObject = new JSONObject(response);
boolean status = jsonObject.getBoolean("status");
if (status == true) {
JSONArray list = jsonObject.getJSONArray("list");
for (int i = 0; i < list.length(); i++) {
JSONObject currentData = list.getJSONObject(i);
number = currentData.getInt("number");
Log.e("number", "....." + number);
value = currentData.getString("value");
Log.e("value", "....." + value);
fieldinstruction = currentData.getString("fieldinstruction");
Log.e("fieldInstruction", "......" + fieldinstruction);
headerList.add(value);
Log.e("headerList...", "......" + headerList);
fieldInstructionList.add(fieldinstruction);
Log.e("instructioList", "....." + fieldInstructionList);
Toast.makeText(getContext(), "Dispaly data", Toast.LENGTH_SHORT).show();
}
// headerView.setText(headerList.get(1));
// fieldInstructionView.setText(fieldInstructionList.toString());
}
MainActivity.hideLoader();
} catch (JSONException e) {
e.printStackTrace();
MainActivity.hideLoader();
}
Log.e("number out.....", "....." + number);
MainActivity.hideLoader();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
String errorResponse = String.valueOf(error.networkResponse.data);
Log.e("ErrorResponse", "........" + errorResponse);
MainActivity.hideLoader();
}
}) {
};
ApplicationController.getInstance().addToRequestQueue(stringRequest);
}
@Override
public void onClick(View view) {
}
class FormContentViewAdapter extends PagerAdapter {
public FormContentViewAdapter(){
Log.e("FormContentViewAdapter","..............inside....");
Log.e("headerList1...", "......" + headerList.toString());
Log.e("instructioList1...", "....." + fieldInstructionList.toString());
Log.e("FormContentViewAdapter","..............end of it....");
}
@Override
public int getCount() {
Log.e("headerList1...", "......" + headerList.toString());
Log.e("instructioList1...", "....." + fieldInstructionList.toString());
Log.e("value getCount().......","........"+number);
return number;
}
@Override
public boolean isViewFromObject(View view, Object object) {
Log.e("isViewFromObject().....","inside.........");
return false;
}
@Override
public View instantiateItem(ViewGroup container, int position) {
Log.e("instantiateItem()......","inside.........");
textView = inflater.inflate(R.layout.view_pager_content, container, false);
headerView = (TextView) textView.findViewById(R.id.header_text_view);
fieldInstructionView = (TextView) textView.findViewById(R.id.instruction_text_view);
headerView.setText(headerList.get(position));
Log.e("headerList2.....","............."+headerList.toString());
fieldInstructionView.setText(fieldInstructionList.toString());
Log.e("fieldInstructionList2........",".........."+fieldInstructionList.get(position));
container.addView(textView);
Log.e("instantiateItem()......","end of it.........");
return textView;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
}
Ошибка:Как использовать другой макет в ViewPager внутри фрагмента?
java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 0, found: 2 Pager id: com.formfilling:id/view_pager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.formfilling.FormContentViewFragment$FormContentViewAdapter
at android.support.v4.view.ViewPager.populate(ViewPager.java:1171)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1120)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1646)
У меня есть один фрагмент, в том, что ViewPager есть я хочу, чтобы установить другую раскладку к этому ViewPager Поэтому я также использовать адаптер для установки макета в ViewPager, но он бросает ошибка IllegalStateException. Я уже делаю все, что для этого, но это не working.Give ваших предложений
Опубликовать полную ошибку logcat с кодом viewpager –
использовать фрагмент adpter в viewpager для разной компоновки. –
add adapter.notifydatasetchanged() после viewPager.setAdapter (адаптер); –