碎片没有重新加载

时间:2012-12-07 11:51:54

标签: android dynamic android-fragments reload

我正在尝试实现片段。正在创建Fragment,但问题是其中的视图为空并显示空白屏幕。当我第一次运行它时会显示视图,但在重新加载时它的视图变为空,并显示一个空白屏幕。不知道是什么问题。

这是我的代码......

   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                View root = inflater
                                .inflate(R.layout.winedescription, container, false);
                tvTitle = (TextView) root.findViewById(R.id.tvWineDescriptionTitle);
                tvDescription = (TextView) root
                                .findViewById(R.id.tvWineDescriptionDescription);
                tvWineName = (TextView) root
                                .findViewById(R.id.tvWineDescriptionWineName);
                tvWineType = (TextView) root.findViewById(R.id.tvWineDescriptionType);
                tvWinePrice = (TextView) root.findViewById(R.id.tvWineDescriptionPrice);
                btnSelect = (Button) root.findViewById(R.id.btnWineDescriptionSelect);
                imgWine = (ImageView) root.findViewById(R.id.ivWineImage);
                setDescription(position); 
                btnSelect.setOnClickListener(this);
                return root;
        }

My Parent Activity它是一个适配器。

public class WineFragmentPagerAdapter extends FragmentPagerAdapter {

        public static ArrayList<WineSubCategoryNameListModel> itemData = new ArrayList<WineSubCategoryNameListModel>();
        private int size = 0;

        public WineFragmentPagerAdapter(FragmentManager fm,
                        ArrayList<WineSubCategoryNameListModel> wineSubCategory) {
                super(fm);
                try {
                        itemData.clear();
                        if (wineSubCategory != null) {
                                for (int i = 0; i < wineSubCategory.size(); i++) {
                                        itemData.add(wineSubCategory.get(i));
                                }
                                size = itemData.size();
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

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

        @Override
        public Fragment getItem(int position) {
                Log.d("wine", "Fragment Created");
                FragmentWineView f = FragmentWineView.newInstance();
                // f.setWineList(itemData);
                f.setPosition(position);
                return f;
        }
}




if (llWinePager.getChildCount() == 0) {
                    LayoutInflater infSelectTypeLayout = (LayoutInflater)     getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    wineDescriptionView = (LinearLayout) infSelectTypeLayout.inflate(
                                    R.layout.winedescriptionpager, null);
                    viewPage = (ViewPager) wineDescriptionView
                                    .findViewById(R.id.viewPager);
                    btnBackWineDescriptionView = (Button) wineDescriptionView
                                    .findViewById(R.id.btnBackWineDescriptionView);

                    WineFragmentPagerAdapter adapter = new WineFragmentPagerAdapter(
                                    getSupportFragmentManager(), wineSubCategory.get(0)
                                                    .getWineSubCategoryNameList());

                    viewPage.setAdapter(adapter);
                    viewPage.setOnPageChangeListener(WineMenu.this);
                    displayImage(subCategoryId);
                    llWinePager.addView(wineDescriptionView);
                    btnBackWineDescriptionView.setOnClickListener(this);
                    Log.d("wine", ".WineMenu .....IF....");
            } else {
                    WineFragmentPagerAdapter adapter = new WineFragmentPagerAdapter(
                                    getSupportFragmentManager(), wineSubCategory.get(0)
                                                    .getWineSubCategoryNameList());
                    viewPage.setAdapter(adapter);
                    displayImage(subCategoryId);
                    Log.d("wine", ".WineMenu .....Else....");
            }

1 个答案:

答案 0 :(得分:0)

首先你为扩展FragmentActivity定义父类然后在里面定义anyClass扩展Fragment然后这段代码实现并检查,

public class DemoFragment extends Fragment {

        public ConatactFragment() {
            // defult constructor
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.YOUR_LAYOUT,container,false);
            return view;
        }

    }