从RecyclerView适配器启动片段

时间:2015-05-23 06:43:39

标签: java android android-fragments

我要从Fragment的适配器开始新的RecyclerView。这是代码。

@Override
public AdapterDrawer.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    if(viewType == ITEM) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_drawer, parent, false);
        viewHolder = new ViewHolder(view, viewType);

        return viewHolder; 

    } else if (viewType == HEADER) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false); 
        viewHolder = new ViewHolder(view, viewType);

        return viewHolder;    
    }

    return null;
}  

public ViewHolder(View view, int viewType) {             
    super(view);

    switch(viewType) {
    case ITEM:
        // ..... code

        break;
    case HEADER:
        //code.... findiewById ecc

        mycardView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Bundle bundle = new Bundle();
                bundle.putString("Code", "Value");
                Fragment fragment = new MyFragment();
                fragment.setArguments(bundle);
                ((ActionBarActivity)context).getFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
            }                   
        });

        break;

当我点击CardView时出现此错误

05-23 08:35:04.861: E/AndroidRuntime(24568): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-23 08:35:04.861: E/AndroidRuntime(24568):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3936)
05-23 08:35:04.861: E/AndroidRuntime(24568):    at android.view.ViewGroup.addView(ViewGroup.java:3786)
05-23 08:35:04.861: E/AndroidRuntime(24568):    at android.view.ViewGroup.addView(ViewGroup.java:3727)
05-23 08:35:04.861: E/AndroidRuntime(24568):    at android.view.ViewGroup.addView(ViewGroup.java:3700)
05-23 08:35:04.861: E/AndroidRuntime(24568):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:905)
05-23 08:35:04.861: E/AndroidRuntime(24568):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
05-23 08:35:04.861: E/AndroidRuntime(24568):    at android.app.BackStackRecord.run(BackStackRecord.java:834)
05-23 08:35:04.861: E/AndroidRuntime(24568):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
05-23 08:35:04.861: E/AndroidRuntime(24568):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
05-23 08:35:04.861: E/AndroidRuntime(24568):    at android.os.Handler.handleCallback(Handler.java:739)
05-23 08:35:04.861: E/AndroidRuntime(24568):    at android.os.Handler.dispatchMessage(Handler.java:95)
05-23 08:35:04.861: E/AndroidRuntime(24568):    at android.os.Looper.loop(Looper.java:135)
05-23 08:35:04.861: E/AndroidRuntime(24568):    at android.app.ActivityThread.main(ActivityThread.java:5254)
05-23 08:35:04.861: E/AndroidRuntime(24568):    at java.lang.reflect.Method.invoke(Native Method)
05-23 08:35:04.861: E/AndroidRuntime(24568):    at java.lang.reflect.Method.invoke(Method.java:372)
05-23 08:35:04.861: E/AndroidRuntime(24568):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
05-23 08:35:04.861: E/AndroidRuntime(24568):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

为什么呢?我怎么解决?我的RecyclerView位于Fragment内。

1 个答案:

答案 0 :(得分:0)

尝试替换以下行

view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_drawer, parent, false);
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false); 

view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_drawer, null);
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header,null);