Expandable recycler view and a textview below issue - android

I'm trying to implement a 2 expandable recyclerview with 2 headings.So my view has
1.
2.
3.
4.
Since my recyclerview is expandable, and when I expand the 1st recyclerview , it goes inside my 2nd textview.
ie.there is a separate scroll for my recycler view.
I want the 2nd textview and recyclerview to move down when expanding the 1st recyclerview.
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
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:orientation="vertical"
android:layout_marginBottom="10dp"
tools:context=".ListActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="22sp"
android:text="Weightage: 40%"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/recview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fafafa" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="22sp"
android:layout_marginBottom="10dp"
android:text="Weightage: 60%"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fafafa" />
</LinearLayout>
</ScrollView>
the same happens with the second recyclerview. On expanding the second recycler view, recyclerview items have a separate scroll and the text above stays at a place.The overall scroll doesn't work.

You can achieve this using only one Recyclerview. Make one model class named "Model" of your data, in which add one variable like "isHeader". When you prepare your data list to show in Recyclerview make 2 model object with
'isHeader = true'
Also, make two (item_layout.xml & header_layout.xml) xml files, one for header & another for your original data item.
And in Recyclerview adapter class you have two separate ViewHolder classes for each of the above layouts. refer this for more details and example
public class MultiViewTypeAdapter extends RecyclerView.Adapter {
public static final int HEADER_VIEW = 0;
public static final int LIST_VIEW = 1;
private Resources resources;
private DownloadListener downloadListener;
public MyToursAdapter(List<Model> objects) {
super(objects);
resources = App.getApp().getResources();
}
#NonNull
#Override
public RecycleView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if (viewType == HEADER_VIEW) {
return new SectionHeaderItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.header_layout, parent, false));
} else
return new ListItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false));
}
#Override
public void onBindViewHolder(#NonNull BaseRecycleAdapter.ViewHolder holder, int position) {
if (getItemViewType(position) == HEADER_VIEW)
((HeaderItemViewHolder) holder).bindHeaderViewHolder(objects.get(position));
else
((ListItemViewHolder) holder).bindListViewHolder(objects.get(position));
}
#Override
public int getItemViewType(int position) {
if (objects.get(position).isHeader())
return HEADER_VIEW;
else
return LIST_VIEW;
}
public class ListItemViewHolder extends BaseRecycleAdapter.ViewHolder {
//write code to show data from list object.
}
public class HeaderItemViewHolder extends BaseRecycleAdapter.ViewHolder {
//write code to show data from list object.
}
}
enter code here

Related

How do I not recycle only the first 2 views of my recyclerview or any items?

I am using a recyclerview for displaying and broadcasting videos of users. However, when I scroll through the recycler view, I see my first view, which has my video gets recycled hence why it's not visible to other users. Is there a way I can make sure that the first view is not recycled so I dont have to worry about my video view getting resetrecycled every single time I scroll through my list?
Here's my code :
In my fragment...
...
<android.support.v7.widget.RecyclerView
android:id="#+id/videoList"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="#+id/myButtonContainer"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/myoldContainer">
...
and the corresponding adapter...
public class GroupAdapter extends RecyclerView.Adapter<GroupAdapter.myViewHolder> {
private CopyOnWriteArrayList<Person> persons;
private Context mContext;
public GroupAdapter(#NonNull final CopyOnWriteArrayList<Person> persons , Context context) {
this.persons = persons;
this.mContext= context;
for (Person person : this.persons) {
//get names
}
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_person, parent, false);
final MyViewHolder viewHolder = new MyViewHolder(layout);
return viewHolder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final Person person = person.get(position);
final Participant participant = person.getParticipant();
if (person.getName() != null) {
holder.personName.setText(person.getName());
}
if (person.getImage() != null) {
holder.personImage.setImageBitmap(person.getImage());
} else {
holder.personImage.setImageResource(R.drawable.default_profile);
}
holder.personImage.setVisibility(View.INVISIBLE);
holder.personImage.setVisibility(View.VISIBLE);
final VideoView videoView;
if (participant.isMe) {
videoView = participant.videoStreamer.videoView;
} else {
videoView = participant.videoPlayer.videoView;
}
if (holder.personVideo.getChildCount() != 0) {
holder.personVideo.removeAllViews();
}
if (videoView.getParent() != null) {
ViewGroup parent = (ViewGroup) videoView.getParent();
parent.removeView(videoView);
}
holder.personVideo.addView(videoView, myViewHolder.videoLayoutParams);
if (person.isVideoPaused()) {
holder.personVideo.setVisibility(View.INVISIBLE);
holder.personImage.setVisibility(View.VISIBLE);
} else {
holder.personVideo.setVisibility(View.VISIBLE);
holder.personImage.setVisibility(View.INVISIBLE);
}
}
#Override
public int getItemCount() {
return persons.size();
}
public static final class MyViewHolder extends RecyclerView.ViewHolder {
#BindView(R.id.personVideo)
public ViewGroup personVideo;
#BindView(R.id.personImage)
public ImageView personImage;
#BindView(R.id.personName)
public TextView personName;
protected static FrameLayout.LayoutParams videoLayoutParams = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
);
public MyViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
Here's how I am setting it in my fragment:
LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
videoAdapter = new VideoAdapter(myHelper.getPeople(), getContext());
videoList.setLayoutManager(manager);
videoList.setAdapter(videoAdapter);
item_person:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="0dp"
android:background="#drawable/person_border"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:id="#+id/personContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
<FrameLayout
android:id="#+id/personVideo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black" />
<ImageView
android:id="#+id/personImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
android:src="#drawable/default_profile" />
<TextView
android:id="#+id/personName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#AA555555"
android:gravity="center_horizontal|bottom"
android:textColor="#color/green"
android:textSize="12sp"
android:lines="1"
android:ellipsize="end"
tools:text="androiduser#gmail.com"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
fragment with recycle view: xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout>
....
</RelativeLayout>
<include containers>...</include>
...
<android.support.v7.widget.RecyclerView
android:id="#+id/personList"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="invisible"
>
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
I don't think that "not recycling" the video view will actually stop it from being destroyed when you scroll. It's not the recycling is the problem but the view unbinding.
I think such complex component such as VideoView should not be inside the RecyclerView at all.. You can try adding it as a static content on top, which most likely will solve the issue. You can use a NestedScrollView for that. Take a look here: Recyclerview inside scrollview- How to scroll whole content?
If you still think you want to keep it in the RecyclerView and disable the recycling, do the following.
Create a separate view type for your video view items. Here is an example:
https://stackoverflow.com/a/26573338/3086818. Treat your video view item as a header view from the example.
Once you do this, there is a RecycledViewPool class which manages the recycling of items inside a RecyclerView. You can tell it which views to recycle and which not. By default it recycles all views. To disable recycling for your video view items use your new view type like this:
recyclerView.getRecycledViewPool().setMaxRecycledViews(TYPE_VIDEO_VIEW, 0);
where TYPE_VIDEO_VIEW is the new type that you created using the previous example. The number 0 tells the RecyclerView how many items to recycle - in this case it's 0, meaning "do not recycle". More info about this here: https://stackoverflow.com/a/36313437/3086818.
I hope this helps.. Good luck!
The answer is yes, you can actually do that.
Since you said "The first item" so you simply add a check.
if(position == 0)
{
holder.setIsRecyclable(false); //This line prevents the row from recycling
}

RecyclerView - Layout_weight is ignored when it loads first time

I have an Activity that contains a Fragment. Fragment contains a RecyclerView, displaying CardViews.
CardView is very simple, just a TextView and a CustomView. This is the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="100dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/card_current_heat_cardview"
android:layout_gravity="center_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:weightSum="4">
<TextView
android:id="#+id/card_current_heat_textview"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textStyle="bold"
android:textColor="#000000"
android:background="#android:color/holo_red_dark"
android:gravity="center_vertical"/>
<com.amige.vm2.CurrentHeatChartView
android:id="#+id/card_current_heat_chart_view"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
TextView should use 1/4 of the width, and the CustomView 3/4.
Im having trouble understanding why weights are ignored the first time the RecyclerView loads
Image of RecyclerView loaded for the first time
But if I scroll up and down, the layout works as expected for some Cards
Image of RecyclerView scrolled up and down
I also have another Activity (no fragment involved here) that has a RecyclerView and IS using this same CardView Layout and it works perfectly!!
I dont know if it has to do with the Fragment...
What am I missing?
Thanks!
EDIT
This the Adapter code
public class MainAdapter extends RecyclerView.Adapter<MyFragment.MainAdapter.ViewHolder>
{
public MainAdapter()
{
}
#Override
public MyFragment.MainAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_current_heat, parent, false);
return new MyFragment.MainAdapter.ViewHolder(v);
}
#Override
public void onBindViewHolder(MyFragment.MainAdapter.ViewHolder holder, int position)
{
if (arrayList.size() == 2)
{
if (position == 0)
{
holder.variableNameTextView.setText(“Var Name”);
holder.currentHeatChartView.reloadChartWithSamples(arrayList.get(0));
}
else
{
if ((position - 1) < arrayList.get(1).size())
{
HashMap variableHashMap = (HashMap) arrayList.get(1).get(position - 1);
holder.variableNameTextView.setText(variableHashMap.get("VarName") != null ? (String)variableHashMap.get("VarName") : "");
holder.currentHeatChartView.reloadChartWithVariableValuesAndColors(variableHashMap, colorGradientsArrayList.get((position - 1) % colorGradientsArrayList.size()));
}
}
}
}
#Override
public int getItemCount()
{
if (arrayList.size() == 2)
{
if (arrayList.get(1).size() > 0)
{
return 1 + arrayList.get(1).size();
}
else
{
return 1;
}
}
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder
{
TextView variableNameTextView;
CurrentHeatChartView currentHeatChartView;
public ViewHolder(View v)
{
super(v);
this.variableNameTextView = (TextView) v.findViewById(R.id.card_current_heat_textview);
this.currentHeatChartView = (CurrentHeatChartView) v.findViewById(R.id.card_current_heat_chart_view);
}
}
}
I found the culprit.
There was a ConstraintLayout on top of the hierarchy of the Activity containing the Fragment. Since I was working with nested layouts, ConstraintLayout was not the right way to go.
I changed it to RelativeLayout and the cards are now rendered properly.
I think your layout inflation is wrong.
View view = View.inflate(mContext, R.layout.your_layout, null);
If you do like that change your code like this.
View view = LayoutInflater.from(mContext).(R.layout.your_layout, parent, false);
Hope it helps:)

Horizontal Scrolling GridView

I am trying to create a Horizontal Scrollable GridView of my own (don't want to use anything that is out there because I want to play with the concept and understand it better on my own) but for some reason no grid item appears on screen.
The GridLayout is part of a ListItem and I instantiate it there. (if i use a normal GridLayout, that is not added in a Scroll View the data will show)
Here is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="true">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/horizontalScrollView">
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:columnWidth="100dp"
/>
</HorizontalScrollView>
</FrameLayout>
And Here is my adapter Class:
public class RelatedCarsAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Cars> cars;
public RelatedCarsAdapter(Context context, ArrayList< Cars > cars) {
this.mContext = context;
this.cars = cars;
}
#Override
public int getCount() {
return cars.size();
}
#Override
public Object getItem(int i) {
return cars.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
TextView dummyTextView = new TextView(mContext);
dummyTextView.setText(String.valueOf(position));
return dummyTextView;
}
}
And this is how I instantiate it :
GridView gridView = (GridView) rowView.findViewById(R.id.gridview);
RelatedCarsAdapter relatedCarsAdapter = new RelatedCarsAdapter(mContext, cars);
gridView.setNumColumns(cars.size());
gridView.setAdapter(relatedCarsAdapter);
GridView won't scroll inside HorizontalScrollView. You need to use RecyclerView instead of GridView in your layout (and probably extend RecyclerView.Adapter)
See documentation: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

RecyclerView scrolling issue (Child items far away from each other)

Goodd day.I have simple recycler view with simplest dummy datas for test purpose,thus i have an weird issue to which the google did not find any solution or even an issue at all.On first launch the view is all good but as soon as i start to scrool,the child items are being as far from each other as no one can image...Really very and very far.But the issue is that the actual child items layout parameters are correct,only issue is that i dont know why RecyclerView decides to have each item heaps far away from each other.Please can you give me an help?Posting full code of my RecyclerView.
The view for recyclerView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.ink.activities.HomeActivity"
tools:showIn="#layout/app_bar_home">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>
The Adapter.
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.ViewHolder> {
private List<FeedModel> feedList;
private Context mContext;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView title, content;
public ViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.feedTitle);
content = (TextView) view.findViewById(R.id.feedContent);
}
}
public FeedAdapter(List<FeedModel> feedList, Context context) {
mContext = context;
this.feedList = feedList;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.feed_single_view, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
FeedModel feedModel = feedList.get(position);
holder.title.setText(feedModel.getTitle());
holder.content.setText(feedModel.getContent());
// animate(holder);
}
public void animate(RecyclerView.ViewHolder viewHolder) {
final Animation animAnticipateOvershoot = AnimationUtils.loadAnimation(mContext, R.anim.bounce_interpolator);
viewHolder.itemView.setAnimation(animAnticipateOvershoot);
}
#Override
public int getItemCount() {
return feedList.size();
}
}
I guess you won`t need holder as no view initiated with it.
The single child item view of RecyclerView adapter.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
app:cardCornerRadius="5dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/feedTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:fontFamily="#string/appFont"
android:text="loading...."
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/feedContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/feedTitle"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="10dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
The initiation of actual parameters.
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter = new FeedAdapter(mFeedModelArrayList, this);
RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator();
itemAnimator.setAddDuration(500);
itemAnimator.setRemoveDuration(500);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setItemAnimator(itemAnimator);
This code is as simple as it can get and it is important to mention that i am initiation all this inside the default NAVIGATION DRAWER ACTIVITY of android studio (the default templae inside content_main layout).So plaese can you give me any hint about the issue?
You're using
android:layout_width="match_parent"
android:layout_height="match_parent"
on your child item views. As of Support Library 23.2:
The RecyclerView widget provides an advanced and flexible base for creating lists and grids as well as supporting animations. This release brings an exciting new feature to the LayoutManager API: auto-measurement! This allows a RecyclerView to size itself based on the size of its contents. This means that previously unavailable scenarios, such as using WRAP_CONTENT for a dimension of the RecyclerView, are now possible. You’ll find all built in LayoutManagers now support auto-measurement.
Due to this change, make sure to double check the layout parameters of your item views: previously ignored layout parameters (such as MATCH_PARENT in the scroll direction) will now be fully respected.
Change your layout_height to wrap_content if you only want your items to be as large as needed. match_parent means they will be as large as the screen.

How To Make a RecyclerView Show Two Different Fragments

I'm developing an android app with a group of colleage friends , we are learing android development as we go so if this question is really easy , we are all sorry .
In one of the sections of our apps , we need to show a Card (that contains data about a user ) and then , beneath de Card , a list (variable in size ) of items all of the same type . Previously , when we needed to do a list we used a recycler view with a custom adapter , and that worked perfectly with a list consisting of just one type of item , but now that we need the recyclerView to host both the Card and the list of items we don't know how to do it .
For now , we have made the Card static and only apply the rcycler view to the list of items beneath the Card , and that means that only the list is scrollable . We need to make the whole screen scrollable .
So , how can we achieve this? My gut tells me that we would need to turn that card at the top into a fragment and then make some changes to the our custom adapter so that it can handle both tipes of fragments (the card and the item of the list that goes beneath the card) . Is recyclerView the right answer to our problems?
Hope you can help us , here is our code so far :
TeacherProfileFragment
public class TeacherProfileFragment extends Fragment implements FragmentsMethods,ItemAdapterListener<Course>{
private TextView teacherName;
private ImageView teacherImage;
private RecyclerView recyclerviewTeacherRatings;
private RecyclerView recyclerViewTeacherCourses;
private RatingListAdapter ratingListAdapter;
private CourseListAdapter courseListAdapter;
private ImageView teacherCardBackground;
public TeacherProfileFragment(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(Constants.FRAGMENT_PROFILE_TEACHER_LAYOUT, container, false);
setUpElements(view);
addListeners();
return view;
}
#Override
public void setUpElements(View view) {
teacherCardBackground= (ImageView) view.findViewById(R.id.teacher_card_background);
Picasso.with(getActivity()).load("https://newevolutiondesigns.com/images/freebies/google-material-design-wallpaper-17.jpg").fit().centerCrop().into(teacherCardBackground);
//Bitmap bitmap = ((BitmapDrawable)teacherCardBackground.getDrawable()).getBitmap();
//Palette p = Palette.from(bitmap).generate();
//teacherCardBackground.setBackgroundColor(p.getVibrantColor(0x0000000));
teacherName=(TextView)view.findViewById(R.id.profile_teacher_mame);
teacherName.setText(Storage.getSingelton().getInfo(this,Storage.KEY_TEACHER_NAME));
teacherImage=(ImageView)view.findViewById(R.id.profile_teacher_image);
Picasso.with(getActivity()).load(Storage.getSingelton().getInfo(this,Storage.KEY_TEACHER_IMAGE)).transform(new CircleTransform()).into(teacherImage);
recyclerViewTeacherCourses=(RecyclerView)view.findViewById(R.id.course_list);
//recyclerviewTeacherRatings=(RecyclerView)view.findViewById(R.id.rating_list);
courseListAdapter=new CourseListAdapter(getActivity(),getData(""));
courseListAdapter.setListener(this);
ratingListAdapter=new RatingListAdapter(getActivity(),getStaticData());
//recyclerviewTeacherRatings.setAdapter(ratingListAdapter);
//recyclerviewTeacherRatings.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerViewTeacherCourses.setAdapter(courseListAdapter);
recyclerViewTeacherCourses.setLayoutManager(new LinearLayoutManager(getActivity()));
}
#Override
public void addListeners() {
}
#Override
public void itemClicked(View view, Course object) {
Storage.getSingelton().storage(object,this);
Redirect.getSingelton().showFragment(this,Constants.TEACHER_CONTAINER,Constants.FRAGMENT_TEACHER_COURSE);
}
public List<Rating> getStaticData(){
List<Rating> ratings=new ArrayList<>();
ratings.add(new Rating((float) 4.0,"tecnica"));
ratings.add(new Rating((float) 2.5,"salud"));
ratings.add(new Rating((float) 1.3,"conmosion"));
ratings.add(new Rating((float) 3.0,"desarrollo"));
return ratings;
}
#Override
public List<Course> getData(String data) {
List<Course> courses=new ArrayList<>();
courses.add(new Course("Matematica ", (float) 4.0,"FISI"));
courses.add(new Course("Fisica", (float) 4.0,"FISI"));
courses.add(new Course("Matematica", (float) 4.0,"FISI"));
courses.add(new Course("Matematica", (float) 4.0,"FISI"));
return courses;
}
}
CourseListAdapter
public class CourseListAdapter extends RecyclerView.Adapter<CourseListAdapter.CourseHolder>{
private List<Course>courses= Collections.emptyList();
private ItemAdapterListener itemAdapterListener;
private LayoutInflater layoutInflater;
public CourseListAdapter(Context context,List<Course> courses){
layoutInflater=LayoutInflater.from(context);
this.courses=courses;
}
#Override
public CourseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=layoutInflater.inflate(Constants.COURSE_NEW_ITEM,parent,false);
CourseHolder courseHolder=new CourseHolder(view);
return courseHolder;
}
#Override
public void onBindViewHolder(CourseHolder holder, int position) {
Course course=courses.get(position);
holder.setElements(course);
}
#Override
public int getItemCount() {
return courses.size();
}
public void setListener(ItemAdapterListener listener){
this.itemAdapterListener =listener;
}
public class CourseHolder extends RecyclerView.ViewHolder implements ViewHolderSetters<Course>,View.OnClickListener{
private TextView courseName;
private TextView courseFaculty;
private RatingBar courseRating;
private TextView courseMark;
private View vista;
private Course current;
private ImageView initialLetterImage;
public CourseHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
courseName= (TextView) itemView.findViewById(R.id.profile_course_course_name);
courseFaculty=(TextView) itemView.findViewById(R.id.course_faculty);
initialLetterImage=(ImageView) itemView.findViewById(R.id.letterImageBackground);
//courseRating=(RatingBar) itemView.findViewById(R.id.course_rating);
//courseMark=(TextView) itemView.findViewById(R.id.course_mark);
vista=itemView;
}
#Override
public void onClick(View v) {
itemAdapterListener.itemClicked(v,current);
}
#Override
public void setElements(Course elements) {
current=elements;
courseName.setText(elements.getName());
courseFaculty.setText(elements.getFaculty());
ColorGenerator generator = ColorGenerator.MATERIAL;
int color = generator.getColor(courseName.getText().charAt(0));
TextDrawable.IBuilder builder = TextDrawable.builder().round();
TextDrawable textDrawable = builder.build(courseName.getText().toString().charAt(0)+"", color);
initialLetterImage.setImageDrawable(textDrawable);
//courseMark.setText(elements.getRating()+"");
//courseRating.setRating(elements.getRating());
}
}
}
FrangmentTeacherProfile
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_teacher_profile">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_teacher_profile"
android:layout_width="match_parent"
android:layout_height="280dp"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="5dp"
card_view:cardPreventCornerOverlap="true"
card_view:cardUseCompatPadding="true">
<ImageView
android:id="#+id/teacher_card_background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginBottom="5dp"
android:id="#+id/profile_teacher_image"
android:layout_centerHorizontal="true"
/>
<!--style="#style/ProfileTeacherItemImage" -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/profile_teacher_mame"
android:layout_below="#id/profile_teacher_image"
android:textColor="#android:color/white"
android:textSize="25dp"
android:text="Frank Escobedo Bailon"
android:gravity="center"
android:maxLines="2"/>
<!--style="#style/ProfileTeacherItemName"-->
<RelativeLayout
android:id="#+id/profile_rating_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/profile_teacher_mame">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/profile_teacher_rating"
android:textColor="#android:color/white"
android:textSize="20dp"
android:text="4.5"
android:gravity="center"
android:maxLines="1"
android:layout_centerHorizontal="true"/>
<ImageView
android:id="#+id/teacher_profile_star_drawable"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_toRightOf="#id/profile_teacher_rating"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:src="#mipmap/ic_grade_white_24dp"/>
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/profile_courses_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/card_view_teacher_profile"
android:layout_margin="5dp"
android:textSize="15dp"
android:text="Cursos Que Dicta"/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#color/eventDetailDividerColor"
android:layout_toRightOf="#id/profile_courses_label"
android:layout_below="#id/card_view_teacher_profile"
android:layout_marginTop="15dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/course_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/profile_courses_label"
style="#style/ProfileTeacherItemCourse">
</android.support.v7.widget.RecyclerView>
Before the recycler view (e.g. list view), there was an easy way to achieve this:
Just add the card view as header to the list.
With a recycler view you can't simply call addHeaderView(), but you can declare two view types inside your adapter for the recycler view.
With this link you should be able to do this :
Is there an addHeaderView equivalent for RecyclerView?
This way, you just need two layout files (one for the card view and one for the list items) and then you can inflate both layouts inside your adapter.
If you have further questions just ask!

Categories

Resources