I want to make my homepage with two row with recyclerview
with data of books and the events which are display independent of each other how can i accomplish it I want to add event to the my code how can i do it?
MoviesAdapter.java
public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {
private List<Movie> moviesList;
public Context context;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public ImageView thumbnail;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
thumbnail = (ImageView) view.findViewById(R.id.book);
}
}//End of MyViewHolder class
public MoviesAdapter(List<Movie> moviesList) {
this.moviesList = moviesList;
}
//display different items in the data set
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.movie_list_row, parent, false);
return new MyViewHolder(itemView);
}
//display data at specified location
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.title.setText(moviesList.get(position).getTitle());
holder.thumbnail.setImageResource(moviesList.get(position).getImage());
}
#Override
public int getItemCount() {
return moviesList.size();
}
}
I want to make my page like this
It should be fine to use a LinearLayout with 3 RecyclerViews in it and create an Adapter for each of them. Nested RecyclerViews would only make sense if you had a lot of categories.
I done it with the three recyclerview and their adapter class here is the code of only MainActivity.java and activity.xml
MainActivity.java
public class MainActivity extends AppCompatActivity {
private List<Book> bookList = new ArrayList<>();
private List<Author> authorList = new ArrayList<>();
private List<Event> eventList = new ArrayList<>();
private RecyclerView recyclerView1;
private RecyclerView recyclerView2;
private RecyclerView recyclerView3;
private BookAdapter bAdapter;
private AuthorAdapter aAdapter;
private EventAdapter eAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView1 = findViewById(R.id.recycler_book);
bAdapter = new BookAdapter(this,bookList);
recyclerView1.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView1.setLayoutManager(mLayoutManager);
recyclerView1.setAdapter(bAdapter);
prepareBookData();
//second recycler
recyclerView2 = findViewById(R.id.recycler_author);
aAdapter = new AuthorAdapter(this,authorList);
recyclerView2.setHasFixedSize(true);
RecyclerView.LayoutManager aLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView2.setLayoutManager(aLayoutManager);
recyclerView2.setAdapter(aAdapter);
prepareAuthorData();
//third recycler
recyclerView3 = (RecyclerView) findViewById(R.id.recycler_event);
eAdapter = new EventAdapter(this,eventList);
recyclerView3.setHasFixedSize(true);
RecyclerView.LayoutManager eLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
// RecyclerView.LayoutManager eLayoutManager = new GridLayoutManager(this,1,GridLayoutManager.HORIZONTAL,false);
recyclerView3.setLayoutManager(eLayoutManager);
recyclerView3.setAdapter(eAdapter);
prepareEventData();
}
private void prepareBookData(){
int[] drawableArray = {R.drawable.youcanwin, R.drawable.halfgirl};
String[] nameArray = {"You Can Win", "Half Girlfriend"};
Book a=new Book(nameArray[0],drawableArray[0]);
bookList.add(a);
Book b=new Book(nameArray[1],drawableArray[1]);
bookList.add(b);
Book c=new Book(nameArray[0],drawableArray[0]);
bookList.add(c);
bAdapter.notifyDataSetChanged();
}
private void prepareAuthorData(){
int[] drawableArray = {R.drawable.youcanwin, R.drawable.halfgirl};
String[] nameArray = {"You Can Win", "Half Girlfriend"};
Author a=new Author(nameArray[0],drawableArray[0]);
authorList.add(a);
Author b=new Author(nameArray[1],drawableArray[1]);
authorList.add(b);
Author c=new Author(nameArray[0],drawableArray[0]);
authorList.add(c);
Author d=new Author(nameArray[1],drawableArray[1]);
authorList.add(d);
bAdapter.notifyDataSetChanged();
}
private void prepareEventData(){
String[] desArray = {"new","old"};
String[] nameArray = {"You Can Win", "Half Girlfriend"};
Event a=new Event(nameArray[0],desArray[0]);
eventList.add(a);
Event b=new Event(nameArray[0],desArray[0]);
eventList.add(b);
Event c=new Event(nameArray[0],desArray[0]);
eventList.add(c);
bAdapter.notifyDataSetChanged();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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="fill_parent"
android:layout_height="wrap_content"
tools:context="com.example.hardik.threerecycler.MainActivity"
android:background="#e6edec">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
app:cardCornerRadius="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Books"
android:textSize="20sp"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_book"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginTop="20dp" />
</android.support.v7.widget.CardView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Authors"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginTop="10dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_author"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="#+id/recycler_book"
android:layout_marginTop="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Events"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginTop="10dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_event"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="#+id/recycler_author"
android:layout_marginTop="5dp"/>
</LinearLayout>
Related
This question already has answers here:
RecyclerView onClick
(49 answers)
Closed 1 year ago.
So,basically in this as soon as I will click on the image view i should go on the next page that is firstpage .IN this for firstpage I have used recyclerview and for the introduction pge i have used gridadapter.but when I click on the imag view its not getting intented to the next page.I tried all possible sols. ut still the same.plzz help.
this is my introduction.xml code
<?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:background="#drawable/ic_launcher_background"
tools:context=".MainActivity2">
<androidx.cardview.widget.CardView
android:layout_width="199dp"
android:layout_height="199dp"
app:cardElevation="10dp"
app:cardUseCompatPadding="true"
app:contentPadding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.247"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="35dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="193dp"
android:layout_height="194dp">
<ImageView
android:id="#+id/imageView4"
android:layout_width="143dp"
android:layout_height="109dp"
tools:layout_editor_absoluteX="7dp"
tools:layout_editor_absoluteY="16dp"
tools:srcCompat="#tools:sample/avatars"
android:onClick="image"/>
<TextView
android:id="#+id/textView15"
android:layout_width="158dp"
android:layout_height="33dp"
android:layout_marginTop="4dp"
android:text="text"
android:textColor="#color/DarkBlue"
android:textSize="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/imageView4" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
this is my MainActivity2.java code
public abstract class MainActivity2 extends AppCompatActivity implements ClickedListener {
RecyclerView recycleview1;
List<String> titles;
List<Integer>images;
GridAdapter adapter;
ImageView image;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intropage);
recycleview1 = findViewById(R.id.recycleview1);
titles = new ArrayList<>();
images = new ArrayList<>();
titles.add("LATEST MOVIES");
titles.add("UPCOMING MOVIES");
titles.add("TRENDING MOVIES");
titles.add("NOW PLAYING");
titles.add("RELEASED MOVIES");
titles.add("HINDI MOVIES");
titles.add("MARATHI MOVIES");
titles.add("ENGLISH MOVIES");
titles.add("TELUGU MOVIES");
titles.add("TAMIL MOVIES");
images.add(R.drawable.latestmovie);
images.add(R.drawable.upcomingmovies);
images.add(R.drawable.trendingmovie);
images.add(R.drawable.nowplaying);
images.add(R.drawable.releasedmovie);
images.add(R.drawable.hindimovies);
images.add(R.drawable.marathimovies);
images.add(R.drawable.englishmovies);
images.add(R.drawable.telugumovies);
images.add(R.drawable.tamilmovies);
adapter = new GridAdapter(this,titles,images,this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
recycleview1.setLayoutManager(gridLayoutManager);
recycleview1.setAdapter(adapter);
}
#Override
public void onPictureClicked() {
Intent intent = new Intent(MainActivity2.this, MainActivity1.class);
startActivity(intent);
}
}
this is my GridAdapter.java code
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
List<String>titles;
List<Integer>images;
LayoutInflater inflater;
public GridAdapter(Context context,List<String>titles,List<Integer>images){
this.titles = titles;
this.images = images;
this.inflater = LayoutInflater.from(context);
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.intoduction,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.titles.setText(titles.get(position));
holder.images.setImageResource(images.get(position));
}
#Override
public int getItemCount() {
return titles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView titles;
ImageView images;
public ViewHolder(#NonNull View itemView) {
super(itemView);
titles = itemView.findViewById(R.id.textView15);
images = itemView.findViewById(R.id.imageView4);
}
}
}
this is my firstpage.xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/LightGrey"
android:layout_marginTop="5dp"
tools:context=".MainActivity1">
<ImageView
android:id="#+id/imageView3"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_margin="10dp"
tools:srcCompat="#tools:sample/avatars" />
<LinearLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Title"
android:textSize="20sp"
android:textStyle="bold">
</TextView>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="-10dp"
android:text="Description"
android:textSize="20sp">
</TextView>
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Ratings"
android:textSize="20sp">
</TextView>
</LinearLayout>
</LinearLayout>
This interface will be implemented in class where your adapter is called.
I created an interface and implemented in MainActivity2, that interface has on override() method clickedListener. This clickedListener will be called whenever an image clicked and then the intent inside clickedListener will be called and you will be moved to MainActivity1.java.
This is your working code.
public interface ClickedListener {
void onPictureClicked();
}
GridAdapter.java
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
List<String>titles;
List<Integer>images;
LayoutInflater inflater;
ClickedListener clickedListener;
public GridAdapter(Context context,List<String>titles,List<Integer>images, ClickedListener clickedListener ){
this.titles = titles;
this.images = images;
this.inflater = LayoutInflater.from(context);
this.clickedListener = clickedListener;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.intoduction,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.titles.setText(titles.get(position));
holder.images.setImageResource(images.get(position));
}
#Override
public int getItemCount() {
return titles.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView titles;
ImageView images;
public ViewHolder(#NonNull View itemView) {
super(itemView);
titles = itemView.findViewById(R.id.textView15);
images = itemView.findViewById(R.id.imageView4);
images.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickedListener.onPictureClicked();
}
});
}
}
}
MainActivity2
public class MainActivity2 extends AppCompatActivity implements ClickedListener{
RecyclerView recycleview1;
List<String> titles;
List<Integer>images;
GridAdapter adapter;
ImageView image;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intropage);
recycleview1 = findViewById(R.id.recycleview1);
titles = new ArrayList<>();
images = new ArrayList<>();
titles.add("LATEST MOVIES");
titles.add("UPCOMING MOVIES");
titles.add("TRENDING MOVIES");
titles.add("NOW PLAYING");
titles.add("RELEASED MOVIES");
titles.add("HINDI MOVIES");
titles.add("MARATHI MOVIES");
titles.add("ENGLISH MOVIES");
titles.add("TELUGU MOVIES");
titles.add("TAMIL MOVIES");
images.add(latestmovie);
images.add(R.drawable.upcomingmovies);
images.add(R.drawable.trendingmovie);
images.add(R.drawable.nowplaying);
images.add(R.drawable.releasedmovie);
images.add(R.drawable.hindimovies);
images.add(R.drawable.marathimovies);
images.add(R.drawable.englishmovies);
images.add(R.drawable.telugumovies);
images.add(R.drawable.tamilmovies);
adapter = new GridAdapter(this,titles,images,this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
recycleview1.setLayoutManager(gridLayoutManager);
recycleview1.setAdapter(adapter);
}
}
#Override
public void onPictureClicked() {
Intent intent = new Intent(getApplicationContext(), MainActivity1.class);
startActivity(intent);
}
}
MainActivity1.java
public class MainActivity1 extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter MovieAdapter;
RecyclerView.LayoutManager layoutManager;
String[] movieNameList = {"BHOOT","PANGA","PSYCHO","JUMANJI","DIL BECHARA","BAAGHI 3","THE BRIDGE CURSE","LOST GIRLS","BAD BOYS FOR LIFE",
"THAPPAD","SADAK 2","TANHAJI","BHEESHMA" };
String[] movieDescriptionList = {"Horror/Mystery","Sport/Drama","Thriller/Psychological Thriller","Comedy/Action","Romance/Drama","Action/Thriller",
"Horror/Thriller","Mystery/Drama","Action/Comedy","Drama","Drama/Thriller","Action/Historical drama","Romance/Rom-com"};
String[] movieRatings = {"3.5/5","4.5/5","3/5","4.5/5","4/5","4/5","3.5/5","2.5/5","3/5","5/5","3.5/5","4/5","3/5"};
int[] movieImages = {R.drawable.bhoot,R.drawable.panga,R.drawable.psycho,R.drawable.jumanji,R.drawable.dil_bechera,R.drawable.baaghi3,R.drawable.bridgecurse,
R.drawable.lostgirls,R.drawable.bad_boys,R.drawable.thappad,R.drawable.sadak2,R.drawable.tanhaji,R.drawable.bheeshma};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
recyclerView = findViewById(R.id.recycleview);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
MovieAdapter= new MovieAdapter(this,movieNameList,movieDescriptionList,movieRatings,movieImages);
recyclerView.setAdapter(MovieAdapter);
}
}
FirebaseRecyclerAdapter - is just showing a blank screen, I get no errors when compiling and no errors while scrolling around. I've added Adapter().startListening(); to the onStart, I've added data to firebase in the orders child. I'd be greatful for any help.
Here is my FirebaseRecyclerAdapter,
private FirebaseRecyclerAdapter<Orders, OrderViewHolder> Adapter()
{
Query query = mDatabaseReference.limitToLast(50);
final FirebaseRecyclerOptions<Orders> options =
new FirebaseRecyclerOptions.Builder<Orders>()
.setQuery(query, Orders.class)
.build();
return new FirebaseRecyclerAdapter<Orders, OrderViewHolder>(options) {
#Override
public OrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.order_single_layout, parent, false);
return new OrderViewHolder(view);
}
#Override
protected void onBindViewHolder(OrderViewHolder ViewHolder, int position, Orders orders) {
ViewHolder.setOrderID(orders.getOrder_id());
ViewHolder.setProductName(orders.getProduct_name());
ViewHolder.setStatus(orders.getStatus());
}
};
}
My Viewholder
public static class OrderViewHolder extends RecyclerView.ViewHolder
{
View mView;
private OrderViewHolder(View itemView)
{
super(itemView);
mView = itemView;
}
public void setOrderID(String orderID)
{
TextView orderIdTextView = (TextView) mView.findViewById(R.id.orderSingle_OrderIDTextView);
orderIdTextView.setText(orderID);
}
public void setStatus(String status)
{
TextView statusTextView = (TextView) mView.findViewById(R.id.orderSingle_StatusTextView);
statusTextView.setText(status);
}
public void setProductName(String productName)
{
TextView productNameTextView = (TextView) mView.findViewById(R.id.orderSingle_ProductNameTextView);
productNameTextView.setText(productName);
}
}
on Create
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orders);
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("orders");
adapter = Adapter();
mOrderList = (RecyclerView) findViewById(R.id.order_RecyclerView);
mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mOrderList.setHasFixedSize(true);
mOrderList.setLayoutManager(mLayoutManager);
mOrderList.setAdapter(adapter);
}
activityOrder.xml
<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"
tools:context="com.chat.OrdersActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/order_RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_alignParentEnd="true">
</android.support.v7.widget.RecyclerView>
The messageView for each message intended to go into the RecyclerView
RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/orderSingle_ProductNameTextView"
style="#style/textView_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:textColor="#000000"
android:text="Product"
android:textSize="18sp" />
<TextView
android:id="#+id/orderSingle_StatusTextView"
style="#style/textView_style"
android:text="Status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/orderSingle_ProductNameTextView"
android:layout_below="#+id/orderSingle_ProductNameTextView"
android:layout_marginTop="12dp"
android:textColor="#000000"/>
<TextView
android:id="#+id/orderSingle_OrderIDTextView"
style="#style/textView_style"
android:text="order_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/orderSingle_ProductNameTextView"
android:layout_below="#+id/orderSingle_StatusTextView"
android:layout_marginTop="12dp"
android:textColor="#000000" />
Got it to start working by adding .setLifecycleOwner(this) to
final FirebaseRecyclerOptions<Orders> options =
new FirebaseRecyclerOptions.Builder<Orders>()
.setQuery(query, Orders.class)
.build();
No idea why it works now but it does
Everytime we open the activity that bares the recycler adapter it fails to load on the first try. Exiting the activity and re-entering fixes the problem. Here is a gif for example. https://gyazo.com/32dc664dd427ef1129704a09861a3708
The Item i am spawning in:
<?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:card_view="http://schemas.android.com/tools"
android:id="#+id/show_chat_single_item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="70dp"
app:cardBackgroundColor="#dcdada"
app:cardCornerRadius="0dp"
app:contentPadding="3dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="2dp"
card_view:cardUseCompatPadding="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/chat_persion_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/chat_persion_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="0dp"
android:text="Name"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintLeft_toRightOf="#+id/chat_persion_image"
app:layout_constraintTop_toTopOf="#+id/chat_persion_image" />
<TextView
android:id="#+id/chat_persion_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="8dp"
android:text="Email"
app:layout_constraintLeft_toLeftOf="#+id/chat_persion_name"
app:layout_constraintTop_toBottomOf="#+id/chat_persion_name" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
My conversation layout:
<?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"
android:id="#+id/msgs_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:theme="#style/AppTheme2"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/active_chats"
android:layout_width="match_parent"
android:layout_height="510dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="57dp">
</android.support.v7.widget.RecyclerView>
<include
android:id="#+id/include2"
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
My Adapter:
public class ActiveChatConvo extends RecyclerView.Adapter<ActiveChatConvo.ViewHolder>
{
private ArrayList<User> mUsers;
private Context mContext;
public ActiveChatConvo(ArrayList<User> photos,Context dick) {
mUsers = photos;
mContext = dick;
}
private Context getContext() {
return mContext;
}
#Override
public ActiveChatConvo.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.chat_single_item, parent, false);
// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// Get the data model based on position
User user = mUsers.get(position);
// Set item views based on your views and data model
TextView name = holder.mItemName;
name.setText(user.getName());
TextView description = holder.mItemDescription;
description.setText(user.getEmail());
ImageView pic = holder.mItemImage;
Picasso.with(pic.getContext()).load(Uri.parse(user.getPic())).into(pic);
}
#Override
public int getItemCount() {
return mUsers.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView mItemImage;
private TextView mItemName;
private TextView mItemDescription;
private LinearLayout layout;
final LinearLayout.LayoutParams params;
public ViewHolder(View v) {
super(v);
mItemImage = (ImageView) v.findViewById(R.id.chat_persion_image);
mItemName = (TextView) v.findViewById(R.id.chat_persion_name);
mItemDescription = (TextView) v.findViewById(R.id.chat_persion_email);
layout = (LinearLayout) itemView.findViewById(R.id.show_chat_single_item_layout);
params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
v.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Context context = itemView.getContext();
Intent showChatIntent = new Intent(context, ChatConversationActivity.class);
//showPhotoIntent.putExtra(PHOTO_KEY, mPhoto);
context.startActivity(showChatIntent);
}
}
}
My main class
public class ConnectionsActivity extends AppCompatActivity {
private Toolbar toolbar;
private ViewPager mViewPager;
private TextView person_name,person_email;
private RecyclerView recyclerView;
private DatabaseReference mRef;
private LinearLayoutManager mLinearLayoutManager;
private ArrayList<User> users;
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connections);
//Database
mRef = FirebaseDatabase.getInstance().getReference("Users");
mRef.keepSynced(true);
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
users = new ArrayList<>();
mRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
String name = postSnapshot.child("Name").getValue(String.class);
String email = postSnapshot.child("Email").getValue(String.class);
String pic = postSnapshot.child("image").getValue(String.class);
users.add(new User(name,email,pic));
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
//Recycler View
recyclerView = (RecyclerView)findViewById(R.id.active_chats);
ActiveChatConvo adapter = new ActiveChatConvo(users,this);
recyclerView.setAdapter(adapter);
mLinearLayoutManager = new LinearLayoutManager(this);
//mLinearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(mLinearLayoutManager);
//VIEWS
toolbar = (Toolbar) findViewById(R.id.tToolbar);
if (toolbar != null)
setSupportActionBar(toolbar);
ImageView profileActivity = (ImageView) toolbar.findViewById(R.id.action_profile);
ImageView homeActivity = (ImageView) toolbar.findViewById(R.id.action_home);
profileActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent profileActivity = new Intent(ConnectionsActivity.this, ProfileActivity.class);
startActivity(profileActivity);
finish();
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
}
});
homeActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent home = new Intent(ConnectionsActivity.this, HomeActivity.class);
startActivity(home);
finish();
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
}
});
}
}
So if the recyclerView Activity is the first activity I visit then it will not create the items.
Firebase is synchronize so it doesn't block the main thread of your application, that mean that your application continue executing, you need to notify the adapter after firebase finishing his job by using adapter.notifiyDatasetChanged() after the for loop
Replace this:
recyclerView = (RecyclerView)findViewById(R.id.active_chats);
ActiveChatConvo adapter = new ActiveChatConvo(users,this);
recyclerView.setAdapter(adapter);
mLinearLayoutManager = new LinearLayoutManager(this);
//mLinearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(mLinearLayoutManager);
with this:
recyclerView = (RecyclerView)findViewById(R.id.active_chats);
ActiveChatConvo adapter = new ActiveChatConvo(users,this);
mLinearLayoutManager = new LinearLayoutManager(this);
//mLinearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(mLinearLayoutManager);
recyclerView.setAdapter(adapter);
You need to set adapter after you set LayoutManager.
In my case I managed to solve it I found a way to start FirebaseDatabase before opening the activity and added the cod
adapter.notifyDataSetChanged();
and then I started the firebase before opening it with this
FirebaseDatabase.getInstance().getReference().keepSynced(true);
You can use Boolean button to check if it is true adapter is made
I want to create a RecyclerView with a few CardViews in it. For this I start a Thread in the onCreate of my Activity. There I get the data from my server and put this in a list. Then I create the Adapter for the RecyclerView, but it doesn't work. Here is my code:
Here I create the adapter and it should fill all CardViews in:
ListAdapter adapter = new ListAdapter(posts);
RecyclerView rv = (RecyclerView)findViewById(R.id.post_list);
rv.setAdapter(adapter);
Here is my adapter class
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.PostViewHolder> {
List<Post> posts;
public ListAdapter(List<Post> posts) {
Log.d("ListAdapter", "");
this.posts = posts;
}
#Override
public int getItemCount() {
Log.d("getItemCount", "");
return posts.size();
}
#Override
public PostViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
Log.d("onCreateView", "");
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.insert_layout, viewGroup, false);
PostViewHolder pvh = new PostViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(PostViewHolder postViewHolder, int i) {
Log.d("onBindView", "");
postViewHolder.username.setText(posts.get(i).getUsername());
postViewHolder.text.setText(posts.get(i).getText());
postViewHolder.time.setText(Long.toString(posts.get(i).getTime()));
postViewHolder.postPhoto.setImageResource(posts.get(i).returnIMG());
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
Log.d("onAttached", "");
super.onAttachedToRecyclerView(recyclerView);
}
public static class PostViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView username;
TextView time;
TextView text;
ImageView postPhoto;
PostViewHolder(View itemView) {
super(itemView);
Log.d("PostViewHolder", "");
cv = (CardView) itemView.findViewById(R.id.cv);
username = (TextView) itemView.findViewById(R.id.usernameText);
time = (TextView) itemView.findViewById(R.id.timeText);
text = (TextView) itemView.findViewById(R.id.textText);
postPhoto = (ImageView) itemView.findViewById(R.id.postPhoto);
}
}
Here is my recyclerview:
<android.support.v7.widget.RecyclerView
android:id="#+id/post_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
and my cardview:
<android.support.v7.widget.CardView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/postPhoto"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textText"
android:layout_alignParentTop="true"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/usernameText"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/timeText"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
And here the 2 errors:
E/RecyclerView: No adapter attached; skipping layout E/RecyclerView:
No layout manager attached; skipping layout
But I do both, dont't I?
set the LayoutManager first.
From your code :
ListAdapter adapter = new ListAdapter(posts);
RecyclerView rv = (RecyclerView)findViewById(R.id.post_list);
rv.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(layoutManager);
rv.setAdapter(adapter);
Hope this will help you!!..
I want to make a ListView inside a RecyclerView that should display some data in a list form. I currently have a basic Adapter and RecyclerView with no ListView inside of it, so can someone tell me how to modify my code to include a listview inside the RecyclerView?
Adapter:
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.AdapterViewHolder> {
public static class AdapterViewHolder extends RecyclerView.ViewHolder {
TextView title;
TextView summary;
AdapterViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.recycler_list_title);
summary = (TextView) itemView.findViewById(R.id.recycler_list_summary);
}
}
List<Items> items;
public RVAdapter(List<Items> items){
this.items = items;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public AdapterViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_recycler, viewGroup, false);
AdapterViewHolder pvh = new AdapterViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(AdapterViewHolder adapterViewHolder, int i) {
adapterViewHolder.title.setText(item.get(i).title);
adapterViewHolder.summary.setText(item.get(i).summary);
}
#Override
public int getItemCount() {
return item.size();
}
}
Items:
public class Items {
String title;
String summary;
public Items(String title, String summary) {
this.title = title;
this.summary = summary;
}
}
Layout:
<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.RecyclerView
android:id="#+id/recycler"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_marginTop="0dp" />
</LinearLayout>
list_recycler:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="6dp"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp">
<TextView
android:id="#+id/recycler_list_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/textColor"
android:textSize="16sp"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/recycler_list_summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/textColorSub"
android:textSize="14sp"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
How I'm creating the ListView:
private List item;
private RecyclerView rv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
rv = (RecyclerView) ((Activity) MainActivity.context).findViewById(R.id.recycler);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(this));
initializeData();
initializeAdapter();
}
private void initializeData(){
item = new ArrayList<>();
item.add(new Items("Title", "Summary"));
}
private void initializeAdapter() {
RVAdapter adapter = new RVAdapter(item);
rv.setAdapter(adapter);
}
}
ListView listview = (ListView) findViewById(R.id.listview);
ListView parentListView=(ListView)listview.getParent();
you can get parent listView by this way