片段未显示回收视图

时间:2019-07-28 20:11:10

标签: android-fragments android-recyclerview

我有一个片段,我想在其中显示水平回收视图,但它显示没有适配器错误。我不确定出了什么问题。我有一个名为NewEvent的片段类,我正在引用回收视图和设置回收视图的适配器。

片段

public class NewEvent extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_new_event, container, false);
        RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.JoinedGroupsRecycle);
        List<ModelJoinedGroups> joinedGroupsList= new ArrayList<ModelJoinedGroups>();
        joinedGroupsList.add(new ModelJoinedGroups("Waterford Tech Meetup","Link to waterford"));
        joinedGroupsList.add(new ModelJoinedGroups("Dublin Hikers","link to dublin"));
        LinearLayoutManager horizontalLayoutManager
                = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(horizontalLayoutManager);
        GroupAdapter adapter = new GroupAdapter(getActivity(),joinedGroupsList);
        recyclerView.setAdapter(adapter);











        return inflater.inflate(R.layout.fragment_new_event, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        TextView addGroups = (TextView) getView().findViewById(R.id.NewGroup);
        addGroups.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), AddGroupActivity.class);
                startActivity(intent);
            }
        });
    }
}

AdapterClass

package com.example.meru;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

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

    private List<ModelJoinedGroups> mData;
    private LayoutInflater mInflater;
    private ItemClickListener mClickListener;

    // data is passed into the constructor
    GroupAdapter(Context context, List<ModelJoinedGroups> data) {

        this.mInflater = LayoutInflater.from(context);
        this.mData = data;
    }

    // inflates the row layout from xml when needed
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.joinedgroupsrow, parent, false);
        Log.i("ADAP",view.toString());
        return new ViewHolder(view);
    }

    // binds the data to the TextView in each row
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {


        String GroupName = mData.get(position).GroupName;
        holder.myTextView.setText(GroupName);
    }

    // total number of rows
    @Override
    public int getItemCount() {
        return mData.size();
    }


    // stores and recycles views as they are scrolled off screen
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView myTextView;

        ViewHolder(View itemView) {
            super(itemView);
            myTextView = itemView.findViewById(R.id.GroupName);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
        }
    }

    // convenience method for getting data at click position
    String getItem(int id) {
        return mData.get(id).GroupName;
    }

    // allows clicks events to be caught
    void setClickListener(ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    // parent activity will implement this method to respond to click events
    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }
}

FragmentNewevent.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
    tools:context=".NewEvent">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="20dp"
        android:textFontWeight="1000"
        android:text="Your groups"
        android:textSize="22dp"
        android:textColor="#000"
        />

    <TextView
        android:id="@+id/NewGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:textColor="@color/text"
        android:layout_marginLeft="260dp"
        android:layout_marginTop="40dp"
        android:clickable="true"
        android:text="+ New group"
        />

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/JoinedGroupsRecycle"
            android:layout_width="103dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="100dp" />
    </RelativeLayout>
</FrameLayout>

joinedgrouprow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp">

   <androidx.cardview.widget.CardView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">

       <ImageView
           android:layout_width="100dp"
           android:layout_height="130dp"
           android:src="@color/colorAccent"/>


   </androidx.cardview.widget.CardView>
    <TextView
        android:id="@+id/GroupName"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:textSize="20sp"/>


</LinearLayout>

0 个答案:

没有答案