I am writing a ListView with 2 TextViews per row, but when I execute I get only one TextView.
Here is my adapter extending Base adapter
public class GlossaryListViewAdapter extends BaseAdapter {
List<Glossary_Entry> glossary_entryList;
Context context;
public GlossaryListViewAdapter(Context context,List<Glossary_Entry> glossary_entryList){
this.glossary_entryList = glossary_entryList;
this.context = context;
}
#Override
public int getCount() {
return glossary_entryList.size();
}
#Override
public Object getItem(int postion) {
return glossary_entryList.get(postion);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if(view == null){
view = LayoutInflater.from(context).inflate(R.layout.glossary_single_row,viewGroup,false);
viewHolder = new ViewHolder(view);
view.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)view.getTag();
}
viewHolder.titleTextView.setText(glossary_entryList.get(position).getTitle());
viewHolder.definitionTextView.setText(glossary_entryList.get(position).getDefinition());
return view;
}
private static class ViewHolder{
TextView titleTextView;
TextView definitionTextView;
public ViewHolder(View view){
titleTextView = (TextView)view.findViewById(R.id.glossary_title_textview);
definitionTextView = (TextView)view.findViewById(R.id.glossary_word_definition_textview);
}
}
}
Here is my Activity code
public class GlossaryAcvtivity extends AppCompatActivity {
ListView glossaryListView;
ListAdapter listAdapter;
List<Glossary_Entry> glossary_entryList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glossary);
/**
* Sample data
*/
glossary_entryList = new ArrayList<>();
glossary_entryList.add( new Glossary_Entry("Force","This is a push or push on an object.They can be duew to a phenomenon such as gravity, magnetism, or anything that might cause a mass to accelerate."));
glossary_entryList.add( new Glossary_Entry("Motion","Change in position of an object over time, described in terms of displacement,distance,velocity,acceleration,time and speed"));
glossary_entryList.add( new Glossary_Entry("Quantum Leap","An Abrupt transition of an electron,atom, or molecule from one quantum state to another,with the absorption or emission of a quantum"));
glossary_entryList.add( new Glossary_Entry("Force","This is a push or push on an object.They can be duew to a phenomenon such as gravity, magnetism, or anything that might cause a mass to accelerate."));
glossaryListView = (ListView)findViewById(R.id.glossary_list_view);
listAdapter = new GlossaryListViewAdapter(getApplicationContext(),glossary_entryList);
glossaryListView.setAdapter(listAdapter);
}
}
The source of my data is an Entry class
public class Glossary_Entry {
public String title;
public String definition;
public String getTitle() {
return title;
}
public String getDefinition() {
return definition;
}
public Glossary_Entry(String title,String definition){
this.title = title;
this.definition = definition;
}
}
Here is my glossary_single_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textColor="#color/blue"
android:textStyle="bold"
android:textSize="16sp"
android:layout_margin="4dp"
android:id="#+id/glossary_title_textview"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_description"
android:layout_margin="4dp"
android:textSize="12sp"
android:id="#+id/glossary_word_definition_textview"/>
</LinearLayout>
Here is my glossary_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/white"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:id="#+id/glossary_toolbar"
android:background="#color/colorPrimary"
android:layout_height="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
<ListView
android:layout_width="match_parent"
android:id="#+id/glossary_list_view"
android:layout_margin="8dp"
android:layout_below="#+id/glossary_toolbar"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
Please help when I execute I get only one TextView per row...
Apparently, I needed to change the text color to black in the second TextView. The text was there just not visible.
<LinearLayout xmlns:android="http:// schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textColor="#color/blue"
android:textStyle="bold"
android:textSize="16sp" android:layout_margin="4dp"
android:id="#+id/glossary_title_textview"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_description"
android:layout_margin="4dp"
android:textSize="12sp"
android:textColor="#color/black"
android:id="#+id/glossary_word_definition_textview"/>
</LinearLayout
Related
I am trying to load a RecyclerView with data, but I get a NullPointerException every time I want to load the RecyclerView. (The RecyclerView is displayed in a Dialog). First, I have this class:
public class FondosClase {
private int recurso;
private String nombre;
public int getRecurso() {
return recurso;
}
public void setRecurso(int recurso) {
this.recurso = recurso;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public FondosClase(){}
public FondosClase(int recurso, String nombre) {
this.recurso = recurso;
this.nombre = nombre;
}
}
Then my adapter:
public class AdaptadorMenuIzq extends RecyclerView.Adapter<AdaptadorMenuIzq.ViewHolder>{
private FondosClase[] fondosClaseArrayList;
private Context context;
public AdaptadorMenuIzq(FondosClase[] fondosClaseArrayList, Context context) {
this.fondosClaseArrayList = fondosClaseArrayList;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutInflater = LayoutInflater.from(parent.getContext()).inflate(R.layout.design_rv_bg_men_izq, parent, false);
return new AdaptadorMenuIzq.ViewHolder(layoutInflater);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
final FondosClase fondosClase = fondosClaseArrayList[position];
holder.textView.setText(fondosClase.getNombre());
holder.imageView.setImageResource(fondosClase.getRecurso());
}
#Override
public int getItemCount() {
return fondosClaseArrayList.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public TextView textView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
this.imageView = itemView.findViewById(R.id.imageView);
this.textView = itemView.findViewById(R.id.textView);
}
}
}
Then create a class where I create the method for Dialog:
public class Alertas {
private AdaptadorMenuIzq adaptadorMenuIzq;
public void AlertaMenuIzquierdoFondo(Context context){
LayoutInflater layoutInflater = LayoutInflater.from(context);
Dialog anuncio = new Dialog(context, R.style.FondoDialog);
final View view = layoutInflater.inflate(R.layout.alerta_fondo_tarjeta_cv_rv, null);
final RecyclerView rv = view.findViewById(R.id.rv_tarjetas);
FondosClase[] fondosClases = new FondosClase[]{
new FondosClase(R.drawable.permanent_bg_a, "a"),
new FondosClase(R.drawable.permanent_bg_b, "b")
};
adaptadorMenuIzq = new AdaptadorMenuIzq(fondosClases, context);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(context));
rv.setAdapter(adaptadorMenuIzq);
anuncio.setContentView(view);
anuncio.show();
}
}
And from my MainActivity I run it through a FAB:
binding.appBarMain.fab.setOnClickListener(view ->{
alertas = new Alertas();
alertas.AlertaMenuIzquierdoFondo(MainActivity.this);
}
);
When I press the button to show it I get the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.frabasoft.genshinimpactrecursos.Adaptadores.AdaptadorMenuIzq.onBindViewHolder(AdaptadorMenuIzq.java:35)
The line that marks the error is the following:
holder.textView.setText(fondosClase.getNombre());
I changed it to:
holder.textView.setText(fondosClaseArrayList[position].geNombre());
But, the result was the same.
I attach the xml:
alerta_fondo_tarjeta_cv_rv:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_gravity="center"
app:cardCornerRadius="75dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="25dp"
android:layout_marginRight="25dp"
android:layout_marginBottom="25dp"
android:layout_marginLeft="25dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_tarjetas"
android:layout_marginTop="25dp"
android:layout_marginRight="25dp"
android:layout_marginBottom="25dp"
android:layout_marginLeft="25dp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
design_rv_bg_menu_izq:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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"
style="#style/CardView.Light"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:elevation="5dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/ivIcon"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#mipmap/ic_launcher"
android:layout_margin="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvIcon"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="#mipmap/ic_launcher"
android:layout_margin="8dp"
android:text="HOLA"
android:gravity="center"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
Does someone tell me what data I'm going wrong?
in the view holder you are not passing your XML view id's
it should be:
this.imageView = itemView.findViewById(R.id.ivIcon);
this.textView = itemView.findViewById(R.id.tvIcon);
RecyclerView is not showing at all. Only the TextView is displayed.
I looked at other questions and used all answers from previous questions.
Other answers suggested that recycler's width can't be set wrap content or that setAdapter should be called after setting layout manager and I meet these conditions.
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.main_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
final MainAdapter adapter = new MainAdapter();
recyclerView.setAdapter(adapter);
List<FirebaseProduct> firebaseProductList = new ArrayList<>();
firebaseProductList = getData(); //Here is my Firebase code
adapter.setList(firebaseProductList);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/main_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/aktualne_produkty"
android:gravity="center"
android:background="#color/colorPrimary"
android:textAppearance="#style/TextAppearance.AppCompat.Large">
</TextView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/main_recycler"
android:layout_height="wrap_content"
android:layout_width="match_parent"
tools:listitem="#layout/card_main"
android:layout_below="#id/main_text"/>
</RelativeLayout>
MainAdapter
public class MainAdapter extends RecyclerView.Adapter<MainAdapter.MainHolder> {
private List<FirebaseProduct> productList = new ArrayList<>();
#NonNull
#Override
public MainHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_main, parent, false);
return new MainHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull MainHolder holder, int position) {
FirebaseProduct product = productList.get(position);
Log.d("Prod holder", "name - " + product.getProductName());
Log.d("Prod holder", "num - " + product.getProductNumber());
holder.nameView.setText(product.getProductName());
holder.numView.setText(product.getProductNumber());
}
#Override
public int getItemCount() {
return productList.size();
}
public void setProductList(List<FirebaseProduct> productList1){
this.productList = productList1;
notifyDataSetChanged();
}
public List<FirebaseProduct> getList(){
return productList;
}
public static class MainHolder extends RecyclerView.ViewHolder{
private TextView nameView;
private TextView numView;
public MainHolder(View itemView){
super(itemView);
nameView = itemView.findViewById(R.id.product_name);
numView = itemView.findViewById(R.id.product_count);
}
}
}
card_main
<androidx.cardview.widget.CardView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:layout_weight="3">
<TextView
android:id="#+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
</TextView>
<TextView
android:id="#+id/product_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/product_name">
</TextView>
</RelativeLayout>
<ImageView
android:id="#+id/product_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:contentDescription="#string/product_description">
</ImageView>
</LinearLayout>
</androidx.cardview.widget.CardView>
You are calling adapter.setList(firebaseProductList); in MainActivity but the function name in MainAdapter is setProductList(). So change it to adapter. setProductList(firebaseProductList)
Also, you want to be sure getData(); in MainActivity should return a non-empty list. you can verify it using debugger or by just adding a log statement before adapter.setList(firebaseProductList). like this:
Log.d("LIST_SIZE", firebaseProductList.size()); //this will print list size
adapter.setList(firebaseProductList); //you have to change it to adapter. setProductList(firebaseProductList)
I've been trying to get a custom Recycler view adapter to work. I can't quite see why it wouldn't work. What I find strange is that the code for the custom adapter does not execute (as in it doesn't break when I put a breakpoint there) and I'm certain I bind the custom adapter to the recycler view. I hope someone can see why it doesn't work.
Activity:
public class payment_history extends AppCompatActivity {
RecyclerView list;
ArrayList<pay_item> list_data;
pay_adapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment_history);
list_data = new ArrayList<>();
list_data.add(new pay_item(10, 100, "jow", "10"));
list_data.add(new pay_item(10, 100, "joe", "10"));
list_data.add(new pay_item(10, 100, "joe", "10"));
list = findViewById(R.id.payment_history_list);
list.setLayoutManager(new LinearLayoutManager(this));
adapter = new pay_adapter(list_data);
list.setHasFixedSize(true);
list.setAdapter(adapter);
}
}
class pay_item {
int time;
double amount;
String name, table;
pay_item(int time, double amount, String name, String table) {
this.time = time;
this.amount = amount;
this.name = name;
this.table = table;
}
}
Adapter:
class pay_adapter extends RecyclerView.Adapter<pay_adapter.viewholder> {
private ArrayList<pay_item> data;
pay_adapter(ArrayList<pay_item> in) {
data = in;
}
#NonNull
#Override
public viewholder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.payment_history_item, parent, false);
return new viewholder(v);
}
#Override
public void onBindViewHolder(#NonNull viewholder holder, int position) {
holder.name.setText(data.get(position).name);
holder.table.setText(data.get(position).table);
holder.amm.setText(String.valueOf(data.get(position).amount));
holder.time.setText(data.get(position).time);
}
#Override
public int getItemCount() {
return 0;
}
static class viewholder extends RecyclerView.ViewHolder {
TextView time, amm, name, table;
viewholder(View view) {
super(view);
time = view.findViewById(R.id.pay_time);
amm = view.findViewById(R.id.pay_amount);
name = view.findViewById(R.id.pay_name);
table = view.findViewById(R.id.pay_table);
}
}
}
Row view:
<?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="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="16dp">
<TextView
android:id="#+id/pay_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Time"
android:textColor="#000000"
android:textSize="36sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/pay_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:id="#+id/pay_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:text="table" />
</LinearLayout>
<TextView
android:id="#+id/pay_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0"
android:text="Amount"
android:textColor="#color/colorPrimary"
android:textSize="36sp" />
</LinearLayout>
Layout with recycler view:
<?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:orientation="vertical"
tools:context=".payment_history">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/payment_history_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Any help is appreciated.
Two things
pay_adapter(ArrayList<pay_item> in) {
data = in;
}
#Override
public int getItemCount() {
return data.size() ;
}
In the UserListAdapter
public class UserListAdapter extends RecyclerView.Adapter
gives this error
Class UserListAdapter must either be declared abstract or implement abstract method onBindViewHolder(VH, int) in Adapter
I've looked into other answers on stack overflow but I haven't found anything that fixes the error.
UserListAdapter.java
public class UserListAdapter extends RecyclerView.Adapter {
private List<String> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
// data is passed into the constructor
UserListAdapter(Context context, List<String> data) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
}
// inflates the row layout from xml when needed
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.user_layout, parent, false);
return new ViewHolder(view);
}
// binds the data to the TextView in each row
public void onBindViewHolder(ViewHolder holder, int position) {
String animal = mData.get(position);
holder.myTextView.setText(animal);
}
// total number of rows
public int getItemCount() {
return mData.size();
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;
ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.txtStudentNameSubjectInfoPage);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
// convenience method for getting data at click position
String getItem(int id) {
return mData.get(id);
}
// allows clicks events to be caught
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
user_layout.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="wrap_content"
android:paddingTop="8dp">
<ImageView
android:id="#+id/imgStudentIconSubjectInfoPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#tools:sample/avatars[13]" />
<TextView
android:id="#+id/txtStudentNameSubjectInfoPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="User Name"
android:textAlignment="center"
android:textColor="#292929"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.1"
app:layout_constraintStart_toEndOf="#+id/imgStudentIconSubjectInfoPage"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_subject_info_page.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"
tools:context=".SubjectInfoPage">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="#+id/tableLayout"
tools:layout_editor_absoluteX="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Parent class RecyclerView.Adapter is confused with your's custom defined Viewholder versus its own. If you wanna use yours, extend RecyclerView.Adapter as follows
public class UserListAdapter extends RecyclerView.Adapter<UserListAdapter.ViewHolder>
{
.......
}
Cheers :)
I'm working to make a cool layout like this:
For this I'm using Android Parallax library from Github.
This library is creating all the views (as shown in picture) from xml itself.
But I want to create my own recyclerview, create adpaters, model classes and show them with cardview.
I tried using cardview and recyclerview.
Problem:
When I put RecyclerView's height as match_parent (android:layout_height="match_parent"), it gives UI like this:
Only first cardview is display with half part only. Other cardviews are overlapped somehow.
But when I give its height with fixed height like (1000dp, 2000dp), it shows the UI as expected (as shown in first figure). I think it is not a good solution to give its height fixed since data items may differ.
I don't understand what is wrong with my code. Please do suggest some solutions on this.
Following is my different views with my code.
activity_main.xml
<com.github.florent37.parallax.ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="#drawable/background"
android:tag="parallax=0.3" />
<TextView
style="#style/MyTitle"
android:layout_width="match_parent"
android:layout_height="160dp"
android:gravity="center"
android:tag="parallax=0.5"
android:text="My awesome title" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="150dp"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</LinearLayout>
</FrameLayout>
</com.github.florent37.parallax.ScrollView>
card_view.xml
<?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:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_margin="5dp"
card_view:cardCornerRadius="2dp"
card_view:contentPadding="10dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Hello"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_marginTop="10dp"
android:text="world" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
My Adapter class is like this:
public class MyRecyclerViewAdapter extends RecyclerView
.Adapter<MyRecyclerViewAdapter
.DataObjectHolder> {
private ArrayList<ContactsModel> mDataset;
private static MyClickListener myClickListener;
public static class DataObjectHolder extends RecyclerView.ViewHolder
implements View
.OnClickListener {
TextView label;
TextView dateTime;
public DataObjectHolder(View itemView) {
super(itemView);
label = (TextView) itemView.findViewById(R.id.textView);
dateTime = (TextView) itemView.findViewById(R.id.textView2);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
//myClickListener.onItemClick(getAdapterPosition(), v);
}
}
public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}
public MyRecyclerViewAdapter(ArrayList<ContactsModel> myDataset) {
mDataset = myDataset;
}
#Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_view, parent, false);
DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}
#Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
holder.label.setText(mDataset.get(position).getmText1());
holder.dateTime.setText(mDataset.get(position).getmText2());
}
public void addItem(ContactsModel dataObj, int index) {
mDataset.add(index, dataObj);
notifyItemInserted(index);
}
public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}
#Override
public int getItemCount() {
return mDataset.size();
}
public interface MyClickListener {
public void onItemClick(int position, View v);
}
}
In MainActivity, I have written code like this:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyRecyclerViewAdapter(getDataSet());
mRecyclerView.setAdapter(mAdapter);
Fabric.with(this, new Crashlytics());
}
private ArrayList<ContactsModel> getDataSet() {
ArrayList results = new ArrayList<ContactsModel>();
for (int index = 0; index < 20; index++) {
ContactsModel obj = new ContactsModel("Some Primary Text " + index,
"Secondary " + index);
results.add(index, obj);
}
return results;
}
}
Model class is like:
public class ContactsModel {
private String mText1;
private String mText2;
ContactsModel (String text1, String text2){
mText1 = text1;
mText2 = text2;
}
public String getmText1() {
return mText1;
}
public void setmText1(String mText1) {
this.mText1 = mText1;
}
public String getmText2() {
return mText2;
}
public void setmText2(String mText2) {
this.mText2 = mText2;
}
}
Apologies for the very long bulk of code.
Thanks!
You have a RecyclerView (which is scrollable) within a Scrollable Layout.
Replace your ScrollView tag with "android.support.v4.widget.NestedScrollView". This will allow the RecyclerView to properly expand.
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</android.support.v4.widget.NestedScrollView>
After Replaceing your ScrollView with NestedScrollView you can just add this line into the NestedScrollView
android:fillViewport="true"
I was having a same problem in my NestedScrollView.
Just added one line inside NestedScrollView and my problem solved.
android:fillViewport="true"