-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#5 - SO example
- Loading branch information
Showing
5 changed files
with
344 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
app/src/main/java/com/eusecom/attendance/fragment/LiveFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
package com.eusecom.attendance.fragment; | ||
|
||
import android.content.Context; | ||
import android.net.Uri; | ||
import android.os.Bundle; | ||
import android.support.annotation.Nullable; | ||
import android.support.v4.app.Fragment; | ||
import android.support.v7.widget.LinearLayoutManager; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.util.Log; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.ImageView; | ||
import android.widget.TextView; | ||
|
||
import com.eusecom.attendance.models.Poll; | ||
import com.firebase.ui.database.FirebaseRecyclerAdapter; | ||
import com.google.firebase.database.DataSnapshot; | ||
import com.google.firebase.database.DatabaseError; | ||
import com.google.firebase.database.DatabaseReference; | ||
import com.google.firebase.database.FirebaseDatabase; | ||
import com.eusecom.attendance.R; | ||
import com.google.firebase.database.ValueEventListener; | ||
|
||
public class LiveFragment extends Fragment { | ||
|
||
// TODO: Rename parameter arguments, choose names that match | ||
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER | ||
private static final String ARG_PARAM1 = "param1"; | ||
private static final String ARG_PARAM2 = "param2"; | ||
|
||
private RecyclerView mRecyclerview; | ||
private DatabaseReference mBaseRef; | ||
private DatabaseReference mPollsRef; | ||
private LinearLayoutManager mLayoutManager; | ||
|
||
private FirebaseRecyclerAdapter <Poll, PollHolder> mFireAdapter; | ||
|
||
|
||
// TODO: Rename and change types of parameters | ||
private String mParam1; | ||
private String mParam2; | ||
|
||
private OnFragmentInteractionListener mListener; | ||
|
||
public LiveFragment() {} | ||
|
||
public static LiveFragment newInstance(String param1, String param2) { | ||
LiveFragment fragment = new LiveFragment(); | ||
Bundle args = new Bundle(); | ||
args.putString(ARG_PARAM1, param1); | ||
args.putString(ARG_PARAM2, param2); | ||
fragment.setArguments(args); | ||
return fragment; | ||
} | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
mBaseRef = FirebaseDatabase.getInstance().getReference(); | ||
mPollsRef = mBaseRef.child("absences"); | ||
|
||
if (getArguments() != null) { | ||
mParam1 = getArguments().getString(ARG_PARAM1); | ||
mParam2 = getArguments().getString(ARG_PARAM2); | ||
|
||
} | ||
} | ||
|
||
@Override | ||
public View onCreateView (LayoutInflater inflater, ViewGroup container, | ||
Bundle savedInstanceState) { | ||
super.onCreateView(inflater, container, savedInstanceState); | ||
|
||
// Inflate the layout for this fragment | ||
final View v = inflater.inflate(R.layout.fragment_new, container, false); | ||
Log.v("TAG", "ON CREATE CALLED FROM NEW"); | ||
|
||
mLayoutManager = new LinearLayoutManager(getActivity()); | ||
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); | ||
|
||
mRecyclerview = (RecyclerView)v.findViewById(R.id.new_RecyclerView); | ||
|
||
if (mRecyclerview != null){ | ||
mRecyclerview.setHasFixedSize(true); | ||
} | ||
|
||
if (mRecyclerview == null){ | ||
Log.v("TAG", "RECYCLERVIEW NULL"); | ||
} else if (mLayoutManager == null){ | ||
Log.v("TAG", "LAYOUTMANAGER NULL"); | ||
} else if (mFireAdapter == null) { | ||
Log.v("TAG", "mFIREADAPTER NULL"); | ||
} | ||
return v; | ||
|
||
} | ||
|
||
|
||
// TODO: Rename method, update argument and hook method into UI event | ||
public void onButtonPressed(Uri uri) { | ||
if (mListener != null) { | ||
mListener.onFragmentInteraction(uri); | ||
} | ||
} | ||
|
||
@Override | ||
public void onStart() { | ||
super.onStart(); | ||
|
||
|
||
} | ||
|
||
@Override | ||
public void onStop() { | ||
super.onStop(); | ||
if (mFireAdapter != null){ | ||
mFireAdapter.cleanup(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onActivityCreated(@Nullable Bundle savedInstanceState) { | ||
super.onActivityCreated(savedInstanceState); | ||
|
||
|
||
mRecyclerview.setLayoutManager(mLayoutManager); | ||
mFireAdapter = new FirebaseRecyclerAdapter<Poll, PollHolder>(Poll.class, R.layout.latest_item, PollHolder.class, mPollsRef) { | ||
@Override | ||
protected void populateViewHolder(PollHolder viewHolder, Poll model, int position) { | ||
viewHolder.mPollQuestion.setText(model.getQuestion()); | ||
//Picasso.with(getActivity().getApplicationContext()) | ||
// .load(model.getImage_URL()) | ||
// .fit() | ||
// .into(viewHolder.mPollImage); | ||
Log.v("QUESTION", model.getQuestion()); | ||
//Log.v("IMAGE", model.getImage_URL()); | ||
} | ||
}; | ||
mRecyclerview.setAdapter(mFireAdapter); | ||
|
||
mPollsRef.addListenerForSingleValueEvent(new ValueEventListener() { | ||
@Override | ||
public void onDataChange(DataSnapshot dataSnapshot) { | ||
for (DataSnapshot x : dataSnapshot.getChildren()){ | ||
mFireAdapter.notifyItemInserted(0); | ||
} | ||
} | ||
|
||
@Override | ||
public void onCancelled(DatabaseError databaseError) { | ||
|
||
} | ||
}); | ||
|
||
|
||
|
||
|
||
|
||
} | ||
|
||
@Override | ||
public void onAttach(Context context) { | ||
super.onAttach(context); | ||
if (context instanceof OnFragmentInteractionListener) { | ||
mListener = (OnFragmentInteractionListener) context; | ||
} else { | ||
throw new RuntimeException(context.toString() | ||
+ " must implement OnFragmentInteractionListener"); | ||
} | ||
} | ||
|
||
@Override | ||
public void onDetach() { | ||
super.onDetach(); | ||
mListener = null; | ||
} | ||
|
||
public static class PollHolder extends RecyclerView.ViewHolder { | ||
|
||
TextView mPollQuestion; | ||
ImageView mPollImage; | ||
|
||
|
||
public PollHolder(View itemView) { | ||
super(itemView); | ||
|
||
mPollQuestion = (TextView) itemView.findViewById(R.id.latest_item_question); | ||
mPollImage = (ImageView) itemView.findViewById(R.id.pollThumbNailImage); | ||
|
||
} | ||
} | ||
|
||
/** | ||
* This interface must be implemented by activities that contain this | ||
* fragment to allow an interaction in this fragment to be communicated | ||
* to the activity and potentially other fragments contained in that | ||
* activity. | ||
* <p> | ||
* See the Android Training lesson <a href= | ||
* "http://developer.android.com/training/basics/fragments/communicating.html" | ||
* >Communicating with Other Fragments</a> for more information. | ||
*/ | ||
public interface OnFragmentInteractionListener { | ||
// TODO: Update argument type and name | ||
void onFragmentInteraction(Uri uri); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.eusecom.attendance.models; | ||
|
||
import com.google.firebase.database.Exclude; | ||
import com.google.firebase.database.IgnoreExtraProperties; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
// [START Poll_class] | ||
@IgnoreExtraProperties | ||
public class Poll { | ||
|
||
public String idm; | ||
public String iname; | ||
|
||
|
||
public Poll() { | ||
// Default constructor required for calls to DataSnapshot.getValue(interrupt.class) | ||
} | ||
|
||
public Poll(String idm, String iname) { | ||
this.idm = idm; | ||
this.iname = iname; | ||
|
||
} | ||
|
||
// [START Poll_to_map] | ||
@Exclude | ||
public Map<String, Object> toMap() { | ||
HashMap<String, Object> result = new HashMap<>(); | ||
result.put("idm", idm); | ||
result.put("iname", iname); | ||
|
||
return result; | ||
} | ||
// [END Poll_to_map] | ||
|
||
public String getQuestion() { | ||
|
||
return this.iname; | ||
|
||
} | ||
|
||
} | ||
// [END Poll_class] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<FrameLayout 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=".DatabaseActivity"> | ||
|
||
<android.support.v7.widget.RecyclerView | ||
android:id="@+id/new_RecyclerView" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:layout_alignParentTop="true" | ||
android:clipToPadding="false" | ||
android:padding="5dp" | ||
android:scrollbars="vertical" | ||
tools:listitem="@layout/item_post" /> | ||
|
||
</FrameLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<android.support.v7.widget.CardView 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:layout_margin="5dp"> | ||
|
||
<RelativeLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:padding="10dp"> | ||
|
||
<include | ||
android:id="@+id/post_author_layout" | ||
layout="@layout/include_post_author" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_alignParentLeft="true" /> | ||
|
||
<LinearLayout | ||
android:id="@+id/star_layout" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_alignBottom="@+id/post_author_layout" | ||
android:layout_alignParentRight="true" | ||
android:layout_alignTop="@+id/post_author_layout" | ||
android:gravity="center_vertical" | ||
android:orientation="horizontal"> | ||
|
||
<ImageView | ||
android:id="@+id/pollThumbNailImage" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginLeft="10dp" | ||
android:layout_marginRight="5dp" | ||
android:background="?attr/selectableItemBackground" | ||
android:src="@drawable/ic_toggle_star_outline_24" /> | ||
|
||
<TextView | ||
android:id="@+id/latest_item_question" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:gravity="center" | ||
tools:text="7" /> | ||
|
||
</LinearLayout> | ||
|
||
<include layout="@layout/include_post_text" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_alignParentLeft="true" | ||
android:layout_below="@+id/post_author_layout" | ||
android:layout_marginLeft="5dp" | ||
android:layout_marginTop="10dp" /> | ||
|
||
</RelativeLayout> | ||
|
||
</android.support.v7.widget.CardView> |