Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainFragment extends Fragment {
- private RecyclerView broneListView;
- private RecyclerView imagesListView;
- private List<BronePost> brone_list;
- private List<User> users_list;
- private List<Imagens> image_list;
- private Button criaAds;
- private FirebaseFirestore firebaseFirestore;
- private BroneRecyclerAdapter broneRecyclerAdapter;
- private ImageRecyclerAdapter imageRecyclerAdapter;
- private FirebaseAuth firebaseAuth;
- private DocumentSnapshot lastVisible;
- private Boolean isFirstPageFirstLoad = true;
- public MainFragment() {
- // Required empty public constructor
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- View view = inflater.inflate(R.layout.fragment_main, container, false);
- firebaseAuth = FirebaseAuth.getInstance();
- brone_list = new ArrayList<>();
- users_list = new ArrayList<>();
- broneListView = view.findViewById(R.id.post_list_view);
- broneRecyclerAdapter = new BroneRecyclerAdapter(brone_list, users_list);
- broneListView.setLayoutManager(new LinearLayoutManager(getActivity()));
- broneListView.setAdapter(broneRecyclerAdapter);
- broneListView.setHasFixedSize(true);
- broneRecyclerAdapter.notifyDataSetChanged();
- image_list = new ArrayList<>();
- imagesListView = view.findViewById(R.id.list_image_view);
- imageRecyclerAdapter = new ImageRecyclerAdapter(image_list);
- imagesListView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
- imagesListView.setAdapter(imageRecyclerAdapter);
- imagesListView.addItemDecoration(new LinePagerIndicatorDecoration());
- imagesListView.setHasFixedSize(true);
- imageRecyclerAdapter.notifyDataSetChanged();
- if (firebaseAuth.getCurrentUser() != null) {
- firebaseFirestore = FirebaseFirestore.getInstance();
- broneListView.addOnScrollListener(new RecyclerView.OnScrollListener() {
- @Override
- public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
- super.onScrolled(recyclerView, dx, dy);
- Boolean reachedBottom = !recyclerView.canScrollVertically(-1);
- if (reachedBottom) {
- loadMoreQuery();
- }
- }
- });
- Query firstQuery = firebaseFirestore.collection("Posts").orderBy("timestamp", Query.Direction.ASCENDING).limit(4);
- firstQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
- @Override
- public void onEvent(QuerySnapshot queryDocumentSnapshots, FirebaseFirestoreException e) {
- if (!queryDocumentSnapshots.isEmpty()) {
- if (isFirstPageFirstLoad) {
- lastVisible = queryDocumentSnapshots.getDocuments().get(queryDocumentSnapshots.size() - 1);
- brone_list.clear();
- users_list.clear();
- image_list.clear();
- }
- for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
- if (doc.getType() == DocumentChange.Type.ADDED) {
- final String bronePostId = doc.getDocument().getId();
- final BronePost bronePost = doc.getDocument().toObject(BronePost.class).withId(bronePostId);
- String broneUserId = doc.getDocument().getString("user_id");
- if (broneUserId != null) {
- firebaseFirestore.collection("Users").document(broneUserId).get()
- .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
- @Override
- public void onComplete(final Task<DocumentSnapshot> task) {
- if (task.isSuccessful()) {
- firebaseFirestore.collection("Posts/" + bronePostId + "/Images").addSnapshotListener(new EventListener<QuerySnapshot>() {
- @Override
- public void onEvent(QuerySnapshot queryDocumentSnapshots, FirebaseFirestoreException e) {
- if (!queryDocumentSnapshots.isEmpty()) {
- for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
- if (doc.getType() == DocumentChange.Type.ADDED) {
- User user = task.getResult().toObject(User.class);
- Imagens imagens = doc.getDocument().toObject(Imagens.class);
- if (isFirstPageFirstLoad) {
- users_list.add(user);
- brone_list.add(bronePost);
- image_list.add(imagens);
- } else {
- users_list.add(0, user);
- brone_list.add(0, bronePost);
- image_list.add(0, imagens);
- }
- }
- }
- }
- }
- });
- broneRecyclerAdapter.notifyDataSetChanged();
- imageRecyclerAdapter.notifyDataSetChanged();
- }
- }
- });
- }
- }
- }
- isFirstPageFirstLoad = false;
- }
- }
- });
- }
- criaAds = view.findViewById(R.id.btn_criar_anuncio);
- criaAds.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- onCriarAnuncio();
- }
- });
- return view;
- }
- public void onCriarAnuncio(){
- //Intent intent = new Intent(MainActivity.this, CriarAnuncioActivity.class);
- //ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.makeCustomAnimation(getApplicationContext(), R.anim.fade_in, R.anim.mover_direita);
- //ActivityCompat.startActivity(MainActivity.this, intent, activityOptionsCompat.toBundle());
- startActivity(new Intent(getContext(), ZipCodeActivity.class));
- getActivity().finish();
- }
- public void loadMoreQuery(){
- if (firebaseAuth.getCurrentUser() != null) {
- Query nextQuery = firebaseFirestore.collection("Posts")
- .orderBy("timestamp", Query.Direction.ASCENDING)
- .startAfter(lastVisible)
- .limit(4);
- nextQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
- @Override
- public void onEvent(QuerySnapshot queryDocumentSnapshots, FirebaseFirestoreException e) {
- if (!queryDocumentSnapshots.isEmpty()) {
- lastVisible = queryDocumentSnapshots.getDocuments().get(queryDocumentSnapshots.size() - 1);
- for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
- if (doc.getType() == DocumentChange.Type.ADDED) {
- final String bronePostId = doc.getDocument().getId();
- final BronePost bronePost = doc.getDocument().toObject(BronePost.class).withId(bronePostId);
- String broneUserId = doc.getDocument().getString("user_id");
- if (broneUserId != null) {
- firebaseFirestore.collection("Users").document(broneUserId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
- @Override
- public void onComplete(final Task<DocumentSnapshot> task) {
- if (task.isSuccessful()){
- firebaseFirestore.collection("Posts/" + bronePostId + "/Images").addSnapshotListener(new EventListener<QuerySnapshot>() {
- @Override
- public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
- for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
- if (doc.getType() == DocumentChange.Type.ADDED) {
- User user = task.getResult().toObject(User.class);
- Imagens imagens = doc.getDocument().toObject(Imagens.class);
- users_list.add(user);
- brone_list.add(bronePost);
- image_list.add(imagens);
- broneRecyclerAdapter.notifyDataSetChanged();
- }
- }
- }
- });
- }
- }
- });
- }
- }
- }
- }
- }
- });
- }
- }
- }
- import android.content.Context;
- import android.support.v7.widget.RecyclerView;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- import com.brone.brone.R;
- import com.brone.brone.model.BronePost;
- import com.brone.brone.model.User;
- import com.google.firebase.auth.FirebaseAuth;
- import com.google.firebase.firestore.FirebaseFirestore;
- import java.util.List;
- public class BroneRecyclerAdapter extends RecyclerView.Adapter<BroneRecyclerAdapter.ViewHolder> {
- public List<BronePost> broneList;
- public List<User> user_list;
- public Context context;
- private FirebaseFirestore firebaseFirestore;
- private FirebaseAuth firebaseAuth;
- public BroneRecyclerAdapter(List<BronePost> brone_list, List<User> user_list){
- this.broneList = brone_list;
- this.user_list = user_list;
- }
- @Override
- public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
- View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_list_item, parent, false);
- context = parent.getContext();
- firebaseFirestore = FirebaseFirestore.getInstance();
- firebaseAuth = FirebaseAuth.getInstance();
- return new ViewHolder(view);
- }
- @Override
- public void onBindViewHolder(final ViewHolder holder, final int position) {
- holder.setIsRecyclable(false);
- String[] endereco = broneList.get(position).getAddress().split("¬");
- String type = broneList.get(position).getType();
- holder.nameView.setText(type+" - "+endereco[3]);
- }
- @Override
- public int getItemCount() {
- return broneList.size();
- }
- public class ViewHolder extends RecyclerView.ViewHolder{
- private View view;
- private TextView rentView, nameView, enderecoView;
- // private ImageView broneLikeBtn;
- //private CardView cardView;
- public ViewHolder(View itemView) {
- super(itemView);
- view = itemView;
- nameView = view.findViewById(R.id.name_view);
- }
- public void setDesText(String descText){
- rentView = view.findViewById(R.id.rent_view);
- rentView.setText(descText);
- }
- }
- }
- import android.content.Context;
- import android.support.v7.widget.RecyclerView;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ImageView;
- import com.brone.brone.R;
- import com.brone.brone.model.Imagens;
- import com.bumptech.glide.Glide;
- import java.util.List;
- public class ImageRecyclerAdapter extends RecyclerView.Adapter<ImageRecyclerAdapter.ViewHolder> {
- public List<Imagens> image;
- public Context context;
- public ImageRecyclerAdapter(List<Imagens> image) {
- this.image = image;
- }
- @Override
- public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
- View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.multiple_list_itens, parent, false);
- context = parent.getContext();
- return new ViewHolder(view);
- }
- @Override
- public void onBindViewHolder(final ViewHolder holder, int position) {
- holder.setIsRecyclable(true);
- String imageUrl = image.get(position).getImage();
- holder.setBroneImage(imageUrl);
- }
- @Override
- public int getItemCount() {
- return image.size();
- }
- public class ViewHolder extends RecyclerView.ViewHolder {
- private View view;
- private ImageView imageView, broneLikeBtn;
- public ViewHolder(View itemView) {
- super(itemView);
- view = itemView;
- broneLikeBtn = view.findViewById(R.id.brone_like_btn);
- }
- public void setBroneImage(String image_url) {
- imageView = view.findViewById(R.id.image_view);
- Glide.with(context).load(image_url)
- .into(imageView);
- }
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".fragment.MainFragment">
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <android.support.v7.widget.RecyclerView
- android:id="@+id/post_list_view"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_above="@+id/btn_criar_anuncio"
- app:layout_constraintBottom_toTopOf="@id/btn_criar_anuncio" />
- <Button
- android:id="@+id/btn_criar_anuncio"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/selector_button"
- android:focusable="true"
- android:fontFamily="@font/ubuntu"
- android:gravity="center"
- android:text="Criar anúncio agora"
- android:textAllCaps="false"
- android:layout_alignParentBottom="true"
- android:textColor="@color/colorAccent"
- android:visibility="visible" />
- </RelativeLayout>
- </FrameLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:padding="20dp">
- <android.support.v7.widget.CardView
- android:id="@+id/card_image"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:padding="20dp"
- app:cardCornerRadius="2dp"
- app:cardElevation="2dp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent">
- <android.support.constraint.ConstraintLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <android.support.v7.widget.RecyclerView
- android:id="@+id/list_image_view"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <TextView
- android:id="@+id/name_view"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:fontFamily="@font/ubuntu"
- android:textStyle="bold"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/list_image_view" />
- <TextView
- android:id="@+id/rent_view"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="8dp"
- app:layout_constraintStart_toStartOf="@+id/name_view"
- app:layout_constraintTop_toBottomOf="@+id/name_view" />
- </android.support.constraint.ConstraintLayout>
- </android.support.v7.widget.CardView>
- </android.support.constraint.ConstraintLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- xmlns:app="http://schemas.android.com/apk/res-auto">
- <ImageView
- android:id="@+id/image_view"
- android:layout_width="match_parent"
- android:layout_height="240dp"
- android:scaleType="centerCrop"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.0"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.0"
- app:srcCompat="@android:color/darker_gray" />
- <ImageView
- android:id="@+id/brone_like_btn"
- android:layout_width="32dp"
- android:layout_height="32dp"
- android:layout_marginTop="8dp"
- android:layout_marginEnd="8dp"
- android:contentDescription="Amei"
- android:visibility="visible"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintTop_toTopOf="@+id/image_view"
- app:srcCompat="@mipmap/ic_like_gray" />
- </android.support.constraint.ConstraintLayout>
Add Comment
Please, Sign In to add comment