The pits encountered in the course of the project

Keywords: network JSON Fragment simulator

Viewpager's adapter

@Override
        public Object instantiateItem(ViewGroup container, int position) {
            BasePager basePager=basePagers.get(position);//Examples of each subpage
            View rootView=basePager.rootView;//Represents individual sub-pages
            //Call the initData () method for each page
            basePager.initData();

            container.addView(rootView);
            return rootView;//Note that returning to rootView is the sub-page

        }

Note that it returns its page, not its instance.

Shield viewPager sliding, because you want to slide out the menu left first, without shielding viewPager, you will slide left to its left page instead of the left menu.

Method: At this point, you can only customize a ViewPager and inherit the way ViewPager overwrites it.

 @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return true;
    }

This method is a screen touch event distributor, the return value is changed to true, that is, to consume the touch event, and to put it plainly, the touch event will not be executed.

Button click switch page

rgMain.setOnCheckedChangeListener(new MyOnCheckedchangeListener());

 class MyOnCheckedchangeListener implements 

RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int 

checkedId) {
            switch (checkedId) {
                case  R.id.rb_home:
                     viewPager.setCurrentItem(0,false);//false sets the animation effect without shaking
                    break;
                case  R.id.rb_newscenter:
                    viewPager.setCurrentItem(1,false);
                    break;
                case  R.id.rb_smartservic:
                    viewPager.setCurrentItem(2,false);
                    break;
                case  R.id.rb_setting:
                    viewPager.setCurrentItem(3,false);
                    break;
            }
        }
    }

Shielding ViewPager Data Preloading

There is a phenomenon, I just opened app, it will automatically initialize the first two pages of data, the main page and the news center page, when I click the second news center button, it initializes the third page, click the third button to initialize the fourth page, so we have to shield the viewpager data preloader. Method: Let initData () not be invoked, the initData () of each sub-page would have been invoked in the RadioButton listener. In fact, this is not true. If you choose the left menu, it will not be initialized, so call it directly in the viewpager, call it in the view listener, not the adapter. OK. Write an OnPage ChangListener that handles viewPager page sliding

  //Listen for a page to be selected and initialize the data of the corresponding page
        viewPager.addOnPageChangeListener(new 

MyaddOnPageChangeListener());

 private class MyaddOnPageChangeListener implements 

ViewPager.OnPageChangeListener {
        //Called when the page is sliding
        @Override
        public void onPageScrolled(int position, float positionOffset, int 

positionOffsetPixels) {

        }
        //Page skips and calls later
        @Override
        public void onPageSelected(int position) {
           basePagers.get(position).initData();
        }
        //Called when the state is changed
        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

However, the first page was not initialized when it first entered, so the first page was called when initializing the data.

initData () of a page

Shielding a specified page cannot drag and drop menus, because sometimes menus are designed for a page, not all pages.

It's a bit awkward. My original idea was to set MainActivity as not to drag. That's what I meant.
Setting drag-and-drop on a given page is not feasible. Once you touch the specified page, you can drag and-drop on the back page. So you need to set one touch event for each page. Also, MainActivity (almost global) is set to drag and-drop, otherwise the menu button cannot be pushed.

Refactoring (that is, extracting code into a void method)
alt +shift+m

Manual analysis

Caching data

In Cache Class

 public static void putString(Context context, String key, String value) {
        SharedPreferences sp = context.getSharedPreferences("NewsMessage", 

Context.MODE_PRIVATE);
        sp.edit().putString(key,value).commit();
    }

Getting caches before networking requests

 String saveJson=CacheUtils.getString

(context,Constants.NEWSCENTER_PAGER_URL);
        if(!TextUtils.isEmpty(saveJson)) {
            processData(saveJson);
        }

Caching data

Before networking requests, check the getString cache to see if there are previously saved Json s, and if so, parse them directly.
Data, if not for networking requests (putString caches data in the way that networking requests succeed)

Configuring networking requests

Network Address

http://192.168.155.1:8080/web_home/static/api/news/categories.json
http://10.0.2.0:8080 Local simulator, access to local tomcat server
http://wangyi.butterfly.mopaasapp.com/

Write a constant class Constants.java. Configure networking request address

public class Constants {
// ip and port of networking request (server address)
public static final String BASE_URL= "URL address";

//Network address of News Centre
public static final String NEWSCENTER_PAGER_URL=BASE_URL+"  ";

Request network connection in NewsCenter Pager, get data, get MainActivity through context, get LeftmenuFragment through MainActivity according to tag, call setData () method to transfer data to LeftmenuFragment

After the parsing data of the news center is successful, add the details page to the news center
detailBasePagers = new ArrayList<>();

   detailBasePagers.add(new NewsMenuDetailPager(context));
    detailBasePagers.add(new TopicMenuDetailPager(context));
    detailBasePagers.add( new PhotosMenuDetailPager(context));
    detailBasePagers.add(new InteracMenuDetailPager(context));

// Step by step, get Content Fragment in MainActivity, and then in Content Fragment

Get the NewsCenterPager, and then switch the details page in NewsCenterPager
ContentFragment contentFragment= mainActivity.getContentFragment();
NewsCenterPager newsCenterPager=

contentFragment.getNewsCenterPager();
newsCenterPager.swichPager(prePosition);

Use of ViewPager Indicator

Posted by ogge1 on Sat, 13 Jul 2019 15:47:21 -0700