I have a problem with item height and gravity in a horizontal recyclerview as you can see in the picture: RecyclerView item height and gravity not working
How to set the item height to same height as recyclerciew and the gravity at the bottom of recyclerview ?
Thank you for your help.
My code for Activity_Drill_Graph.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Drill_Graph_Activity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/toolbar_drill_graph"
layout="#layout/toolbar_drill_graph" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_5dp"
android:background="#color/grey"
android:gravity="bottom"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:background="#color/lightgrey"
android:orientation="vertical"
android:padding="#dimen/padding_5dp">
<TextView
android:id="#+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_10"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_9"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_8"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_7"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_6"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_5"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_4"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_3"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_2"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_1"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textView0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/_0"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="14sp" />
<TextView
android:id="#+id/textViewDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/date"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="18sp" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewDrillGraph"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_margin="#dimen/margin_5dp"
android:background="#color/white"
android:orientation="horizontal"
android:padding="#dimen/padding_5dp"
android:scrollbars="horizontal" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
item code: drill_graph_single_column.xml :
<?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="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:background="#color/lightgrey"
android:gravity="bottom"
android:orientation="vertical"
android:padding="#dimen/padding_1dp"
android:weightSum="110">
<Button
android:id="#+id/btnSkill"
style="#style/btnStyleScoreGrey"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:text="#string/skill"
android:textAllCaps="false"
android:textSize="16sp" />
<Button
android:id="#+id/btnDate"
style="#style/btnStyleDrillGraphGrey"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="#dimen/margin_1dp"
android:layout_weight="10"
android:text="#string/date"
android:textAllCaps="false"
android:textSize="12sp" />
</LinearLayout>
My code for RecyclerView Adapter:
package org.poolshot.poolshotacademy;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import java.util.List;
public class My_DrillGraph_RecyclerView_Adapter extends RecyclerView.Adapter<My_DrillGraph_RecyclerView_Adapter.MyHolder> {
SharedPreferences defaultPreferencesSettings;
private Context myRCAContext;
private List<String> myDrillGraphDates;
private List<Integer> myDrillGraphSkill;
// constructor
public My_DrillGraph_RecyclerView_Adapter(Context applicationContext, List<String> mDrillGraphDates, List<Integer> mDrillGraphSkill) {
this.myRCAContext = applicationContext;
this.myDrillGraphDates = mDrillGraphDates;
this.myDrillGraphSkill = mDrillGraphSkill;
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int position) {
View layout = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.drill_graph_single_column,null);
MyHolder myHolder = new MyHolder(layout);
return myHolder;
}
#Override
public void onBindViewHolder(#NonNull final MyHolder viewHolder, final int position) {
String skill = myDrillGraphSkill.get(position).toString();
// set color for buttons
viewHolder.btnDate.setBackground(myRCAContext.getResources().getDrawable(R.drawable.custom_btn_grey));
viewHolder.btnSkill.setTextColor(myRCAContext.getResources().getColor(R.color.white));
// set text for buttons
viewHolder.btnDate.setText(myDrillGraphDates.get(position));
viewHolder.btnSkill.setText(skill);
}
#Override
public int getItemCount() {
return myDrillGraphDates.size();
}
public static class MyHolder extends RecyclerView.ViewHolder {
Button btnDate;
Button btnSkill;
public MyHolder(View itemView) {
super(itemView);
btnDate = itemView.findViewById(R.id.btnDate);
btnSkill = itemView.findViewById(R.id.btnSkill);
}
}
}
This is my solution :
I changed the code of the class MyHolder as :
public static class MyHolder extends RecyclerView.ViewHolder {
Button btnDate;
Button btnSkill;
public MyHolder(View itemView) {
super(itemView);
btnDate = itemView.findViewById(R.id.btnDate);
btnSkill = itemView.findViewById(R.id.btnSkill);
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.BOTTOM;
itemView.setLayoutParams(layoutParams);
}
}
Related
hi I am trying to display data in a recyclerview in android by parsing a json from mysql database. But it keeps displaying just some of the data and I can't seem to find the problem.Any help will be greatly appreciated.
This is the adapter:
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
List<Course> data;
private Context context;
public Adapter(Context context,List<Course> data){
this.data=data;
this.context=context;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.course_list,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
DecimalFormat form = new DecimalFormat("0.00");
Course current=data.get(position);
holder.tCoursecode.setText(current.getmCoursecode());
holder.tCoursetitle.setText(current.getmCoursetitle());
holder.tCredit.setText(String.valueOf(current.getmCredit()));
holder.tMarks.setText(form.format (current.getmMarks()));
holder.tGrade.setText(current.getmGrade());
}
#Override
public int getItemCount() {
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tCoursecode;
TextView tCoursetitle;
TextView tCredit;
TextView tMarks;
TextView tGrade;
public MyViewHolder(View itemView) {
super(itemView);
tCoursecode=itemView.findViewById(R.id.tcc);
tCoursetitle=itemView.findViewById(R.id.tct);
tCredit=itemView.findViewById(R.id.tc);
tMarks=itemView.findViewById(R.id.tm);
tGrade=itemView.findViewById(R.id.tg);
}
}
this the layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:padding="16dp">
<LinearLayout
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/right">
<TextView
android:id="#+id/tcc"
android:layout_width="match_parent"
android:textSize="18sp"
android:layout_height="wrap_content"
tools:text="CSC 460" />
<TextView
android:id="#+id/tct"
android:layout_width="match_parent"
android:textSize="18sp"
android:layout_height="wrap_content"
tools:text= "Information Security"/>
</LinearLayout>
<LinearLayout
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/tc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:padding="25dp"
android:textSize="18sp"
tools:text="1"/>
<TextView
android:id="#+id/tm"
android:layout_width="wrap_content"
android:padding="25dp"
android:textSize="18sp"
android:layout_height="wrap_content"
tools:text="78.4"/>
<TextView
android:id="#+id/tg"
android:layout_width="wrap_content"
android:padding="25dp"
android:textSize="18sp"
android:layout_height="wrap_content"
tools:text="A"/>
</LinearLayout>
Screenshot:the grade "A" is not displaying
Screenshot
try this way
<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:baselineAligned="false"
android:orientation="horizontal"
android:padding="16dp"
>
<LinearLayout
android:id="#+id/right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical"
>
<TextView
android:id="#+id/tcc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
tools:text="CSC 460"
/>
<TextView
android:id="#+id/tct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
tools:text="Information Security"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:id="#+id/tc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textAlignment="center"
android:textSize="18sp"
tools:text="1"
/>
<TextView
android:id="#+id/tm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="18sp"
tools:text="78.4"
/>
<TextView
android:id="#+id/tg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="18sp"
tools:text="A"
/>
</LinearLayout>
</LinearLayout>
hope this helps
I am working with com.beardedhen.androidbootstrap.BootstrapDropDown and I don't understand how to get the value of the selected item from the dropdown
dropdownresource.xml
`<resources>
<string name="app_name">GTFirstPage</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
<string name="action_settings">Settings</string>
<array name="bootstrap_dropdown_example_data">
<item>Family Trip</item>
<item>Camping</item>
<item>Business Trip</item>
<item>{dropdown_separator}</item>
<item>Custom Trip</item>
</array>
</resources>`
plan.xml
<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="com.example.hp.gtfirstpage.plantrip">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/autotv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center_horizontal|top"
android:layout_marginTop="25dp"
android:background="#f3f3f3"
android:hint="Where do you want to go?"
android:textSize="30sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="107dp"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/textView2"
android:layout_width="176dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Destination"
android:textSize="30sp" />
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#f3f3f3"
android:ems="10"
android:textSize="20sp"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="107dp"
android:orientation="horizontal">
<TextView
android:id="#+id/textView11"
android:layout_width="176dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="0.00"
android:text="Category"
android:textSize="30sp"/>
<com.beardedhen.androidbootstrap.BootstrapDropDown
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:bootstrapBrand="regular"
app:bootstrapExpandDirection="down"
app:bootstrapSize="md"
app:bootstrapText="Select Category"
app:dropdownResource="#array/bootstrap_dropdown_example_data"
app:roundedCorners="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="107dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Date"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="85dp"
android:orientation="horizontal">
<TextView
android:id="#+id/tv4"
android:layout_width="165dp"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:background="#f3f3f3"
android:inputType="date" />
<TextView
android:id="#+id/textView14"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:text="to"
android:textSize="30sp" />
<TextView
android:id="#+id/tv5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:textSize="20sp"
android:background="#f3f3f3"
android:inputType="date" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="107dp"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<Button
android:id="#+id/bt1"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="BACK" />
<Button
android:id="#+id/bt2"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_weight="1"
android:text="DONE" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
plantrip.java
package com.example.hp.gtfirstpage;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.TextView;
import android.widget.Toast;
import com.beardedhen.androidbootstrap.BootstrapDropDown;
public class plantrip extends Fragment implements
AdapterView.OnItemSelectedListener, DatePickerDialog.OnDateSetListener
{
BootstrapDropDown spin;
String city,category,dest;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View root=null;
root= inflater.inflate(R.layout.fragment_plantrip, container, false);
spin = (BootstrapDropDown) root.findViewById(R.id.spinner);
spin.setOnDropDownItemClickListener(new
BootstrapDropDown.OnDropDownItemClickListener() {
#Override
public void onItemClick(ViewGroup parent, View v, int id) {
// category=spin.getResources().getResourceName(spin.getId());
}
});
return root;
}
}
I am new to android so if there is any mistake please explain properly.
Please tell me how to retrieve items from dropdown list.
Thank You!!!
BootstrapDropDown has a method called getDropdownData();
This method returns a String[] array containing the items in your BootstapDropDown. Use int id as an index for your array. The 'int id' gets passed to you as a parameter of the method:
public void onItemClick(ViewGroup parent, View v, int id) {...}
I believe you can grab the selected item by adding this line to your code:
String[] dropDownItems = spin.getDropdownData();
String item = dropDownItems[id];
spin.setOnDropDownItemClickListener(new
BootstrapDropDown.OnDropDownItemClickListener() {
#Override
public void onItemClick(ViewGroup parent, View v, int id) {
// category=spin.getResources().getResourceName(spin.getId());
String[] dropDownItems = spin.getDropdownData();
String item = dropDownItems[id];
}
});
// made a couple of edits to fix a mistake
// I hope this helped!
used tabpane take 120dp height in top.
mainxml:
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#E0E0E0"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.suranpc.recy.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="120dp"
android:scrollbars="vertical"
/>
</RelativeLayout>
cardview design xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="3dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:padding="16dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#ffffff"
android:orientation="horizontal"
android:padding="16dp"
>
<TextView
android:id="#+id/firstcardtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="TextView"
android:textColor="#color/colorFontblack"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="60dp"
android:background="#ffffff"
android:orientation="horizontal"
android:padding="4dp"
>
<TextView
android:id="#+id/firstcardtext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="TextView :"
android:textAlignment="center"
android:textColor="#color/colorFontblack"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/firstcardtext3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.91"
android:gravity="right"
android:text="TextView"
android:textAlignment="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:background="#ffffff"
android:orientation="horizontal"
android:padding="1dp"
>
<TextView
android:id="#+id/firstcardtext4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.92"
android:gravity="left"
android:text="TextView:"
android:textAlignment="center"
android:textColor="#color/colorFontblack"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/firstcardtext5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.98"
android:gravity="right"
android:text="20000"
android:textAlignment="center"
android:textColor="#color/colorFontheader"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:background="#ffffff"
android:orientation="horizontal"
android:padding="1dp"
>
<TextView
android:id="#+id/firstcardtext6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="TextView:"
android:textAlignment="center"
android:textColor="#color/colorFontblack"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/firstcardtext7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="TextView"
android:textAlignment="center"
android:textColor="#color/colorFontheader"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
recycler adapter code:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class Readapter extends RecyclerView.Adapter<Readapter.ProductViewHolder> {
//this context we will use to inflate the layout
private Context mCtx;
//we are storing all the products in a list
private List<Front> productList;
//getting the context and product list with constructor
public Readapter(Context mCtx, List<Front> productList) {
this.mCtx = mCtx;
this.productList = productList;
}
#Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflating and returning our view holder
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.layoutte,parent,false);
return new ProductViewHolder(view);
}
#Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
//getting the product of the specified position
Front product = productList.get(position);
//binding the data with the viewholder views
holder.textView1.setText(product.getbud());
holder.textView2.setText(product.getbudnum());
holder.textView3.setText(product.gettosp());
holder.textView4.setText(product.gettospnum());
holder.textView5.setText(product.getrem());
holder.textView6.setText(product.getremnum());
holder.textView7.setText(product.getremnums());
}
#Override
public int getItemCount() {
return productList.size();
}
class ProductViewHolder extends RecyclerView.ViewHolder {
TextView textView1, textView2, textView3, textView4, textView5,textView6,textView7;
public ProductViewHolder(View itemView) {
super(itemView);
textView1 = itemView.findViewById(R.id.firstcardtext);
textView2 = itemView.findViewById(R.id.firstcardtext2);
textView3 = itemView.findViewById(R.id.firstcardtext3);
textView4 = itemView.findViewById(R.id.firstcardtext4);
textView5 = itemView.findViewById(R.id.firstcardtext5);
textView6 = itemView.findViewById(R.id.firstcardtext6);
textView7 = itemView.findViewById(R.id.firstcardtext7);
}
}
}
Mainactivity code(add datas):
recyclerView=(RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
productList =new ArrayList<>();
productList.add(new
Front("tested1","tested","tested","tested","tested","tested","tested"));
productList.add(new
Front("tested2","tested","tested","tested","tested","tested","tested"));
productList.add(new
Front("tested3","tested","tested","tested","tested","tested","tested"));
productList.add(new Front("tested4","tested","tested","tested","tested","tested","tested"));
Readapter adapter=new Readapter(this,productList);
recyclerView.setAdapter(adapter);
only two cards are visible in screen.another two cards not visibled.fixed height datas only showed.(not scrolled).
only test1 and test2 datas visible in screen another test3 abd test4 not viewd and scrolled .
appbar xml code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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="com.example.suranpc..MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:background="#color/colorTabbg"
app:tabSelectedTextColor="#color/colorFontwhite"
app:tabIndicatorColor="#color/colorFontwhite"
app:tabIndicatorHeight="4dp"
app:tabTextColor="#color/colorFontblack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabGravity="fill"
android:id="#+id/tbl_pages"/>
<!-- <tabbed pane" />-->
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/vp_pages"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
tabpane code in mainactivity
//the tabpane>
ViewPager vp_pages= (ViewPager) findViewById(R.id.vp_pages);
PagerAdapter pagerAdapter=new FragmentAdapter(getSupportFragmentManager());
vp_pages.setAdapter(pagerAdapter);
TabLayout tbl_pages= (TabLayout) findViewById(R.id.tbl_pages);
final int[]ICONS=new int[]{
R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera,
};
tbl_pages.setupWithViewPager(vp_pages);
tbl_pages.getTabAt(0).setIcon(ICONS[0]);
tbl_pages.getTabAt(1).setIcon(ICONS[1]);
tbl_pages.getTabAt(2).setIcon(ICONS[2]);
tbl_pages.getTabAt(3).setIcon(ICONS[3]);
//<tabpane>
Try this just Change this in your cardview design xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView 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="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:padding="16dp"
app:cardCornerRadius="3dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#ffffff"
android:orientation="horizontal"
android:padding="16dp"
>
<TextView
android:id="#+id/firstcardtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="TextView"
android:textColor="#color/colorFontblack"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="60dp"
android:background="#ffffff"
android:orientation="horizontal"
android:padding="4dp"
>
<TextView
android:id="#+id/firstcardtext2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="TextView :"
android:textAlignment="center"
android:textColor="#color/colorFontblack"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/firstcardtext3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.91"
android:gravity="right"
android:text="TextView"
android:textAlignment="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:background="#ffffff"
android:orientation="horizontal"
android:padding="1dp"
>
<TextView
android:id="#+id/firstcardtext4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.92"
android:gravity="left"
android:text="TextView:"
android:textAlignment="center"
android:textColor="#color/colorFontblack"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/firstcardtext5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.98"
android:gravity="right"
android:text="20000"
android:textAlignment="center"
android:textColor="#color/colorFontheader"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:background="#ffffff"
android:orientation="horizontal"
android:padding="1dp"
>
<TextView
android:id="#+id/firstcardtext6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="TextView:"
android:textAlignment="center"
android:textColor="#color/colorFontblack"
android:textSize="22dp"
android:textStyle="bold" />
<TextView
android:id="#+id/firstcardtext7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="TextView"
android:textAlignment="center"
android:textColor="#color/colorFontheader"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
EDIT
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E0E0E0"
tools:context="com.example.suranpc.recy.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginTop="120dp"
android:scrollbars="vertical" />
</LinearLayout>
Display content main xml behalf of tabpane fragment(home page xml).use fragment it works.
i am trying to set my json into my Recyclerview, where i can looking with recycler view, but the json is shown in just one row. How can fix this?
This is my code :
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(PenawaranDetail.this, LinearLayoutManager.HORIZONTAL, true));
recyclerView.setHasFixedSize(true);
This is my Adapter Code :
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
import android.widget.ScrollView;
import android.widget.TextView;
/**
* Created by Programx4 on 1/21/2017.
*/
public class AdapterBarangPenawaran extends RecyclerView.Adapter <AdapterBarangPenawaran.ViewHolder> {
private Context context;
private List<DataBarangPenawaran> data;
public AdapterBarangPenawaran(Context context, List<DataBarangPenawaran>data){
this.context = context;
this.data = data;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View Itemview = LayoutInflater.from(parent.getContext()).inflate(R.layout.daftarbarangpenawaran, parent, false);
return new ViewHolder(Itemview);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.tvnama.setText(data.get(position).getBarang());
holder.tvjumlah.setText(String.valueOf(data.get(position).getJumlah()));
holder.tvsup1.setText(String.valueOf(data.get(position).getHarga1()));
holder.tvsup2.setText(String.valueOf(data.get(position).getHarga2()));
holder.tvsup3.setText(String.valueOf(data.get(position).getHarga3()));
}
#Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView tvnama, tvjumlah, tvsup1, tvsup2, tvsup3, tvtotal;
public ViewHolder(View Itemview){
super(Itemview);
tvjumlah = (TextView)Itemview.findViewById(R.id.jumlah);
tvnama = (TextView)Itemview.findViewById(R.id.namabar);
tvsup1 = (TextView)Itemview.findViewById(R.id.sup1);
tvsup2 = (TextView)Itemview.findViewById(R.id.sup2);
tvsup3 = (TextView)Itemview.findViewById(R.id.sup3);
tvtotal = (TextView)Itemview.findViewById(R.id.total);
}
}
}
and this is my xml layout :
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#383735"
android:baselineAligned="false">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:id="#+id/koded"
android:textColor="#ffffff"
android:text="Kode: "
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/koded"
android:textColor="#ffffff"
android:id="#+id/isikoded"/>
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_below="#id/isikoded"
android:id="#+id/tglaju"
android:textColor="#ffffff"
android:text="Tgl Input: "
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/tglaju"
android:layout_below="#+id/isikoded"
android:id="#+id/isitglaju"
android:textColor="#ffffff"
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_below="#id/isitglaju"
android:id="#+id/jpegawai"
android:textColor="#ffffff"
android:text="Pegawai: "
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/jpegawai"
android:layout_below="#+id/isitglaju"
android:id="#+id/isipegawai"
android:textColor="#ffffff"
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_below="#id/isipegawai"
android:id="#+id/jdivisi"
android:textColor="#ffffff"
android:text="Divisi: "
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/jdivisi"
android:layout_below="#+id/isipegawai"
android:id="#+id/isidivisi"
android:textColor="#ffffff"
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_below="#id/isidivisi"
android:id="#+id/tglbth"
android:textColor="#ffffff"
android:text="Tgl Penawaran: "
android:layout_marginTop="10dp"
/>
<TextView android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/tglbth"
android:layout_below="#+id/isidivisi"
android:id="#+id/isitglbutuh"
android:textColor="#ffffff"/>
<TextView
android:layout_width="130dp"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:id="#+id/supd"
android:layout_marginTop="10dp"
android:text="No Penawaran: "
android:layout_below="#+id/tglbth"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/isitglbutuh"
android:layout_toRightOf="#id/supd"
android:id="#+id/isinomor"
android:textColor="#ffffff"
android:layout_marginTop="10dp"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/isinomor"
android:id="#+id/tablejdl"
android:weightSum="7"
android:layout_marginTop="20dp"
>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:weightSum="7"
android:gravity="center_horizontal">
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:text="Barang"
android:layout_weight="2"
android:id="#+id/kodebar"
/>
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:text="Jumlah"
android:layout_weight="1"
android:id="#+id/jumlah"
/>
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:text="Sup1"
android:layout_weight="1"
android:id="#+id/namabar"/>
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:text="Sup2"
android:layout_weight="1"
android:id="#+id/jumlahbar"/>
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:text="Sup3"
android:layout_weight="1"
android:id="#+id/stokbar"/>
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:text="Total"
android:layout_weight="1"
android:id="#+id/jtotal"/>
</TableRow>
</TableLayout>
<ScrollView
android:id="#+id/aaaa"
android:layout_width="match_parent"
android:layout_below="#id/tablejdl"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerpenawaran">
</android.support.v7.widget.RecyclerView>
</ScrollView>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/aaaa"
android:id="#+id/tablejdl2"
android:weightSum="7"
android:layout_marginTop="15dp"
>
<TableRow
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:weightSum="7"
android:gravity="center_horizontal">
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:layout_weight="2"
/>
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<CheckBox
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/checksup1"/>
<CheckBox
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/checksup2"/>
<CheckBox
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/checksup3"/>
<TextView
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:textSize="11sp"
android:layout_height="wrap_content"
android:text="Total"
android:layout_weight="1"
android:id="#+id/total"/>
</TableRow>
</TableLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tablejdl2"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="20dp"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tolak"
android:id="#+id/tolak"
android:background="#drawable/buttonback2"
android:layout_alignParentRight="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Approve"
android:layout_alignParentLeft="true"
android:id="#+id/btnapprove"
android:background="#drawable/buttonback"
/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>
Thanks for your help
I think the problem is order ...
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(PenawaranDetail.this,LinearLayoutManager.HORIZONTAL, true));
recyclerView.setAdapter(adapter);
First, set layout manager before set adapter
recyclerView.setLayoutManager(new LinearLayoutManager(PenawaranDetail.this, LinearLayoutManager.HORIZONTAL, true));
recyclerView.setAdapter(adapter);
Second, change your item layout root parameters, the height should be wrap_content
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
....
</LinearLayout>
I am currently working with an Android App using RecyclerView and CardView .I am showing lists of UserPosts(Contains text and Image both) inside RecyclerView . The UserPost item has been created using CardView .
Edit
The problem I am facing here is onCreateViewHolder method is being called only half of the times the actual count returned by getItemCount method even I scrolls till end of the list .E.g I have a list of 10 posts then onCreateViewHolder calls for 5 times only . It should call 10 times (when I scroll till end of the list) first and then created views will be recycled . But it is not happening actually . I have visited many Stackoverflow posts but no luck .
Here is code UserPostAdapter.java :
package com.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.model.UserPostsListItem;
import com.musomeet.R;
import com.squareup.picasso.Picasso;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
/**
* Created by SI_Android on 10/21/2015 .
*
*/
public class UserPostAdapter extends RecyclerView.Adapter<UserPostAdapter.UserPostViewHolder> {
private List<UserPostsListItem> userPostList;
Context conetxt ;
LayoutInflater inflater ;
public UserPostAdapter(){
}
public UserPostAdapter(Context conetxt, List<UserPostsListItem> postlist) {
this.userPostList = postlist;
this.conetxt = conetxt ;
inflater = LayoutInflater.from(this.conetxt);
}
#Override
public int getItemCount() {
//int size = 0 ;
//if( userPostList != null )
//size = userPostList.size();
return userPostList.size();
}
#Override
public void onBindViewHolder(UserPostViewHolder videoViewHolder, int position) {
UserPostsListItem item = userPostList.get(position);
videoViewHolder.userName.setText(item.getName());
videoViewHolder.userLocation.setText(item.getCity()+", "+item.getCountry());
videoViewHolder.timeAgo.setText(item.getAgo());
videoViewHolder.likes.setText(item.getNo_of_likes());
videoViewHolder.comments.setText(item.getNo_of_comments());
videoViewHolder.userMessage.setText(item.getContent());
if(!TextUtils.isEmpty(item.getPhoto())) {
Picasso.with(conetxt).load(item.getPhoto()).into(videoViewHolder.circleImageView);
}
String url = item.getAttachment_url() ;
if (!TextUtils.isEmpty(url)) {
if ((url.endsWith(".png") || url.endsWith(".jpg"))) {
//videoViewHolder.attachment.setVisibility(View.VISIBLE);
Picasso.with(conetxt).load(url).into(videoViewHolder.attachment);
}
}
}
#Override
public UserPostViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = inflater.inflate(R.layout.userposts_listitem_textmessage, viewGroup, false);
return new UserPostViewHolder(itemView);
}
public static class UserPostViewHolder extends RecyclerView.ViewHolder {
TextView userName, userLocation, timeAgo, likes, comments, userMessage;
ImageView attachment;
CircleImageView circleImageView;
public UserPostViewHolder(View v){
super(v);
attachment = (ImageView) v.findViewById(R.id.attachment);
circleImageView = (CircleImageView) v.findViewById(R.id.circleView);
userMessage = (TextView) v.findViewById(R.id.massage);
userName = (TextView) v.findViewById(R.id.name);
userLocation = (TextView) v.findViewById(R.id.location);
timeAgo = (TextView) v.findViewById(R.id.time);
likes = (TextView) v.findViewById(R.id.likespoints);
comments = (TextView) v.findViewById(R.id.commentspoints);
}
}
}
Here is the userposts_listitem_textmessage.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardView="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/userpopstcardview"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
cardView:cardElevation="5dp"
cardView:cardCornerRadius="10dp"
cardView:cardBackgroundColor="#fff"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/contact"
android:layout_margin="10dp"
android:id="#+id/circleView"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_marginLeft="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="Jasmine"
android:textSize="20sp"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#929292"
android:text="Los Angles, California"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#929292"
android:text="-"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#929292"
android:text="2 hours ago"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/userListContentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:orientation="vertical">
<TextView
android:id="#+id/massage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/recet_text"
android:textColor="#000"
android:textSize="15sp"
android:padding="10dp"
android:scrollbars="vertical"/>
<ImageView
android:id="#+id/attachment"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="10dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:tint="#00000000"
android:src="#drawable/contact"
android:visibility="visible"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/likespoints"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textColor="#929292"
android:textSize="14sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Likes"
android:textColor="#929292"
android:textSize="14sp"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="30dp">
<TextView
android:id="#+id/commentspoints"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:textColor="#929292"
android:textSize="14sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comments"
android:textColor="#929292"
android:textSize="14sp"
android:layout_marginLeft="5dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_height="1dp"
android:layout_width="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#eeeeee"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/like"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Like"
android:gravity="center"
android:layout_gravity="center"
android:textSize="14sp"
android:textColor="#929292"
android:padding="10dp"
android:clickable="true"
android:background="#drawable/textview_effect"/>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="1dp">
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#eeeeee"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/comment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Comment"
android:gravity="center"
android:layout_gravity="center"
android:textSize="14sp"
android:textColor="#929292"
android:padding="10dp"
android:clickable="true"
android:background="#drawable/textview_effect"/>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="1dp">
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#eeeeee"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/share"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Share"
android:gravity="center"
android:layout_gravity="center"
android:textSize="14sp"
android:textColor="#929292"
android:padding="10dp"
android:clickable="true"
android:background="#drawable/textview_effect"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
What you see is an expected behaviour. RecylerView won't create as many view as adapter's getItemCount() returns. It create view as many as it's necessary for rendering a view.
For instance, if your adapter contains 100 elements, but only 5 are displayed at at time, system will create 5 views (it may create 1 or 2 more view as "buffer") and reuse these views when user scrolls.
When a View is recycled, it doesn't get "reset" to its original state, so it and its child Views still have the attributes that were set on them the last time the ViewHolder was passed into onBindViewHolder(). Since you are currently only setting the image when your if conditions are true, the ImageView is not updated if either of those conditions is false, and it will still have the image set on it previously. You simply need to set the ImageView's Bitmap to null when those conditions are not true. Also, your two if statements can be combined:
#Override
public void onBindViewHolder(UserPostViewHolder videoViewHolder, int position) {
UserPostsListItem item = userPostList.get(position);
...
String url = item.getAttachment_url() ;
if (!TextUtils.isEmpty(url) && (url.endsWith(".png") || url.endsWith(".jpg"))) {
Picasso.with(conetxt).load(url).into(videoViewHolder.attachment);
}
else {
videoViewHolder.attachment.setImageBitmap(null);
}
}