分页/轮播

时间:2017-10-19 23:17:55

标签: javascript android

我试图用所有相关的答案解决这个谜题,但我似乎无法连接点。

我的MainActivity(下面的代码)中有4个选项卡可以正常工作,在每个选项卡中显示产品列表。

单击产品时,记录的ID将传递给ViewProduct Activity(下面的代码),其中id用于填充产品的详细信息,并再次通过bundle传递给4个片段以显示项目与该产品有关。

所有这些“List Loading”和“Passing”参数都运行正常,但ViewPager / SectionsAdaptor表现得很有趣......

在第一个tab1,tab3和tab4是好的,但tab2是空白的,然后当你“选择”到tab4并返回到tab1,tab1是空白等等。似乎页面/片段被加载两次这可能成为问题的原因。

因此,单独所有片段都能正常工作。为什么会这样? 请帮忙!

注意:我遗漏了一些不相关的代码,例如设置TextViews等并用注释替换它,所以请注意注释并建议扩展哪个Activity

谢谢

这是我的MainActivity.java

      public class MainActivity extends AppCompatActivity {

            private SectionsPagerAdapter mSectionsPagerAdapter;
            private AddUpdateProductOps productOps;
            private ViewPager mViewPager;

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

                this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);

   mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {

                getMenuInflater().inflate(R.menu.menu_main, menu);
                return true;
            }

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                int id = item.getItemId();

                if (id == R.id.action_settings) {
                    return true;
                }

                return super.onOptionsItemSelected(item);
            }

            public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

                @Override

                public Fragment getItem(int position) {

                    switch (position) {
                        case 0:
                            Products tab1 = new Products();
                            return tab1;
                        case 1:
                            Sales tab2 = new Sales();
                            return tab2;
                        case 2:
                            Orders tab3 = new orders();
                            return tab3;
                        case 3:
                            Clients tab4 = new Clients();
                            return tab4;
                        default:
                        return null;
                    }
                }

                @Override
                public int getCount() {
                    // Show 3 total pages.
                    return 4;
                }

                @Override
                public CharSequence getPageTitle(int position) {
                    switch (position) {
                        case 0:
                            return "Products";
                        case 1:
                            return "Sales";
                        case 2:
                            return "Orders";
                        case 3:
                            return "Clients";
                    }
                    return null;
                }

            }

**这是我的ViewProduct.jave **

 public class ViewProduct extends AppCompatActivity {

        private Button actionButton1;
        private Product oldProduct;
        private long productId;
        private AddUpdateProductOps productOps;
        private TextView EditTextNo;
        private TextView EditTextOrigin, ..., ..., ..., ...;
        private TextView tv_banner_txt;

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

            final Context[] context = new Context[1];
            productId = getIntent().getLongExtra(EXTRA_PRODUCT_ID,0);
            if (productId < 1){
                productId = Long.parseLong(getIntent().getStringExtra(EXTRA_PRODUCT_IDE));
            }

            oldProduct = new Product();
            productOps = new AddUpdateProductOps(this);

    >Some TextViews are set over here.....(not Included) - findViewById(....) - No >Problem

            actionButton1.setText("EDIT");
            tv_banner_txt.setText("VIEW THE PRODUCT");
            initializeProduct(productId);

         mSectionsPagerAdapter = new ViewProduct.SectionsPagerAdapter(getSupportFragmentManager());

            mViewPager = (ViewPager) findViewById(R.id.container);
            mViewPager.setAdapter(mSectionsPagerAdapter);

            TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
            tabLayout.setupWithViewPager(mViewPager,false);

            GradientDrawable border = new GradientDrawable();
            border.setStroke(7, 0xFF000000); //black border with full opacity'
            border.setCornerRadius(10);
            if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                tabLayout.setBackgroundDrawable(border);
            } else {
                tabLayout.setBackground(border);
            }

            actionButton1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {

                    context[0] = v.getContext();
                    Intent i = new Intent(ViewProduct.this, AddUpdateProduct.class);
                    i.putExtra(EXTRA_ADD_UPDATE, "Update");
                    i.putExtra(EXTRA_PRODUCT_ID, productId);
                    startActivity(i);
                    finish();
                }
            });

            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    context[0] = v.getContext();
                    Intent i = new Intent(ViewProduct.this, AddUpdateEvf.class);
                    i.putExtra(EXTRA_ADD_UPDATE, "Add");
                    i.putExtra(EXTRA_PRODUCT_NO, oldProduct.getProductNo());
                    i.putExtra(EXTRA_PRODUCT_ID, productId);
                    startActivityForResult(i,12);
                    finish();
                }
            });
        }

        private void initializeProduct(long productId) {

            oldProduct = productOps.getProduct(productId); 
            EditTextNo.setText(oldProduct.getProductNo());
            EditTextOrigin.setText(oldProduct.getOrigin());
            tv_cat.setText(oldProduct.getCat);
            tv_Weight.setText(pldProduct.getWeight);
            EditTextSize.setText(oldProduct.getSize());
            EditTextVolume.setText(oldProduct.getVolume());
       }

        private SectionsPagerAdapter mSectionsPagerAdapter;
        private ViewPager mViewPager;
        public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

            @Override

            public Fragment getItem(int position) {

                switch (position) {
                    case 0:
                        Fragment tab1 = createCustomFragment1();
                        return tab1;
                    case 1:
                        Fragment tab2 = createCustomFragment2();
                        return tab2;
                    case 2:
                        Fragment tab3 = createCustomFragment3();
                        return tab3;
                    case 3:
                        Fragment tab4 = createCustomFragment4();
                        return tab4;
                    default:
                        return null;
                }
            }



            @Override
            public int getCount() {
                // Show 3 total pages.
                return 4;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                switch (position) {
                    case 0:
                        return "Sales";
                    case 1:
                        return "Oders";
                    case 2:
                        return "Suppliers";
                    case 3:
                        return "Actions";
                }
                return null;
            }

        }

        public Fragment createCustomFragment1(){
            final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

            final Sales salesFrg = new Sales();
            ft.replace(R.id.container, salesFrg);

            final Bundle bdl = new Bundle();
            bdl.putString("productIde", String.valueOf(productId));
            salesFrg.setArguments(bdl);

            ft.commit();
            return salesFrg;
        }

    > createCustomFragment2(),createCustomFragment3(),createCustomFragment4() >looks exactly like createCustomFragment1()

    }

0 个答案:

没有答案