简体   繁体   中英

Android RecyclerView Adapter in a fragment not working

I am trying to use a androidx.recyclerview.widget.RecyclerView inside a Fragment and having a tough time trying to add basic items to it via an adapter. Following is my Fragment:

public class TimelineFragment extends Fragment {

    private DataBoundAdapter timelineAdapter;
    private View rootView;

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        // Inflate the layout for this fragment

        rootView = inflater.inflate(R.layout.fragment_timeline_home, container, false);

        initStuff(container);
        return rootView;
    }

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

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private void initStuff(ViewGroup container) {
        ArrayList<FernPost> list = {...};
        timelineAdapter = new DataBoundAdapter(list);
        RecyclerView recyclerView = rootView.findViewById(R.id.scroll_timeline);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
        recyclerView.setAdapter(timelineAdapter);
        timelineAdapter.notifyDataSetChanged();
    }
}

This is the fragment xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp"
        android:text="hi there bullshit!!!"/>
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scroll_timeline"
        android:scrollbars="vertical"
        android:visibility="visible">
    </androidx.recyclerview.widget.RecyclerView>
</LinearLayout>

The Main activity layout xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_input_add" />

    <fragment
        android:id="@+id/timeline_fragment"
        android:name="com.blinkfast.fern.TimelineFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

here is the adapter code for the recycler view:

public class DataBoundAdapter extends RecyclerView.Adapter<DataBoundViewHolder> {

    private List<FernPost> fernPosts;

    public DataBoundAdapter(List<FernPost> fernPosts) {
        this.fernPosts = fernPosts;
    }

    @NonNull
    @Override
    public DataBoundViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemLayoutView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fernpost, null);
        return new DataBoundViewHolder(itemLayoutView);
    }

    @Override
    public void onBindViewHolder(@NonNull DataBoundViewHolder holder, int position) {
        holder.bind(fernPosts.get(position));
    }

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

And the view holder:

public class DataBoundViewHolder extends RecyclerView.ViewHolder {

    private FernPost post;
    private View fernPostView;

    public DataBoundViewHolder(@NonNull View itemView) {
        super(itemView);
        this.fernPostView = itemView;
        Log.i(DataBoundViewHolder.class.getName(), "");
    }

    public void bind(FernPost fernPost){
        Log.i(DataBoundViewHolder.class.getName(), "binding");
        this.post = fernPost;
    }
}

I have spent entire day trying various thing making it work, but nothing has so far. The onCreateViewHolder / onBindViewHolder methods are just not getting called. Not sure what i am doing wrong.

Assuming your fragment (the one holding RecyclerView) is displayed and your list has items before passed to DataBoundAdapter , please use different inflation inside your adapter's onCreateViewHolder :

LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM