RecyclerView不显示在片段内

时间:2015-02-24 08:07:21

标签: android android-fragments android-recyclerview

我最初尝试将RecyclerView添加到活动中并且工作正常。但是当我试图破坏我的recyclerview时出现问题。回收器视图不会出现在片段内。只是想知道我正在做的任何实施错误。

public class MainActivity extends ActionBarActivity  implements RecyclerViewFragment.OnFragmentInteractionListener {

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

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



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

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

    return super.onOptionsItemSelected(item);
}

@Override
public void onFragmentInteraction(Uri uri) {

}
}

这是我的片段RecyclerViewFragment

public class RecyclerViewFragment extends Fragment {

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";


private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

public static RecyclerViewFragment newInstance(String param1, String param2) {
    RecyclerViewFragment fragment = new RecyclerViewFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

public RecyclerViewFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_recycler_view, container, false);
    mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
    return v;

}


public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mRecyclerView.setHasFixedSize(true);

    // using a linear layout manager
    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);
    ArrayList<String> myDataset = new ArrayList<String>();
    myDataset.add("Hello");
    myDataset.add("bello");
    // specifying an adapter (see also next example)
    mAdapter = new MyAdapter(myDataset);
    mRecyclerView.setAdapter(mAdapter);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mListener = (OnFragmentInteractionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}



public interface OnFragmentInteractionListener {

    public void onFragmentInteraction(Uri uri);
}

}

这是MyAdapter,这是我的RecyclerView Adapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<String> mDataset;


public class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView txtHeader;
    public TextView txtFooter;

    public ViewHolder(View v) {
        super(v);
        txtHeader = (TextView) v.findViewById(R.id.firstLine);
        txtFooter = (TextView) v.findViewById(R.id.secondLine);
    }
}

public void add(int position, String item) {
    mDataset.add(position, item);
    notifyItemInserted(position);
}

public void remove(String item) {
    int position = mDataset.indexOf(item);
    mDataset.remove(position);
    notifyItemRemoved(position);
}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(ArrayList<String> myDataset) {
    mDataset = myDataset;
}

// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.each_item
            , parent, false);
    // set the view's size, margins, paddings and layout parameters
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    final String name = mDataset.get(position);
    holder.txtHeader.setText(mDataset.get(position));
    holder.txtHeader.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            remove(name);
        }
    });

    holder.txtFooter.setText("Footer: " + mDataset.get(position));

}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return mDataset.size();
}
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="com.example.nikhil.recyclerviewproject.Fragments.RecyclerViewFragment"
    android:id="@+id/fragment"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
</RelativeLayout>

fragment_recycler_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context="com.example.nikhil.recyclerviewproject.Fragments.RecyclerViewFragment"
android:id="@+id/fragment_layout">

<TextView
    android:id="@+id/jtv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/jtv"/>
</RelativeLayout>

each_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="6dip"
    android:contentDescription="TODO"
    android:src="@drawable/ic_launcher" />

<TextView
    android:id="@+id/secondLine"
    android:layout_width="fill_parent"
    android:layout_height="26dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@id/icon"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text="Description"
    android:textSize="12sp" />

<TextView
    android:id="@+id/firstLine"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/secondLine"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_alignWithParentIfMissing="true"
    android:layout_toRightOf="@id/icon"
    android:gravity="center_vertical"
    android:text="Example application"
    android:textSize="16sp" />

</RelativeLayout>

logcat的

02-24 14:30:57.520  18832-18832/com.example.nikhil.recyclerviewproject D/dalvikvm﹕ Late-enabling CheckJNI
02-24 14:30:58.180  18832-18832/com.example.nikhil.recyclerviewproject I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
02-24 14:30:58.180  18832-18832/com.example.nikhil.recyclerviewproject W/dalvikvm﹕ VFY: unable to resolve virtual method 12690: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
02-24 14:30:58.180  18832-18832/com.example.nikhil.recyclerviewproject D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-24 14:30:58.180  18832-18832/com.example.nikhil.recyclerviewproject I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
02-24 14:30:58.180  18832-18832/com.example.nikhil.recyclerviewproject W/dalvikvm﹕ VFY: unable to resolve virtual method 12696: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
02-24 14:30:58.180  18832-18832/com.example.nikhil.recyclerviewproject D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
02-24 14:30:58.180  18832-18832/com.example.nikhil.recyclerviewproject I/dalvikvm﹕ Could not find method android.view.ViewGroup.onWindowSystemUiVisibilityChanged, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onWindowSystemUiVisibilityChanged
02-24 14:30:58.180  18832-18832/com.example.nikhil.recyclerviewproject W/dalvikvm﹕ VFY: unable to resolve virtual method 12698: Landroid/view/ViewGroup;.onWindowSystemUiVisibilityChanged (I)V
02-24 14:30:58.180  18832-18832/com.example.nikhil.recyclerviewproject D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0008
02-24 14:30:58.200  18832-18832/com.example.nikhil.recyclerviewproject I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
02-24 14:30:58.200  18832-18832/com.example.nikhil.recyclerviewproject W/dalvikvm﹕ VFY: unable to resolve virtual method 9046: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
02-24 14:30:58.200  18832-18832/com.example.nikhil.recyclerviewproject D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
02-24 14:30:58.350  18832-18832/com.example.nikhil.recyclerviewproject I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
02-24 14:30:58.350  18832-18832/com.example.nikhil.recyclerviewproject W/dalvikvm﹕ VFY: unable to resolve virtual method 12693: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
02-24 14:30:58.350  18832-18832/com.example.nikhil.recyclerviewproject D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
02-24 14:30:58.370  18832-18832/com.example.nikhil.recyclerviewproject I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
02-24 14:30:58.370  18832-18832/com.example.nikhil.recyclerviewproject W/dalvikvm﹕ VFY: unable to resolve virtual method 364: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
02-24 14:30:58.370  18832-18832/com.example.nikhil.recyclerviewproject D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-24 14:30:58.370  18832-18832/com.example.nikhil.recyclerviewproject I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
02-24 14:30:58.370  18832-18832/com.example.nikhil.recyclerviewproject W/dalvikvm﹕ VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
02-24 14:30:58.370  18832-18832/com.example.nikhil.recyclerviewproject D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
02-24 14:30:59.670  18832-18832/com.example.nikhil.recyclerviewproject D/TextLayoutCache﹕ Using debug level: 0 - Debug Enabled: 0
02-24 14:30:59.810  18832-18832/com.example.nikhil.recyclerviewproject D/libEGL﹕ loaded /system/lib/egl/libGLES_android.so
02-24 14:30:59.830  18832-18832/com.example.nikhil.recyclerviewproject D/libEGL﹕ loaded /system/lib/egl/libEGL_adreno200.so
02-24 14:30:59.840  18832-18832/com.example.nikhil.recyclerviewproject D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_adreno200.so
02-24 14:30:59.850  18832-18832/com.example.nikhil.recyclerviewproject D/libEGL﹕ loaded /system/lib/egl/libGLESv2_adreno200.so
02-24 14:31:00.090  18832-18832/com.example.nikhil.recyclerviewproject I/Adreno200-EGLSUB﹕ <ConfigWindowMatch:2218>: Format RGBA_8888.
02-24 14:31:00.130  18832-18832/com.example.nikhil.recyclerviewproject D/memalloc﹕ /dev/pmem: Mapped buffer base:0x5172c000 size:13398016 offset:11862016 fd:57
02-24 14:31:00.140  18832-18832/com.example.nikhil.recyclerviewproject D/OpenGLRenderer﹕ Enabling debug mode 0
02-24 14:31:00.540  18832-18832/com.example.nikhil.recyclerviewproject D/memalloc﹕ /dev/pmem: Mapped buffer base:0x5264b000 size:2818048 offset:1282048 fd:60
02-24 14:31:00.560  18832-18832/com.example.nikhil.recyclerviewproject D/CLIPBOARD﹕ Hide Clipboard dialog at Starting input: finished by someone else... !

0 个答案:

没有答案