更改支持库后,Android Studio for Fragment替换中可能存在错误

时间:2015-07-27 07:36:58

标签: android android-fragments android-studio android-support-library fragmentmanager

我正在尝试替换位于主布局中的片段。这些是我的项目中使用的配置。

默认构建版本,

  compileSdkVersion 22
  buildToolsVersion '22.0.1'

依赖关系,

compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:recyclerview-v7:21.0.3'

这是我用来替换片段类中的片段的代码,

FragmentManager fm = ((FragmentActivity) activity).getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            FragmentCategoryContainer fragmentCategoryContainer = new FragmentCategoryContainer();
            fragmentCategoryContainer.category_code = m.getCatagoryCode();
            ft.replace(R.id.fragment_container, fragmentCategoryContainer);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.addToBackStack(null);
            ft.commit();

这是包含片段的布局

   <fragment
        android:id="@+id/fragment_container"
        android:name="com.example.FragmentCategoryList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

这是预期的输出,

enter image description here

现在,最令人惊讶的是,如果我将依赖项更改为

compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:recyclerview-v7:22.2.1

我正在使用重叠片段获得以下输出

enter image description here

我不知道什么是错的。这确实是一个危急情况。任何形式的帮助将非常感激。

更新:

这是我的Adapter类,它包含循环视图,

StickyGridAdapter extends RecyclerView.Adapter<StickyViewHolder> {

    private static final String[] header_title = {"New Apps", "Wap Infotainment", "Wap Download"};
    private static final int VIEW_TYPE_HEADER = 0x01;
    private static final int VIEW_TYPE_CONTENT = 0x00;

    private final ArrayList<LineItem> mItems;
    private final Context mContext;
    private int mHeaderDisplay;
    private boolean mMarginsFixed;
    private List<ServicesList> Items;


    public StickyGridAdapter(Context context, int headerMode, List<ServicesList> items) {
        mContext = context;
        mHeaderDisplay = headerMode;
        mItems = new ArrayList<>();
        Items = items;

        //Insert headers into list of items.
        int sectionManager = -1;
        int headerCount = 0;
        int sectionFirstPosition = 0;

        for (int i = 0; i < Items.size(); i++) {
            ServicesList m = Items.get(i);


            // Insert new header view and update section data.
            if (i == 0) {
                sectionManager = (sectionManager + 1) % 2;
                sectionFirstPosition = i + headerCount;
                headerCount += 1;
                mItems.add(new LineItem(header_title[0], "", true, sectionManager, sectionFirstPosition));

            } else if (i == 4) {
                sectionManager = (sectionManager + 1) % 2;
                sectionFirstPosition = i + headerCount;
                headerCount += 1;
                mItems.add(new LineItem(header_title[2], "", true, sectionManager, sectionFirstPosition));
            }
            mItems.add(new LineItem(m.getServiceName(), m.getIcon(), false, sectionManager, sectionFirstPosition));
        }
    }

    public boolean isItemHeader(int position) {
        return mItems.get(position).isHeader;
    }

    public String itemToString(int position) {
        return mItems.get(position).text;
    }

    @Override
    public StickyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == VIEW_TYPE_HEADER) {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.header_item, parent, false);
        } else {
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.sticky_grid_item, parent, false);
        }
        return new StickyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(StickyViewHolder holder, int position) {
        final LineItem item = mItems.get(position);
        final View itemView = holder.itemView;

        holder.bindItem(item.text);
        if (!item.isHeader) {
            ServicesList m = Items.get(position);
            holder.bindImage(mContext, m);
        }

        final GridSLM.LayoutParams lp = GridSLM.LayoutParams.from(itemView.getLayoutParams());
        lp.setSlm(GridSLM.ID);
        lp.setColumnWidth(mContext.getResources().getDimensionPixelSize(R.dimen.grid_column_width));
        lp.setFirstPosition(item.sectionFirstPosition);
        itemView.setLayoutParams(lp);
    }

    @Override
    public int getItemViewType(int position) {
        return mItems.get(position).isHeader ? VIEW_TYPE_HEADER : VIEW_TYPE_CONTENT;
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    public void setHeaderDisplay(int headerDisplay) {
        mHeaderDisplay = headerDisplay;
        notifyHeaderChanges();
    }

    public void setMarginsFixed(boolean marginsFixed) {
        mMarginsFixed = marginsFixed;
        notifyHeaderChanges();
    }

    private void notifyHeaderChanges() {
        for (int i = 0; i < mItems.size(); i++) {
            LineItem item = mItems.get(i);
            if (item.isHeader) {
                notifyItemChanged(i);
            }
        }
    }

    private static class LineItem {

        public int sectionManager;
        public int sectionFirstPosition;
        public boolean isHeader;
        public String text, path;

        public LineItem(String text, String path, boolean isHeader, int sectionManager,
                        int sectionFirstPosition) {
            this.isHeader = isHeader;
            this.text = text;
            this.path = path;
            this.sectionManager = sectionManager;
            this.sectionFirstPosition = sectionFirstPosition;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为这是Not Found The requested resource / was not found on this server

中的方式
onCreateViewHolder