gpt4 book ai didi

java - 如何使用 Firebase 数据库和不同的 ViewHolder 根据类型(视频、文本、图像)恢复数据?

转载 作者:行者123 更新时间:2023-12-02 10:50:19 26 4
gpt4 key购买 nike

我想将数据从数据库检索到具有多个布局的回收器 View 。如何根据是图片还是视频分别检索内容?

我的意思是我不想给我的 getItemView 像 if image == 1 这样的位置来做到这一点。我想检索特定布局中不同类型的内容。

例如:像 Facebook 一样,如果有人发布视频,而另一个人发布文本,则滚动时会显示它们。

最佳答案

fragment

public class Discover extends Fragment {

private DatabaseReference mDatabaseRefAll;
private RecyclerView recyclerViewDiscover;
DiscoverRecyclerViewAdapter discoverRecyclerViewAdapter;
private ArrayList<Object> discoverItems = new ArrayList<Object>();
private ArrayList<discoverModell> objects = new ArrayList<>();

public Discover() {
// Required empty public constructor
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_discover, container, false);

recyclerViewDiscover = (RecyclerView) view.findViewById(R.id.rcDiscover);
recyclerViewDiscover.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerViewDiscover.setHasFixedSize(true);

discoverRecyclerViewAdapter = new DiscoverRecyclerViewAdapter(getContext(), discoverItems, objects);
// set adapter
recyclerViewDiscover.setAdapter(discoverRecyclerViewAdapter);
recyclerViewDiscover.setItemAnimator(new DefaultItemAnimator());


return view;

}

private void loadPost() {

// loading the posts

mDatabaseRefAll = FirebaseDatabase.getInstance().getReference("Testing");
Query queryDiscover = mDatabaseRefAll;

queryDiscover.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, String s) {

Post post = dataSnapshot.getValue(Post.class);
discoverItems.add(post);

discoverRecyclerViewAdapter.notifyDataSetChanged();

// recyclerViewDiscover.scrollToPosition( discoverItems.size() - 1);

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}


}

适配器

public class DiscoverRecyclerViewAdapter extends RecyclerView.Adapter<DiscoverRecyclerViewAdapter.DiscoverViewHolder> {

private Context ctx;
private ArrayList<Object> arrayListDiscoverContent;
private ArrayList<Object> horizontalContent;
private ArrayList<discoverModell> arraydiscoverRow;
private static final int LAYOUT_ADS = 1;
private static int LAYOUT_DISCOVERROW = 0;
private static int LAYOUT_DISCOVERCONTENT = 2;


public DiscoverRecyclerViewAdapter(Context context, ArrayList<Object> arrayListDiscoverContent, ArrayList<discoverModell> arraydiscoverRow) {

this.ctx = context;
this.arrayListDiscoverContent = arrayListDiscoverContent;
this.arraydiscoverRow = arraydiscoverRow;

}

@Override
public DiscoverViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

ctx = parent.getContext();

if (viewType == LAYOUT_ADS){

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ad_banner_view, parent, false);

return new DiscoverViewHolder(view);
}

if (viewType == LAYOUT_DISCOVERROW) {

View goToContent = LayoutInflater.from(parent.getContext()).inflate(R.layout.discover_by_content, parent, false);
return new DiscoverViewHolder(goToContent);

} else if (viewType == LAYOUT_DISCOVERCONTENT) {

View videoView = LayoutInflater.from(parent.getContext()).inflate(R.layout.pmc_video_row, parent, false);
return new DiscoverViewHolder(videoView);
}

return null;
}

@Override
public void onBindViewHolder(DiscoverViewHolder holder, int position) {
//String type = arrayListDiscoverContent.get(position).getType();
if (getItemViewType(position) == LAYOUT_ADS){

DiscoverViewHolder.adsDiscoverView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent intent = new Intent(ctx, google_ads_video.class);
ctx.startActivity(intent);
}
});

} else if (getItemViewType(position) == LAYOUT_DISCOVERROW) {

final discoverModell discoverM = (discoverModell) arraydiscoverRow.get(position);

DiscoverViewHolder.obooks.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent booksIntent = new Intent(ctx, menu_book.class);
ctx.startActivity(booksIntent);
}
});

DiscoverViewHolder.olisticles.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent listIntent = new Intent(ctx, menu_listicle.class);
ctx.startActivity(listIntent);
}
});

DiscoverViewHolder.oinfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent odatingIntent = new Intent(ctx, Buildinginfo.class);
ctx.startActivity(odatingIntent);
}
});
}

}

public static class DiscoverViewHolder extends RecyclerView.ViewHolder {

public static View adsDiscoverView, gotobannerview;
public static Context ctx;
public static TextView name_discover, autordiscover;
PublisherAdView mPublisherAdView;

public DiscoverViewHolder(View itemView) {
super(itemView);

this.ctx = ctx;
gotobannerview = itemView;

name_discover = itemView.findViewById(R.id.textView19);
autordiscover = itemView.findViewById(R.id.autorId);
}
}

public static class DiscoverRowViewHolder extends RecyclerView.ViewHolder {

public static Context ctx;
public static View adsDiscoverView;
public static ImageView obooks, olisticles, oinfo;

public DiscoverRowViewHolder(View itemView) {
super(itemView);

this.ctx = ctx;
adsDiscoverView = itemView;

obooks = (ImageView) itemView.findViewById(R.id.obooks);
olisticles = (ImageView) itemView.findViewById(R.id.olisticles);
oinfo = (ImageView) itemView.findViewById(R.id.oinfo);
}
}

@Override
public int getItemCount() {
return arrayListDiscoverContent.size();
}

@Override
public int getItemViewType(int position) {

if (position == 0){
return LAYOUT_DISCOVERROW;
} else if (position == 1){
return LAYOUT_ADS;
}

return LAYOUT_DISCOVERCONTENT;
}

关于java - 如何使用 Firebase 数据库和不同的 ViewHolder 根据类型(视频、文本、图像)恢复数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52233667/

26 4 0
文章推荐: java - 在 Spring MVC 项目中使用 Maven 重要吗?
文章推荐: java - 如何使方法参数类型ArrayList采用不同的对象类型
文章推荐: java - 线程被锁定