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)
Related
I am using card view within recycler view and I want to change the color of each single card view means Every card view has different color like in the picture I'm using retrofit technique for fetching data. please tell me where to put what code as I'm new in java android TIA
This is my adapter class
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder>{
private Context context;
private List<Corona> dataList;
public DataAdapter(Context context, List<Corona> dataList) {
this.context = context;
this.dataList = dataList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.data,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.nameTv.setText(dataList.get(position).getName());
holder.cityTv.setText(dataList.get(position).getCity());
}
#Override
public int getItemCount() {
return dataList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView nameTv, cityTv;
public MyViewHolder(View itemView) {
super(itemView);
nameTv = (TextView) itemView.findViewById(R.id.tvName);
cityTv = (TextView) itemView.findViewById(R.id.tvCity);
}
}
}
here is XML of Card View
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardBackgroundColor="#EDCC1A"
app:cardCornerRadius="15dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="#+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Hello World!"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#F00"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvCity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#0000FF"
android:textSize="25sp"
android:textStyle="bold"
android:layout_below="#+id/tvName"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
here is my main activity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private DataAdapter dataAdapter ;
private List<Corona> dataArrayList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews(){
recyclerView=(RecyclerView) findViewById(R.id.recview);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
getCoronaData();
}
private void getCoronaData() {
ApiInterface apiInterface;
apiInterface = ApiClient.getClient().create(ApiInterface.class);
dataArrayList = new ArrayList<>();
Call<List<Corona>> call = apiInterface.getCoronaDataForParsing();
call.enqueue(new Callback<List<Corona>>() {
#Override
public void onResponse(Call<List<Corona>> call, Response<List<Corona>> response) {
dataArrayList = response.body();
dataAdapter=new DataAdapter(getApplicationContext(),dataArrayList);
recyclerView.setAdapter(dataAdapter);
}
#Override
public void onFailure(Call<List<Corona>> call, Throwable t) {
}
});
}
}
first create integer.xml file under /res/values like below:
#Add as many color items as your want.
integer.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="color_array">
<item>#color/color1</item>
<item>#color/color2</item>
<item>#color/color3</item>
<item>#color/color4</item>
<item>#color/color5</item>
</integer-array>
Then, Inside RecyclerViewAdapter pass it through the constructor like below:
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.MyViewHolder>{
private Context context;
private List<Corona> dataList;
private int[] colorList;
public DataAdapter(Context context, List<Corona> dataList){
this.context = context;
this.dataList = dataList;
colorList = context.getResources().getIntArray(R.array.color_array);
}
........
#Override
public void onBindViewHolder(.....){
......
//for setting the different colors to each cardview.
GradientDrawable drawable = (GradientDrawable) holder.cardLayout.getBackground();
drawable.setColor(colorList[position % colorList.length]);
}
public class MyViewHolder extends RecyclerView.ViewHolder{
CardView cardLayout;
.........
public MyViewHolder(View itemView){
super(itemView)
.........
cardLayout = itemView.findViewById(R.id.cardLayout);
}
}
}
And, finally Inside XML of CardView provide id to Cardview as follow:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.cardview.widget.CardView
android:id="#+id/cardLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:cardBackgroundColor="#EDCC1A"
app:cardCornerRadius="15dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="#+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Hello World!"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#F00"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/tvCity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#0000FF"
android:textSize="25sp"
android:textStyle="bold"
android:layout_below="#+id/tvName"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
First of all set an ID to cardView in your XML file, then initialize cardView in MyViewHolder :
CardView cv = (CardView) itemView.findViewById(R.id.YOUR_CARDVIEW_ID);
for adding random colors, generate some colors and add them to a List and pass this list to the adapter.
now go to onBindViewHolder and add this code :
holder.cv.setCardBackgroundColor(YourList.get(i));
Please, help to figure out why my implementation of RecyclerView doesn't show anything.
Retrieving data asynchronously and result is successfull, but don't know how to display it correctly.
Activity
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
checkAndRequestPermissions(Manifest.permission.INTERNET);
RecyclerView recyclerView = binding.recyclerView;
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
NewsAdapter adapter = new NewsAdapter();
recyclerView.setAdapter(adapter);
RssService.runRssFeed(newsList -> {
System.out.println("SIZE ----- " + newsList.size());
adapter.setItems(newsList);
});
}
}
Adapter
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {
private final List<NewsModel> news = new ArrayList<>();
#Override
public NewsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.news_item, parent, false);
return new ViewHolder(view);
}
public void setItems(List<NewsModel> news) {
this.news.addAll(news);
notifyDataSetChanged();
}
public void clearItems() {
this.news.clear();
notifyDataSetChanged();
}
#Override
public void onBindViewHolder(#NonNull NewsAdapter.ViewHolder holder, int position) {
holder.title.setText(news.get(holder.getAdapterPosition()).getTitle());
holder.date.setText(news.get(holder.getAdapterPosition()).getDate());
holder.link.setText(news.get(holder.getAdapterPosition()).getLink());
}
#Override
public int getItemCount() {
return news.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
final TextView title, date, link;
public ViewHolder(View view) {
super(view);
title = view.findViewById(R.id.titleTxt);
date = view.findViewById(R.id.dateTxt);
link = view.findViewById(R.id.linkTxt);
}
}
}
activity_main.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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</LinearLayout>
news_item.xml
<?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:layout_gravity="top"
android:orientation="vertical">
<TextView
android:id="#+id/titleTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="16sp"
android:textStyle="normal"
android:typeface="sans" />
<TextView
android:id="#+id/linkTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#color/white"
android:textSize="12sp"
android:textStyle="normal"
android:typeface="sans" />
<TextView
android:id="#+id/dateTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginTop="10dp"
android:textColor="#color/white"
android:textSize="12sp"
android:textStyle="normal"
android:typeface="sans" />
</LinearLayout>
I think It showed correctly but you can't see because your text color is white
**issue in these lines**
holder.title.setText(news.get(holder.getAdapterPosition()).getTitle());
holder.date.setText(news.get(holder.getAdapterPosition()).getDate());
holder.link.setText(news.get(holder.getAdapterPosition()).getLink());
change these like
holder.title.setText(news.get(position).getTitle());
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() ;
}
I want to make this:
I have a RecyclerView, wherein for each item I create I want to show the time when the item was created. I want to display the time within this item.
My code:
public class VerIncidencia extends AppCompatActivity {
private FirebaseRecyclerAdapter mAdapter;
private DatabaseReference mDatabase;
String idIncidencia;
String idEmpresa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ver_incidencia);
String uid = FirebaseAuth.getInstance().getUid();
idIncidencia = getIntent().getStringExtra("INCIDENCIA_KEY");
idEmpresa = getIntent().getStringExtra("EMPRESA_KEY");
mDatabase = FirebaseDatabase.getInstance().getReference().child("incidencia").child(uid);
RecyclerView recyclerView = findViewById(R.id.list_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Query postsQuery = mDatabase;
FirebaseRecyclerOptions options = new FirebaseRecyclerOptions.Builder<Incidencia>()
.setQuery(postsQuery, Incidencia.class)
.setLifecycleOwner(this)
.build();
mAdapter = new FirebaseRecyclerAdapter<Incidencia, IncidenciaViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull IncidenciaViewHolder holder, final int position, #NonNull final Incidencia empresa) {
holder.departamento.setText(empresa.departamento);
holder.prioridad.setText(empresa.prioridad);
holder.motivo.setText(empresa.motivo);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(VerIncidencia.this, VerIncidenciaCompleta.class);
intent.putExtra("INCIDENCIA_KEY", getRef(position).getKey());
startActivity(intent);
}
});
}
#Override
public IncidenciaViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_incidencia, parent, false);
return new IncidenciaViewHolder(view);
}
};
recyclerView.setAdapter(mAdapter);
}
}
ViewHolder:
public class IncidenciaViewHolder extends RecyclerView.ViewHolder{
TextView departamento;
TextView prioridad;
TextView motivo;
public IncidenciaViewHolder(View itemView) {
super(itemView);
departamento = itemView.findViewById(R.id.departamento);
prioridad = itemView.findViewById(R.id.prioridad);
motivo = itemView.findViewById(R.id.motivo);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?><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"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/departamento"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginTop="-40dp"
android:textStyle="bold"
android:textColor="#android:color/black"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/prioridad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginTop="30dp"
android:textColor="#android:color/black"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/motivo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginTop="85dp"
android:textColor="#android:color/black"
android:textSize="25sp" />
</LinearLayout>
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"