ViewPagerでfirebaseを使用する最も良い方法は何ですか?ViewPagerでFirebaseを実装する方法は?
予想される出力
は、どのように私はそれが位置のためfirebaseを通知し、取得し、それに応じてデータを添付することをFirebaseなどでViewPagerを使用することができますか?
Person.java
public class Person {
private String name;
private String email;
private String hobby;
public Person(){
}
public Person(String name , String email , String hobby){
this.name= name;
this.email = email;
this.hobby = hobby;
}
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getHobby() {
return hobby;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ViewPager viewPager;
// private FirebaseAuth mAuth;
List<Person> person;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
person = new ArrayList<>();
// mAuth = FirebaseAuth.getInstance();
context = this;
viewPager = (ViewPager) findViewById(R.id.pagerView);
viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), person, context));
}
class MyPagerAdapter extends FragmentPagerAdapter {
String[] tabsArray;
private List<Person> personInAdapter = new ArrayList<>();
private Context context;
// int icons[] = {R.drawable.white_home, R.drawable.white_heart,
// R.drawable.white_star, R.drawable.white_heart, R.drawable.white_star};
public MyPagerAdapter(FragmentManager fm, List<Person> person, Context context) {
super(fm);
personInAdapter = person;
this.context = context;
tabsArray = getResources().getStringArray(R.array.TABS);
}
// @Override
// public CharSequence getPageTitle(int position) {
// return tabsArray[position];
// }
@Override
public Fragment getItem(int position) {
return MyFragment.getInstance(position);
}
@Override
public int getCount() {
return personInAdapter.size();
}
}
}
MyFragment.java
public class MyFragment extends Fragment {
private TextView upperText;
private TextView lowerText;
List<Person> personInAdapter = new ArrayList<>();
private Context context;
private DatabaseReference databaseReference;
public static MyFragment getInstance(int position) {
MyFragment myFragment = new MyFragment();
Bundle args = new Bundle();
args.putInt("position", position);
myFragment.setArguments(args);
return myFragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
upperText = (TextView) view.findViewById(R.id.myFragmentUpperText);
lowerText = (TextView) view.findViewById(R.id.myFragmentLowerText);
databaseReference = FirebaseUtil.getBaseRef().child("Person");
int i = getArguments().getInt("position");
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
for (DataSnapshot personSnapshot: dataSnapshot.getChildren()) {
Person person = personSnapshot.getValue(Person.class);
upperText.setText(person.getName());
lowerText.setText(person.getEmail());
Log.i("Data In Person ", person.toString());
}
// [END_EXCLUDE]
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
// [START_EXCLUDE]
}
};
databaseReference.addValueEventListener(postListener);
// Bundle bundle = getArguments();
// if (bundle != null) {
// textView.setText("The value is" +i);
// }
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
IはFirebaseRecyclerViewとfirebase試み、SAMを実装していますLinearSnapHelper
とHorizontal RecyclerView
:カスタムでeはRecyclerviewと、それは私がViewPager
に非常によく似たものにFirebase
から取得したデータを使用することができたSOおよび様々な他のブログにハード汗を流した後
FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,
R.layout.blog_row,
BlogViewHolder.class,
mDatabaseReference
) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
viewHolder.setDesc(model.getDesc());
viewHolder.setTitle(model.getTitle());
viewHolder.setImage(getApplicationContext(), model.getImageUrl());
mGoogleNow.progressiveStop();
mGoogleNow.setVisibility(View.GONE);
}
};
mBlogList.setAdapter(firebaseRecyclerAdapter);
}
public void LogoutFromFirebase(View view) {
mAuth.signOut();
}
public static class BlogViewHolder extends RecyclerView.ViewHolder {
NetworkImageView networkImageView;
ImageLoader imageLoader;
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title) {
TextView sTitle = (TextView) mView.findViewById(R.id.TitleTextView);
sTitle.setText(title);
}
public void setDesc(String desc) {
TextView sDesc = (TextView) mView.findViewById(R.id.descriptionTextView);
sDesc.setText(desc);
}
public void setImage(Context context, String image) {
networkImageView = (NetworkImageView) mView.findViewById(R.id.imageBlogPost);
imageLoader = CustomVolleyRequest.getInstance(context)
.getImageLoader();
imageLoader.get(image, ImageLoader.getImageListener(networkImageView,
R.drawable.crop_image_menu_crop, android.R.drawable
.ic_dialog_alert));
networkImageView.setImageUrl(image, imageLoader);
// Picasso.with(context).load(image).into(networkImageView);
}
}
は、あなただけのリサイクルでfirebaseリサイクルアダプタ+水平linearlayoutmanagerを使用できませんでしたか?あなたの意見は画面全体を占めることができますが、これはあなたが望むものを達成するはずです –
はい、私はそれを考慮しましたが、リサイクラーアダプター+ horizontal linearoutoutmanagerの問題は、外部ライブラリを使用せずにスワイプを一度に単一要素に制限できないことです。 – AndroidBeginner
「一度に1つの要素にスワイプを制限する」という意味を明確にすることができます –