I am trying to implement a nested recycle view i read several post online, but i am still having a difficult time. This is what i did so far, i created a main layout which contains my main Recycle view, i then created the main recycle view row item. And populated it by creating an adapter and implementing it in my main activity. Secondly, i crated a layout with my child recycle view as well as the child recycle view row item. The problem arises when i am trying to access this recycle view, i am unsure as to how to go about doing this? I was able to access my parent recycle and implement the adapter in my main activity, but i am not sure as to how to access my child recycle view. Could someone assist me?
**Parent Recycle View**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
**Parent Recycle View row**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_home"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:elevation="5dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image_view_movie"
android:layout_width="130dp"
android:layout_height="170dp"
android:scaleType="fitXY"
/>
<LinearLayout
android:id="#+id/layout_mov"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/image_view_movie"
android:layout_margin="1dp"
android:orientation="vertical">
<TextView
android:id="#+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Mission Impossible"
android:textSize="12sp" />
<TextView
android:id="#+id/tv_genre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:fontFamily="sans-serif"
android:maxLines="1"
android:text="Action,Comedy..."
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
**Child RecycleView**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_movie_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="Movie Category"
android:textSize="14sp"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/home_recycler_view_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_movie_category"
android:layout_marginLeft="10dp"
android:layout_marginBottom="15dp"
android:orientation="horizontal" />
<View
android:id="#+id/activityMainDivider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/home_recycler_view_horizontal"
android:layout_marginBottom="5dp"
android:background="#31C7C7CC" />
</RelativeLayout>
**Child recycle view row**
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:id="#+id/constraintlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/newsThumbNail"
android:layout_width="0dp"
android:layout_height="200dp"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.448"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/newsTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Dangerous fire is out of control at 24 and park"
android:textSize="17sp"
app:layout_constraintEnd_toEndOf="#+id/newsThumbNail"
app:layout_constraintStart_toStartOf="#+id/newsThumbNail"
app:layout_constraintTop_toBottomOf="#+id/newsThumbNail" />
<TextView
android:id="#+id/newsBody"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Some text is here"
app:layout_constraintEnd_toEndOf="#+id/newsTitle"
app:layout_constraintStart_toStartOf="#+id/newsTitle"
app:layout_constraintTop_toBottomOf="#+id/newsTitle" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
**Parent Adapter**
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MessageViewHolder> {
private Context context;
private ArrayList<DataModel> userMessagesList;
public HomeAdapter(Context context, ArrayList<DataModel> userMessageList) {
this.context = context;
this.userMessagesList = userMessageList;
}
#NonNull
#Override
public MessageViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.inner_recycleview_row, viewGroup, false);
return new MessageViewHolder(view);
}
//Loads data into views
#Override
public void onBindViewHolder(#NonNull MessageViewHolder viewHolder, int i) {
// viewHolder.senderTextView.setText(message.getMessage());
viewHolder.senderTextView.setText(userMessagesList.get(i).getName());
viewHolder.receiverTextView.setText(userMessagesList.get(i).getAge());
}
#Override
public int getItemCount() {
return userMessagesList.size();
}
//links up ui elements
static class MessageViewHolder extends RecyclerView.ViewHolder {
private TextView senderTextView, receiverTextView;
public MessageViewHolder(#NonNull View itemView) {
super(itemView);
senderTextView = itemView.findViewById(R.id.tv_genre);
receiverTextView = itemView.findViewById(R.id.tv_title);
}
}
}
**Main Activity**
public class MainActivity extends AppCompatActivity {
private RecyclerView messagesRecycleView;
private ArrayList<DataModel> messageList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messageList = new ArrayList<>();
initRecycleView();
populateArray();
}
public void populateArray(){
DataModel dataModel1= new DataModel("Bob","18");
messageList.add(dataModel1);
}
//set up RecycleView
public void initRecycleView() {
messagesRecycleView = findViewById(R.id.rv_main);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setStackFromEnd(true);
//linearLayoutManager.setReverseLayout(true);
messagesRecycleView.setLayoutManager(linearLayoutManager);
HomeAdapter homeAdapter = new HomeAdapter(MainActivity.this, messageList);
messagesRecycleView.setAdapter(homeAdapter);
}
}
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.MessageViewHolder> {
private Context context;
private ArrayList<DataModel> userMessagesList;
public NewsAdapter(Context context, ArrayList<DataModel> userMessageList) {
this.context = context;
this.userMessagesList = userMessageList;
}
#NonNull
#Override
public MessageViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_feed_row, viewGroup, false);
return new MessageViewHolder(view);
}
//Loads data into views
#Override
public void onBindViewHolder(#NonNull MessageViewHolder viewHolder, int i) {
// viewHolder.senderTextView.setText(message.getMessage());
viewHolder.senderTextView.setText(userMessagesList.get(i).getName());
viewHolder.receiverTextView.setText(userMessagesList.get(i).getAge());
}
#Override
public int getItemCount() {
return userMessagesList.size();
}
//links up ui elements
static class MessageViewHolder extends RecyclerView.ViewHolder {
private TextView senderTextView, receiverTextView;
public MessageViewHolder(#NonNull View itemView) {
super(itemView);
senderTextView = itemView.findViewById(R.id.newsBody);
receiverTextView = itemView.findViewById(R.id.newsTitle);
}
}
}
Related
I've made an adapter with a layout like the one I share below. it went well but, the appearance of each item became untidy.
Here is my code :
public class TableRecyclerAdapter extends RecyclerView.Adapter<TableRecyclerAdapter.ViewHolder> {
private ArrayList<TableModel> data;
private Context context;
private Fragment fragment;
private Dialog dialog;
public TableRecyclerAdapter(Context context, ArrayList<TableModel> data, Fragment fragment, Dialog dialog) {
this.context = context;
this.data = data;
this.fragment = fragment;
this.dialog = dialog;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View v = inflater.inflate(R.layout.item_table, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.tvTable.setText(data.get(position).getName());
holder.tvState.setText(data.get(position).getStateString());
holder.bg.setBackgroundColor(Color.parseColor(data.get(position).getWarna()));
holder.tvHtml.setText(Html.fromHtml(data.get(position).getShortInfoHtml()));
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
public int getItemCount() {
return data.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
private ImageView paid;
private TextView tvTable, tvHtml, tvState;
private ConstraintLayout bg;
public ViewHolder(View itemView) {
super(itemView);
tvTable = itemView.findViewById(R.id.tvTable);
tvHtml = itemView.findViewById(R.id.tvHtml);
bg = itemView.findViewById(R.id.bg);
tvState = itemView.findViewById(R.id.tvState);
paid = itemView.findViewById(R.id.ivPaid);
}
}
}
This is for layout item
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:cardCornerRadius="10dp"
app:cardElevation="3dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/bg"
android:layout_width="match_parent"
android:background="#drawable/border_button_green"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginStart="12dp"
android:background="#drawable/border_transparent_table"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/tvState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="This text"
android:textColor="#color/color_primary"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="#+id/tvTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="25dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:text="7"
android:textColor="#color/color_primary"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvHtml"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:paddingBottom="12dp"
android:text="Example Text"
android:textColor="#color/color_primary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tvTable" />
<ImageView
android:id="#+id/ivPaid"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="4dp"
android:src="#drawable/paid"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
It makes my recyclerview have different heights depending on the text.
I want the items to be the same height per row (maximum height in the row)
Sorry for my english. I hope someone can help me. Thank you
I'm trying to create a simple app, where in the main activity there is a ViewPager2 with 3 pages linked to a TabLayout. In each page there is a RecyclerView populated by Cards. Just for testing purposes I tried to populate each page with the same 3 cards, but for some reason in the first page I get the 3 cards but on the next ones I get the 3 cards but there is a lot of blank space between each card so you can only see one card at a time, and I don't understand why that happens. And if I instead of 3 card use 4 or more, in the first page when I scroll down it looks fine but after I scroll back up it has the same problem as the other pages, a lot of blank space. Here is my code:
public class MainActivity extends AppCompatActivity {
String data[] = {"Destaque", "Perto de Si", "Brevemente"};
String evento[] = {"Jola", "Bowling", "Concerto"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager2 viewPager = findViewById(R.id.pager);
viewPager.setAdapter( new SwipeAdapter(this, evento));
TabLayout tabLayout = findViewById(R.id.tabLayout);
new TabLayoutMediator(
tabLayout,
viewPager,
new TabLayoutMediator.TabConfigurationStrategy() {
#Override
public void onConfigureTab(#NonNull TabLayout.Tab tab, int position) {
tab.setText(data[position]);
}
}
).attach();
}
//
public class SwipeAdapter extends FragmentStateAdapter {
private String evento[];
public SwipeAdapter(MainActivity mainActivity, String evento[]) {
super(mainActivity);
this.evento = evento;
}
#NonNull
#Override
public Fragment createFragment(int position) {
return new RecyclerFragment(evento);
}
#Override
public int getItemCount() {
return 3;
}
}
//
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
String eventos[];
RecyclerAdapter(String eventos[]){
this.eventos = eventos;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View cardView = inflater.inflate(R.layout.card_layout, parent, false);
// Return a new holder instance
return new ViewHolder(cardView);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
String evento = eventos[position];
TextView textView = holder.textView;
ImageView imageView = holder.imageView;
textView.setText(evento);
}
#Override
public int getItemCount() {
return eventos.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView textView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.card_image);
textView = itemView.findViewById(R.id.card_text);
}
}
}
}
//
public class RecyclerFragment extends Fragment {
String eventos[];
public RecyclerFragment(String eventos[]) {
this.eventos = eventos;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_recycler, container, false);
RecyclerView recyclerView = view.findViewById(R.id.recycler_fragment);
RecyclerAdapter adapter = new RecyclerAdapter(eventos);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
return view;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tabLayout"
app:layout_constraintVertical_bias="0.0" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="680dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Destaque" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Perto de Si" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Brevemente" />
</com.google.android.material.tabs.TabLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
//
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".TestFragment">
<com.google.android.material.card.MaterialCardView
android:id="#+id/card_fragment"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_margin="16dp"
app:cardElevation="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/card_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:src="#mipmap/beer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/card_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="#+id/card_image"
app:layout_constraintTop_toBottomOf="#+id/card_image" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</FrameLayout>
//
<?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=".RecyclerFragment">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_fragment"
/>
</FrameLayout>
Change in card_layout.xml
framelayout height android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="wrap_content">
<com.google.android.material.card.MaterialCardView
android:id="#+id/card_fragment"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_margin="16dp"
app:cardElevation="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/card_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:src="#mipmap/ic_launcher"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/card_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="TextView"
app:layout_constraintStart_toStartOf="#+id/card_image"
app:layout_constraintTop_toBottomOf="#+id/card_image" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</FrameLayout>
I am making an android cv app but I want to implement the UI shown in the screenshot.
screenshot of ui I want
below current UI from real device
current ui
The XML layout where I have implemented my UI, which consists of an ImageView and some TextViews which shows subjects. I have implemented all the tasks but UI is not showing how I want it to show.
<?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_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorBlust"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<ImageView
android:id="#+id/educationImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:src="#drawable/education_information"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/education_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:text="#string/education_information"
android:textColor="#color/colorWhite"
android:textSize="20sp" />
</LinearLayout>
<TextView
android:id="#+id/duration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:text="#string/text_duration"
android:textColor="#color/colorWhite"
android:textSize="16sp" />
<TextView
android:id="#+id/institution"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:text="#string/text_institution"
android:textColor="#color/colorWhite"
android:textSize="16sp" />
<TextView
android:id="#+id/degree"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:text="#string/text_degree"
android:textColor="#color/colorWhite"
android:textSize="16sp" />
<Space
android:layout_width="50dp"
android:layout_height="50dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/subjectImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:src="#drawable/university_subjects"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/subjects"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="100dp"
android:layout_marginLeft="100dp"
android:text="#string/university_subjects"
android:textColor="#color/colorWhite"
android:textSize="20sp" />
<include
layout="#layout/subject_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/subjects"
android:layout_marginTop="60dp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
I have created another adapter and created dummy data in subjectivity
below adapter class
public class SubjectAdapter extends RecyclerView.Adapter<SubjectAdapter.ViewHolder> {
private SubjectActivity subjectActivity;
private int [] subjectImage;
String[] subjectText;
List<FakeData> fakeData;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView subjects;
public ImageView subjectImage;
public ViewHolder(View view) {
super(view);
subjectImage = (ImageView) view.findViewById(R.id.subjectImage);
subjects = (TextView) view.findViewById(R.id.subjects);
}
}
public SubjectAdapter(SubjectActivity subjectActivity, int []subjectImage, String [] subjectText, List<FakeData> fakeData){
this.subjectActivity = subjectActivity;
this.subjectImage = subjectImage;
this.subjectText = subjectText;
this.fakeData = fakeData;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.subject_list, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
FakeData fake = fakeData.get(position);
Picasso.get().load(fake.getImage()).
into(holder.subjectImage);
holder.subjects.setText(fake.getSubjects());
}
// TODO Auto-generated constructor stub
#Override
public int getItemCount() {
return fakeData.size() ;
}
}
below subject XML where I have hosted RecyclerView
<RelativeLayout 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.support.v7.widget.RecyclerView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.RecyclerView>
below subject_list.xml where I have host items
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBlust"
android:orientation="horizontal" >
<ImageView
android:id="#+id/icon"
android:layout_width="60dp"
android:layout_marginLeft="10dp"
android:layout_height="60dp"
android:padding="5dp"
android:src="#drawable/computer_science"
android:layout_marginStart="10dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/computers_science"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#color/colorWhite" />
</LinearLayout>
I have created fake data in order to host other images and texts
below fakeModel class
public class FakeData {
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getSubjects() {
return subjects;
}
public void setSubjects(String subjects) {
this.subjects = subjects;
}
String image;
String subjects;
}
below adapter class where I have extended with RecyclerView
public class SubjectAdapter extends RecyclerView.Adapter {
private SubjectActivity subjectActivity;
private int [] subjectImage;
String[] subjectText;
List<FakeData> fakeData;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView subjects;
public ImageView subjectImage;
public ViewHolder(View view) {
super(view);
subjectImage = (ImageView) view.findViewById(R.id.subjectImage);
subjects = (TextView) view.findViewById(R.id.subjects);
}
}
public SubjectAdapter(SubjectActivity subjectActivity, int []subjectImage, String [] subjectText, List<FakeData> fakeData){
this.subjectActivity = subjectActivity;
this.subjectImage = subjectImage;
this.subjectText = subjectText;
this.fakeData = fakeData;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.subject_list, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
FakeData fake = fakeData.get(position);
Picasso.get().load(fake.getImage()).
into(holder.subjectImage);
holder.subjects.setText(fake.getSubjects());
}
// TODO Auto-generated constructor stub
#Override
public int getItemCount() {
return fakeData.size() ;
}
}
below My Subject class where I have implemented fake images and data
public class SubjectActivity extends Activity {
List<FakeData> fakeData;
int [] subjectImage = {R.drawable.computer_science,
R.drawable.data_structure,
};
ListView list;
String[] subjectText = {
"Computer Science",
"Data Structure",
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subject);
RecyclerView recyclerView= (RecyclerView) findViewById(R.id.list);
SubjectAdapter adapter = new SubjectAdapter(SubjectActivity.this, subjectImage,
subjectText, fakeData);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(adapter);
}
}
This looks like you only have used Android Studio's drag and drop feature to position the UI elements. The problem is, Android Studio shows those UI elements in a generic device, which wont match all devices. When I was starting off with Android, this document helped me a lot to understand how elements in the UI must be placed.
https://developer.android.com/studio/write/layout-editor
You XML is malformed. Your LinearLayout orientation is wrong:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
It should be vertical.
Then validate its content. You have multiple RelativeLayout with orientation attribute.
It should be LinearLayout instead. RelativeLayouts don’t have orientation.
Besides those errors, your layout is very complex and have a deep hierarchy. This will lead to performance issues. My suggestion to you is to learn how to use ConstraintLayout.
The learning curve is a bit high, but it will be worth it!
I have a recyclerview and i want to change the style of each item of the recyclerview with databinding dynamically.
This is my Recyclerview item layout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/channel_priority_item_height"
android:background="#drawable/channel_item_bg"
android:paddingStart="#dimen/channel_priority_header_left_pad"
android:paddingEnd="#dimen/channel_priority_item_right_pad">
<ImageView
android:id="#+id/preference_channel_image"
android:src="#drawable/empty_channel_drawable"
android:layout_width="#dimen/channels_item_width"
android:layout_height="#dimen/channels_item_height"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:layout_marginEnd="#dimen/channel_item_margin_end"/>
<RelativeLayout
android:id="#+id/preference_channel_switch_holder"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true">
<ImageView
android:id="#+id/preference_channel_switch"
android:layout_width="#dimen/channels_preference_switch_size"
android:layout_height="#dimen/channels_preference_switch_size"
android:layout_centerInParent="true"
android:padding="#dimen/switch_padding"
android:src="#drawable/switch_selected_bg"
android:background="#drawable/circle_button_bg" />
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_toEndOf="#+id/preference_channel_image"
android:layout_toStartOf="#+id/preference_channel_switch_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true">
<TextView
android:text="Some Header"
android:id="#+id/channel_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/preference_channel_primary"
android:textColor="#color/white"
android:fontFamily="sans-serif"/>
<TextView
android:text="Some Description"
android:id="#+id/channel_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/preference_channel_secondary"
android:textColor="#color/white_50"
android:fontFamily="sans-serif"/>
</LinearLayout>
</RelativeLayout>
I want to change the style of each item in this layout,like the text color of the textview background of the layout etc.
Where should the databinding be done? In adapter or in the parent fragment?Any example will be very helpful.
Thank you.
Model Class:
public class ColorObject extends BaseObservable {
int color = Color.BLACK;
public void setColor(int color) {
if (this.color==color) return;
this.color = color;
notifyPropertyChanged(BR.color);
}
#Bindable
public int getColor() {
return color;
}
}
However, I won't recommend to create a model class only for color. You can add this Color object in your Activity View Model class.
Adapter:
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
private ColorObject colorObject;
public ListAdapter(Context context){
colorObject = new ColorObject();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ItemListBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_list, parent, false);
return new ViewHolder(binding);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bind(colorObject);
}
#Override
public int getItemCount() {
return 5;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ItemListBinding itemListBinding;
public ViewHolder(ItemListBinding itemBinding) {
super(itemBinding.getRoot());
itemListBinding = itemBinding;
}
public void bind(ColorObject item) {
itemListBinding.setTextColor(item);
itemListBinding.executePendingBindings();
}
}
}
item_list.xml:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="textColor"
type="com.example.ColorObject"/>
</data>
<android.support.constraint.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:padding="#dimen/activity_horizontal_margin"
android:id="#+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#{textColor.color}"
android:background="#color/colorPrimaryDark"
android:text="sjhfjsgjfhgjfhs"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</layout>
I am using Recyclerview and my onCreateViewHolder and onBindViewHolder are not called. I am getting the data but it is not displaying.
My adapter class
public class DishesAdapter extends RecyclerView.Adapter<DishesAdapter.MyViewHolder> {
private Context mContext;
List<List> DialogList = new ArrayList<List>();
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView dishnames;
public RatingBar dishratings;
public ImageView dishimages;
private ImageLoader mLoader;
public MyViewHolder(View view) {
super(view);
Log.d("follower2","hi");
dishnames = (TextView)view.findViewById(R.id.dishname);
dishimages = (ImageView)view.findViewById(R.id.dishimage);
dishratings=(RatingBar)view.findViewById(R.id.dishrating);
}
}
public DishesAdapter(Context mContext, List objects) {
super();
Log.d("follower1","hi");
this.mContext = mContext;
this.DialogList = objects;
Log.d("follower1",objects.toString());
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mContext)
.inflate(R.layout.recipe_list, parent, false);
Log.d("follower","hi");
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
final int i=position;
Log.d("follower","hi");
List dialog = DialogList.get(i);
Log.d("follower",dialog.toString());
String dishid = dialog.get(0).toString();
final String dishname = dialog.get(1).toString();
//byte[] dishimage = Base64.decode(dialog.get(2).toString(), Base64.DEFAULT);
String dishimage=dialog.get(2).toString();
String rating=dialog.get(3).toString();
holder.dishnames.setText(dishname);
holder.dishratings.setRating(Float.parseFloat(rating));
holder.mLoader.DisplayImage(dishimage.replaceAll(" ", "%20"),holder.dishimages);
}
#Override
public int getItemCount() {
return DialogList.size();
}}
Recyclerview
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(mLayoutManager);
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
tools:context="mealplanner.com.main.mealplanner.MainActivity"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbars="vertical" />
</LinearLayout>
Row List
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
card_view:cardUseCompatPadding="true"
card_view:cardElevation="4dp"
card_view:cardCornerRadius="3dp"
android:id="#+id/cv">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/dishimage"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginRight="6dip"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_margin="10dp"
android:src="#drawable/portraitlanding"
/>
<TextView
android:id="#+id/dishname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_toRightOf="#+id/dishimage"
android:textColor="#000000"
android:textSize="20dp"
android:layout_gravity="center"
android:text="Andrew" />
<RatingBar
android:id="#+id/dishrating"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:isIndicator="true"
android:numStars="5"
android:stepSize="0.1"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:layout_below="#+id/dishname"
android:layout_toRightOf="#+id/dishimage"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
DishesAdapter constructor is being called. I am getting follower1 log along with the list of items.