Community.java
3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.example.user.firebaseauthdemo;
import android.content.Context;
import android.content.Intent;
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.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
/**
* Created by HYB on 2017. 10. 11..
*/
public class Community extends Fragment
{
private RecyclerView mBlogList;
private RecyclerView.LayoutManager layoutManager;
private DatabaseReference mDatabase;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
{
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.community, container, false);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
mBlogList = (RecyclerView) view.findViewById(R.id.blog_list);
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
@Override
public void onStart()
{
super.onStart();
FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class, R.layout.blog_row, BlogViewHolder.class, mDatabase)
{
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position)
{
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDesc());
viewHolder.setImage(getActivity().getApplicationContext(), model.getImage());
}
};
mBlogList.setAdapter(firebaseRecyclerAdapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder
{
View mView;
public BlogViewHolder(View itemView)
{
super(itemView);
mView = itemView;
}
public void setTitle(String title)
{
TextView post_title = (TextView) mView.findViewById(R.id.post_title);
if (title.length() > 30)
{
post_title.setText(title.substring(0, 30));
}
else
{
post_title.setText(title);
}
}
public void setDesc(String desc)
{
TextView post_desc = (TextView) mView.findViewById(R.id.post_desc);
if (desc.length() > 50)
{
post_desc.setText(desc.substring(0, 50));
}
else
{
post_desc.setText(desc);
}
}
public void setImage(Context ctx, String image)
{
ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
Picasso.with(ctx).load(image).into(post_image);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.community_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == R.id.action_add)
{
Intent intent = new Intent(getActivity(), PostActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
}