RecyclerView Not Showing up on running the app

时间:2016-04-04 18:55:22

标签: java android android-fragments android-recyclerview

So I am trying to create an app for listing some events. My code compiles but the RecyclerView doesn't show up on running the app.

Any hints/help is appreciated

Here are the Java files:-

EventListFragment.java

package com.execube.volantis.views;

import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.execube.volantis.R;
import com.execube.volantis.model.Events;

import java.util.ArrayList;

import xyz.hanks.library.SmallBang;
import xyz.hanks.library.SmallBangListener;

/** 22
 * Created by Prateek Phoenix on 4/4/2016.
 */
public class EventListFragment extends Fragment {

    private static String day1[]={"Vocal Light Music Group", "Paper Presentation", "Kannada Dumbcharades",
            "Karnataka Quiz", "Robotics - Balance All The Way and Grid Solver", "Online Treasure Hunt",
            "Chitrakala",  "Slow Drag",
            "Biztech Quiz", "Indian Classical Vocal Solo (Prelims)", "Indian Light Music Solo (Prelims)",
            "Rock 'N Rolla Project", "Videcoder", "Shut(ter) Up", "Kannada Antyakshari",
            "Minute For Your Talent", "Kannada Padabanda", "Hindi Antyakshari", "Parody",
            "Litent Quiz","Choreo Stage", "The Walk Off"};

    private static String day2[]={"Indian Classical Vocal Solo (Finals)", "Amazing Race", "Sports Quiz",
            "WCA Events", "Auto Quiz", "Cartooned",
            "Assembly", "Just A Minute (JAM)", "Dumb Charades", "Air Crash Prelims",
            "Indian Classical Dance Solo","Instrumental Solo", "Halla Bol", "Bomb Diffusion", "Charchasparde",
            "Debate", "Air Crash", "Link It Up", "General Quiz", "Smoked '14 - Street Dance",
            "Concert", "Prize"};

    private static String timings[]={"09:00","09:00","09:00","09:00",
                                     "10:00","10:00","10:00","10:00",
                                    "10:00","10:00","10:00","11:00",
                                    "11:00","11:00","11:00","13:00",
                                    "13:00","14:00","14:00","14:00",
                                    "16:30","19:30"};

    private String day1venue[]={"Main Stage", "Edusat Hall", "CS/IS Block", "EC Block", "EEE Lab",
                                "Mech Lab", "Admin Block", "Street", "Placement Cell", "Classrooms",
                               "Classrooms", "Main Stage", "Edusat Hall", "Street", "CS/IS Block",
            "EC Block", "EEE Lab","Mech Lab", "Admin Block", "Street", "Placement Cell", "Classrooms"};


    private ArrayList<Events> mEvents;
    private RecyclerView mRecyclerView;
    private EventAdapter mEventAdapter;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        mEvents=new ArrayList<>();
        for (int i=0;i<day1.length;i++)
        {
            Events event= new Events();
            event.setEvent(day1[i]);
            event.setVenue(day1venue[i]);
            event.setTime(timings[i]);
            event.setDetails("This is Pantheon 2016\n Play it safe and have a good time. Live long and prosper");
            event.setFavourite(false);

        }


        super.onCreate(savedInstanceState);

    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_event_list,container,false);
        mRecyclerView=(RecyclerView)view.findViewById(R.id.event_list_recyclerview);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        updateUI();
        return view;
    }

    private void updateUI() {

        mEventAdapter=new EventAdapter(mEvents);
        mRecyclerView.setAdapter(mEventAdapter);
    }

    private class EventHolder extends RecyclerView.ViewHolder{

        private TextView mEventTileTextView;
        private TextView mEventDetailsTextView;
        private TextView mVenueTextView;
        private TextView mTimeTextView;
        private SmallBang mSmallBang;
        private ImageView mFavouriteView;
        private Events mEvent;

        public EventHolder(View itemView){

            super(itemView);

            mEventTileTextView=(TextView)itemView.findViewById(R.id.event_title_textview);
            mEventDetailsTextView=(TextView)itemView.findViewById(R.id.event_details_text_view);
            mVenueTextView=(TextView)itemView.findViewById(R.id.venue_textview);
            mTimeTextView=(TextView)itemView.findViewById(R.id.time_textview);

            mFavouriteView=(ImageView)itemView.findViewById(R.id.favourite_view);

            Typeface robotoMonoMedium=Typeface.createFromAsset(itemView.getContext().getAssets(),"fonts/RobotoMono-Medium.ttf");
            Typeface robotoMonoRegular =Typeface.createFromAsset(itemView.getContext().getAssets(),"fonts/RobotoMono-Regular.ttf");

            mSmallBang=SmallBang.attach2Window(getActivity());
            mEventDetailsTextView.setTypeface(robotoMonoMedium);
            mVenueTextView.setTypeface(robotoMonoRegular);
            mTimeTextView.setTypeface(robotoMonoMedium);


        }

        public void bindData(final Events event)
        {
            mEvent=event;
            mEventTileTextView.setText(mEvent.getEvent());
            mVenueTextView.setText(mEvent.getVenue());
            mTimeTextView.setText(mEvent.getTime());
            mEventDetailsTextView.setText(mEvent.getDetails());
            mSmallBang.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mEvent.isFavourite()) {
                        unlike(v);
                    } else {
                        like(v);
                    }
                }
            });

        }

        private void unlike(View v) {

            mEvent.setFavourite(false);
            mSmallBang.bang(v);
            mFavouriteView.setImageResource(R.drawable.ic_favorite_black_18dp);
            mSmallBang.setmListener(new SmallBangListener() {
                @Override
                public void onAnimationStart() {

                }

                @Override
                public void onAnimationEnd() {

                }
            });

        }

        private void like(View v) {
             mEvent.setFavourite(true);
            mSmallBang.bang(v);
            mFavouriteView.setImageResource(R.drawable.ic_favorite_red_18dp);
            mSmallBang.setmListener(new SmallBangListener() {
                @Override
                public void onAnimationStart() {

                }

                @Override
                public void onAnimationEnd() {

                }
            });

        }
    }


    private class EventAdapter extends RecyclerView.Adapter<EventHolder>{

       private ArrayList<Events> mEvents;

        public EventAdapter(ArrayList<Events> events) {
            mEvents = events;
        }

        @Override
        public EventHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater=LayoutInflater.from(getActivity());
            View view=inflater.inflate(R.layout.events_item,parent,false);

            return new EventHolder(view);
        }

        @Override
        public void onBindViewHolder(EventHolder holder, int position) {

            Events events=mEvents.get(position);
            holder.bindData(events);

        }

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

}

EventActivity.java

package com.execube.volantis.views;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;

import com.execube.volantis.R;

/**
 * Created by Prateek Phoenix on 4/4/2016.
 */
public class EventActivity extends FragmentActivity {

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


        FragmentManager fm =getSupportFragmentManager();
        Fragment fragment=fm.findFragmentById(R.id.event_list_container);

        if(fragment==null)
        {
            fragment=new EventListFragment();
            fm.beginTransaction()
                    .add(R.id.event_list_container, fragment)
                    .commit();

        }
    }
}

Following are the layout files used:-

fragment_event_list.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/event_list_recyclerview">

</android.support.v7.widget.RecyclerView>

event_activity.xml

  <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:id="@+id/event_list_container"
                 android:background="#5FCF80"/>


**events_item.xml**



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#5FCF80"
                android:paddingBottom="10dp">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="20dp"
    android:id="@+id/view">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/event_title_textview"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:padding="10dp"
            android:textColor="@android:color/black"
            android:textColorHint="@android:color/black"
            android:hint="Event"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/event_details_text_view"
            android:textSize="15sp"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:padding="10dp"
            android:textColor="#777777"
            android:textColorHint="#777777"
            android:hint="Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, "
            android:layout_below="@+id/event_title_textview"
            />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_below="@+id/event_details_text_view"
            android:layout_marginBottom="20dp"
            android:paddingRight="7dp"
            android:layout_marginTop="10dp"
            android:weightSum="1">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/activity_horizontal_margin"
                android:padding="5dp"
                android:id="@+id/calendar_imageview"
                android:src="@drawable/ic_place_black_18dp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/venue_textview"
                android:padding="5dp"
                android:textColor="#777777"
                android:textColorHint="#777777"
                android:hint="April 2nd"
                android:text="Main Stage"/>



            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/activity_horizontal_margin"
                android:padding="5dp"
                android:id="@+id/clock_imageview"
                android:src="@drawable/ic_access_time_black_18dp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp"
                android:id="@+id/time_textview"
                android:padding="5dp"
                android:textColor="#777777"
                android:textColorHint="#777777"
                android:hint="5:30pm"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:id="@+id/phonedial_imageview"
                android:src="@drawable/ic_phone_black_18dp"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/activity_horizontal_margin"
                android:padding="5dp"
                android:id="@+id/favourite_view"
                android:src="@drawable/ic_favorite_black_18dp" />
        </LinearLayout>
    </RelativeLayout>

</android.support.v7.widget.CardView>
    </RelativeLayout>

2 个答案:

答案 0 :(得分:1)

您应该在添加layoutManager之前设置适配器

 updateUI(); 
 mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

答案 1 :(得分:1)

您的mEvents可能是空的,可能要添加 mEvents.add(事件);

循环中的

 for (int i=0;i<day1.length;i++)
    {
        Events event= new Events();
        event.setEvent(day1[i]);
        event.setVenue(day1venue[i]);
        event.setTime(timings[i]);
        event.setDetails("This is Pantheon 2016\n Play it safe and have a good time. Live long and prosper");
        event.setFavourite(false);

    }

:)