在recyclelerview页眉页脚中以编程方式添加framelayout时,没有找到id为0x1(未知)的视图

时间:2018-03-26 17:24:55

标签: android android-recyclerview recycler-adapter android-framelayout

我正在尝试创建一个动态UI,我在其中使用了具有页眉和页脚的recyclerview。 问题:我正在尝试以编程方式添加framelayout,添加了UI但是当我滚动recyclelerview并返回顶部(Position = 0)应用程序崩溃时发出以下错误

标题工作正常,但只要我滚动到recyclerview应用程序的底部就会崩溃(如果调试器打开则工作正常)

 java.lang.IllegalArgumentException: No view found for id 0x1 (unknown) for fragment PreviewFragment{df6a345 #4 id=0x1}
 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1413)
 at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1819)
 at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
 at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)
 at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)
 at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2239)
 at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
 at android.os.Handler.handleCallback(Handler.java:751)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:154)
 at android.app.ActivityThread.main(ActivityThread.java:6692)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

代码:

 public class LatestFragRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

     private static final int TYPE_HEADER = 0;
     private static final int TYPE_FOOTER = 1;
     private static final int TYPE_ITEM = 2;

     private ArrayList<String> stringArrayList;
     private Context mContext;
     private FragmentManager fragmentManager;
     private FrameLayout framelayout;

     public LatestFragRecyclerAdapter(Context mContext, ArrayList<String> strings, FragmentManager fragmentManager) {
         this.mContext = mContext;
         this.stringArrayList = strings;
         this.fragmentManager = fragmentManager;
     }

     @Override
     public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         if (viewType == TYPE_ITEM) {
             View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item, parent, false);
             return new ItemViewHolder(itemView);
         } else if (viewType == TYPE_HEADER) {
             View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_header, parent, false);
             createView();
             return new HeaderViewHolder(itemView);
         } else if (viewType == TYPE_FOOTER) {
             View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_footer, parent, false);
             createView();
             return new FooterViewHolder(itemView);
         } else return null;
     }

     private void createView(){
         int i = 1;
         framelayout = new FrameLayout(mContext);
         FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);

         framelayout.setId(i);

         // Apply the Layout Parameters for FrameLayout
         framelayout.setLayoutParams(lp);
     }
     @Override
     public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
         if (holder instanceof HeaderViewHolder) {
             HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
             headerHolder.headerLinearLayout.removeView(framelayout);
             createView();
             headerHolder.headerLinearLayout.addView(framelayout);
             setFragment(framelayout.getId(), fragmentManager);
             /*headerHolder.headerTitle.setText("Header View");
             headerHolder.headerTitle.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                     Toast.makeText(mContext, "You clicked at Header View!", Toast.LENGTH_SHORT).show();
                 }
             });*/
         } else if (holder instanceof FooterViewHolder) {
             FooterViewHolder footerHolder = (FooterViewHolder) holder;
             footerHolder.footerLinearLayout.removeView(framelayout);
             createView();
             footerHolder.footerLinearLayout.addView(framelayout);
             setFragmentSix(framelayout.getId(), fragmentManager);
             /*footerHolder.footerText.setText("Footer View");
             footerHolder.footerText.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                     Toast.makeText(mContext, "You clicked at Footer View", Toast.LENGTH_SHORT).show();
                 }
             });*/
         } else if (holder instanceof ItemViewHolder) {
             ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
             itemViewHolder.itemText.setText("Recycler Item " + position);
             itemViewHolder.itemText.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                     Toast.makeText(mContext, "You clicked at item " + position, Toast.LENGTH_SHORT).show();
                 }
             });
         }
     }

     @Override
     public int getItemViewType(int position) {
         if (position == 0) {
             return TYPE_HEADER;
         } else if (position == stringArrayList.size() + 1) {
             return TYPE_FOOTER;
         }
         return TYPE_ITEM;
     }

     @Override
     public int getItemCount() {
         return stringArrayList.size() + 2;
     }


     private void setFragmentSix(int resID, FragmentManager m) {


         BannerFragment frag = new BannerFragment();
         FragmentTransaction t = m.beginTransaction();
         t.add(resID, frag);
         t.commit();
         /*resID.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 Toast.makeText(mContext, "You clicked at Footer View", Toast.LENGTH_SHORT).show();
             }
         });*/
     }

     private void setFragment(int resID, FragmentManager m) {


         PreviewFragment frag = new PreviewFragment();
         FragmentTransaction t = m.beginTransaction();
         t.add(resID, frag);
         t.commit();
         /*resID.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 Toast.makeText(mContext, "You clicked at Footer View", Toast.LENGTH_SHORT).show();
             }
         });*/
     }

     private class HeaderViewHolder extends RecyclerView.ViewHolder {
         //        TextView headerTitle;
         LinearLayout headerLinearLayout;

         public HeaderViewHolder(View view) {
             super(view);
             headerLinearLayout = (LinearLayout) itemView.findViewById(R.id.header_linear);
     //            headerTitle = (TextView) view.findViewById(R.id.header_text);
         }
     }

     private class FooterViewHolder extends RecyclerView.ViewHolder {
         //        TextView footerText;
         LinearLayout footerLinearLayout;

         public FooterViewHolder(View view) {
             super(view);
             footerLinearLayout = (LinearLayout) view.findViewById(R.id.footer_linear);
     //            footerText = (TextView) view.findViewById(R.id.footer_text);
         }
     }

     private class ItemViewHolder extends RecyclerView.ViewHolder {
         TextView itemText;

         public ItemViewHolder(View itemView) {
             super(itemView);
             itemText = (TextView) itemView.findViewById(R.id.recycler_item_text);
         }
     }
 }

0 个答案:

没有答案