android - Selepas memuat semula dari rangkaian, gaya kawalan adalah salah.
PHP中文网
PHP中文网 2017-05-16 13:25:00
0
1
691

Saya tetapkan halaman paparan menjadi besar di tengah dan kecil di kedua-dua belah. Walau bagaimanapun, apabila permintaan rangkaian pertama kembali, yang di tengah adalah sebesar yang di sebelah kanan.
Saya telah meletakkannya dalam sub-benang, tetapi ia masih tidak berfungsi. Semoga Allah membalasnya. . .

public class ImpressCardFragment extends Fragment {


    @BindView(R.id.vp_pager)
    ViewPager mPager;
    private List<Impress.ObjectBean.DataBean> mList;
    private Unbinder bind;
    private List<PageFragment> fragmentList;
    private ImpressCardAdapter cartAdapter;
    private ACache aCache;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_impress_card, container, false);
        bind = ButterKnife.bind(this, rootView);
        transformerPager();
        aCache = ACache.get(getContext());
        mList = new ArrayList<>();
        DisplayMetrics metric = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels;     // 屏幕宽度(像素)
        int height = metric.heightPixels;   // 屏幕高度(像素)
        float density = metric.density;      // 屏幕密度(0.75 / 1.0 / 1.5)
        int densityDpi = metric.densityDpi;
        mPager.setOffscreenPageLimit(4);
        mPager.setPageMargin(-width / 4);
        fragmentList = new ArrayList<>();
        cartAdapter = new ImpressCardAdapter(getChildFragmentManager(), getContext(), fragmentList);
        mPager.setAdapter(cartAdapter);
        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        //缓存
        Impress impress = (Impress) aCache.getAsObject("impressCart");
//        if (impress == null) {
//            getData();
//        } else {
//
//            mList.clear();
//            mList.addAll(impress.object.get(0).data);
//            fragmentList.clear();
//            for (int i = 0; i < mList.size(); i++) {
//                fragmentList.add(new PageFragment(mList.get(i)));
//            }
//            cartAdapter.notifyDataSetChanged();
//        }
        handler.post(mRunnable);
    }

    /**
     * viewPager的转换效果
     */
    void transformerPager() {
        mPager.setPageTransformer(true, new ViewPager.PageTransformer() {
            public float position;

            public void transformPage(View page, float position) {
                this.position = position;
                if (position < -1) {
                    // This page is way off-screen to the left.
                } else if (position <= 1) {
                    if (position < 0) {
                        if (position < -0.4f)
                            position = -0.4f;
                        page.setScaleY(1.4f + position);
                        page.setScaleX(1.4f + position);
                    } else {
                        if (position > 0.4)
                            position = 0.4f;
                        page.setScaleY(1.4f - position);
                        page.setScaleX(1.4f - position);
                    }
                } else {
                    // This page is way off-screen to the right.
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        bind.unbind();
        handler.removeCallbacks(mRunnable);
        super.onDestroyView();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    Impress body = (Impress) msg.obj;
                    aCache.put("impressCart", body, 2);
                    mList.clear();
                    mList.addAll(body.object.get(0).data);
                    fragmentList.clear();
                    for (int i = 0; i < mList.size(); i++) {
                        fragmentList.add(new PageFragment(mList.get(i)));
                    }
                    cartAdapter.notifyDataSetChanged();
                    break;
            }

        }
    };
  private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            HttpClient.createRequest(HttpTask.class).impressIndex(String.valueOf(0), String.valueOf(10)).enqueue(new Callback<Impress>() {

                @Override
                public void onResponse(Call<Impress> call, Response<Impress> response) {
                    if (response.body().code.equals(Constants.REQUESTID_SUCCESS)) {
                        Impress body = response.body();
                        Message message = handler.obtainMessage(1);
                        message.obj = body;
                        handler.sendMessage(message);
                    }
                }

                @Override
                public void onFailure(Call<Impress> call, Throwable t) {
                    Toast.makeText(getContext(), "服务器故障", Toast.LENGTH_SHORT).show();
                }
            });
        }
    };
}

PHP中文网
PHP中文网

认证高级PHP讲师

membalas semua(1)
習慣沉默

Rujuk perpustakaan ini: https://github.com/hongyangAn...

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan