I have a question about Android viewpager.

Asked 2 years ago, Updated 2 years ago, 164 views

The Viewpager page is on page 5 All communicate with http to receive data. But the code seems to overlap too much, so is there a way to deal with it separately? I can't think of anything

public class ViewPager1 extends Fragment{
ViewGroup rootView;
String title;

@Override
public void onDetach() {
    super.onDetach();
}

@Nullable
@Override
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    rootView = (ViewGroup) inflater.inflate(R.layout.view_pager1, container, false);

    responsData();



    Button button = rootView.findViewById(R.id.viewpager1_button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


        }
    });

    return rootView;
}
public void responsData() {
    String url = "http://boostcourse-appapi.connect.or.kr:10000/movie/readMovieList?type=1";

    StringRequest stringRequest = new StringRequest(
            Request.Method.GET,
            url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    processRespons(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }
    );
    stringRequest.setShouldCache(false);
    AppHelper.requestQueue.add(stringRequest);
}

public void processRespons(String response) {
    Gson gson = new Gson();

    ResponseInfo info = gson.fromJson(response, ResponseInfo.class);
    MovieList movieList = gson.fromJson(response, MovieList.class);
    if (info.code == 200) {
        MovieInfo movieInfo = movieList.result.get(1);
        title = movieInfo.title;
        TextView text = rootView.findViewById(R.id.viewpager1_title);

        if (title != null) {
            text.setText(title);
        } } else {
            Toast.makeText(getContext(), "data not found Check your Internet connection", Toast.LENGTH_LONG).show();
        }

    }
}

=============================================================================

public class MainActivity extends AppCompatActivity {
MovieInfoFragment movieInfoFragment;
String title;
String resultOk;
ViewPagerAdapter adapter;
ViewPager viewPager;
ViewPager1 viewPager1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    movieInfoFragment = new MovieInfoFragment();

    viewPager = findViewById(R.id.viewPager);
    adapter = new ViewPagerAdapter(getSupportFragmentManager());

    if (AppHelper.requestQueue == null) {
        AppHelper.requestQueue = Volley.newRequestQueue(getApplicationContext());
    }
    responsData();

    viewPager1 = new ViewPager1();
    adapter.addItems(viewPager1);
    Bundle bundle = new Bundle();
    bundle.putString("title",title);
    viewPager1.setArguments(bundle);
    ViewPager2 viewPager2 = new ViewPager2();
    adapter.addItems(viewPager2);
    ViewPager3 viewPager3 = new ViewPager3();
    adapter.addItems(viewPager3);
    ViewPager4 viewPager4 = new ViewPager4();
    adapter.addItems(viewPager4);
    ViewPager5 viewPager5 = new ViewPager5();
    adapter.addItems(viewPager5);


    viewPager.setAdapter(adapter);


}

public void responsData() {
    String url = "http://boostcourse-appapi.connect.or.kr:10000/movie/readMovieList?type=1";

    StringRequest stringRequest = new StringRequest(
            Request.Method.GET,
            url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    processRespons(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }
    );
    stringRequest.setShouldCache(false);
    AppHelper.requestQueue.add(stringRequest);
}

public void processRespons(String response) {
    Gson gson = new Gson();

    ResponseInfo info = gson.fromJson(response, ResponseInfo.class);
    MovieList movieList = gson.fromJson(response, MovieList.class);
    if (info.code == 200) {
        MovieInfo movieInfo = movieList.result.get(1);
        title = movieInfo.title;
    }
}

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    ArrayList<Fragment> items = new ArrayList<Fragment>();


    public void addItems(Fragment fragment) {
        items.add(fragment);
    }

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return items.get(position);
    }

    @Override
    public int getCount() {
        return items.size();
    }
}
}

android-viewpager android fragment volley gson

2022-09-22 17:57

1 Answers

I think we need to take into account the background and so on.

You can try the Repository pattern. If you create a particular Repository class and you create a related method in ViewPager to make an http request for Repository class, you can think of a way to have data in Repository and hand it over through any Listener.

Alternatively, use LiveData and ViewModel.


2022-09-22 17:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.