Home RecyclerView data is not showing but data is sended to adapter
Reply: 0

RecyclerView data is not showing but data is sended to adapter

Sec Cam
1#
Sec Cam Published in 2017-12-07 21:16:40Z

I am having Activity with ViewPager and TabLayout. When I click on first tab data inside RecyclerView is not showing. When I am in debug mode I see data is sended to adapter. I am try this solution without Tablayout and ViewPager only with RecyclerView and adapter inside Activity and this works great.

StudentActivity.java

public class StudentActivity extends AppCompatActivity{

@BindView(R.id.activity_student_view_viewPager)
ViewPager viewPager;

@BindView(R.id.activity_student_view_tabs)
TabLayout tabLayout;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_student_view);
    setTitle("View");
    ButterKnife.bind(this);

    setupViewPager();


}

private void setupViewPager(){
    StudentViewAdapter adapter = new StudentViewAdapter(getSupportFragmentManager());
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);
}



}

activity_student_view.xml

<android.support.constraint.ConstraintLayout 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">


    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <android.support.design.widget.TabLayout
            android:id="@+id/activity_student_view_tabs"
            android:layout_width="match_parent"
            app:tabMode="fixed"
            app:tabTextColor="@color/white"
            app:tabIndicatorColor="@color/white"
            app:tabSelectedTextColor="@color/white"
            app:tabGravity="fill"
            android:layout_height="wrap_content">
        </android.support.design.widget.TabLayout>

    </android.support.design.widget.AppBarLayout>


    <android.support.v4.view.ViewPager
        android:id="@+id/activity_student_view_viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>


</android.support.constraint.ConstraintLayout>

StudentViewAdapter.java

public class StudentViewAdapter extends FragmentStatePagerAdapter {

    public StudentViewPresenceAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment selectedFragment;
        switch (position) {
            case 0:
                selectedFragment = StudentLecturesFragment.newInstance();
                break;
            case 1:
                selectedFragment = StudentLecturesFragment.newInstance();
                break;
            default:
                selectedFragment = StudentLecturesFragment.newInstance();
        }

        return selectedFragment;
    }

    @Override
    public int getCount() {
        return 2;
    }


    @Override
    public CharSequence getPageTitle(int position) {
        String title = "";
        switch (position){
            case 0:
                title = "Lectures 1";
                break;
            case 1:
                title = "Lectures 2";
                break;
        }

        return title;
    }
}

StudentLecturesFragment.java

public class StudentLecturesFragment extends DefaultFragment {

@BindView(R.id.fragment_student_lectures_Need)
TextView need;

@BindView(R.id.fragment_student_lectures_Now)
TextView now;

View view = null;


private RecyclerView.Adapter adapter;


public static StudentLecturesFragment newInstance(){
    StudentLecturesFragment studentLectures = new StudentLecturesFragment();
    return studentLectures;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);

    view = inflater.inflate(R.layout.fragment_student_lectures, container, false);
    ButterKnife.bind(this, view);
    getLectures();
    return view;
}


private void getLectures(){

    SharedPreferences sharedPreferencess = getActivity().getSharedPreferences(Utils.PREFERENCE, Context.MODE_PRIVATE);
    String studentId = sharedPreferencess.getString(Utils.UID, "");

    Firebase userRef = new Firebase(Utils.FIRE_BASE_USER_REFERENCE+studentId+"/lectures");


    userRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            List<Lectures> lecturesList = new ArrayList<>();

            for (DataSnapshot lectur : dataSnapshot.getChildren()){

                    Lectures lct = new Lectures();
                    lct.setTitle(lectur.child("title").getValue(String.class));
                    lct.setDesc(lectur.child("desc").getValue(String.class));

                    lecturesList.add(lct);

            }

            adapter = new StudentLecturesAdapter(getActivity(), lecturesList);
            RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.fragment_student_lectures_RecyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            recyclerView.setAdapter(adapter);

            need.setText("Need");

        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });

}
}

fragment_student_lectures.xml

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


    <android.support.v7.widget.RecyclerView
        android:id="@+id/fragment_student_lectures_RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8">

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

    <TextView
        android:id="@+id/fragment_student_lectures_Need"
        android:text="txt"
        android:textSize="30dp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <TextView
        android:id="@+id/fragment_student_lectures_Now"
        android:text="txt 2"
        android:gravity="center"
        android:textSize="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

StudentLecturesAdapter.java

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


    List<Lectures> lecturesList;
    Context context;

    public StudentLecturesAdapter(Context context, List<Lectures> lecture) {
        this.context = context;
        this.lecturesList = lecture;
    }

    @Override
    public StudentLecturesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_student_lectures, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(StudentLecturesAdapter.ViewHolder holder, int position) {
        Lectures lect = lecturesList.get(position);
        holder.title.setText(lect.getTitle());
        holder.desc.setText(lect.getDesc());
    }

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


    public class ViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.row_student_lectures_title)
        TextView title;

        @BindView(R.id.row_student_lectures_desc)
        TextView desc;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}

row_student_lectures.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="16dp"
    android:paddingTop="16dp"
    android:paddingRight="16dp"
    android:paddingLeft="16dp"
    >


    <TextView
        android:id="@+id/row_student_lectures_title"
        android:textSize="25dp"
        android:text="10.10"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/row_student_lectures_desc"
        android:textSize="25dp"
        android:text="here"
        android:textStyle="bold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />



</LinearLayout>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.342516 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO