用于FirebaseAdapter的自定义适配器,可连接到HorizontalScrollView或Carousel [英] Custom adapter for FirebaseAdapter to a HorizontalScrollView or Carousel
问题描述
我有一个简单的RecyclerView
,我在其中显示firebase
的所有产品,但现在我想在Carousel
上显示它们,例如
I have a simple RecyclerView
where I show all the products from firebase
but now I'd like to show them on a Carousel
like this Library, the thing is that I have all on my ChooseProduct.java
class and I'd like to create a custom Adapter
to put the products as a Carousel
.
首先,我得到如下产品:
First I get the products as follows :
FirebaseRecyclerOptions< CardPOJO > options =
new FirebaseRecyclerOptions.Builder< CardPOJO >()
.setQuery(products_ref, CardPOJO.class)
.build();
然后将其存储在options
上,然后创建Adapter
And I store it on options
, so then I create the Adapter
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter< CardPOJO, CardHolder>(options) {
@Override
public CardHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflate the single recycler view layout(item)
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_product, parent, false);
int width = parent.getMeasuredWidth() / 2;
width -= mContext.getResources().getDimensionPixelSize(R.dimen._8sdp);
final CardHolder cardViewHolder = new CardHolder(view,width);
return cardViewHolder;
}
@Override
public void onDataChanged() {
super.onDataChanged();
tv.setVisibility(getItemCount() == 0 ? View.VISIBLE : View.GONE);
}
@Override
protected void onBindViewHolder(CardHolder holder, int position, final CardPOJO model) {
holder.state.setText(model.getState());
holder.cardName.setText(model.getName());
switch (model.getState()){
case "free":
//Img free
break;
case "not_free":
//Img not free
break;
default:
break;
}
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(model.getState().equals("free")){
//stuff
}
else{
//stuff
}
root_ref.child("PurchasedProducts").child(currentuser).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//stuff
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
});
}
};
adapter.startListening();
products_recycler.setAdapter(adapter);
我的.xml
具有这样的RecyclerView
:
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_chooseCard"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
所以我的问题是,如何更改它以放置Carousel
并像示例一样使用in.goodiebag.carouselpicker.CarouselPicker
而不是RecyclerView
?
So my question is, how do I change this to put a Carousel
and use like the example a in.goodiebag.carouselpicker.CarouselPicker
instead of a RecyclerView
?
我还可以将所有这些代码移到另一个代码以使其干净吗?
Also could I move all of this code to another one to make it clean?
当我尝试实现CourouselPicker
时,它会说:
When I try to implement the CourouselPicker
it says this :
setAdapter (android.support.v4.view.PagerAdapter) 在CarouselPicker中无法应用 到 (com.firebase.ui.database.FirebaseRecyclerAdapter)
setAdapter (android.support.v4.view.PagerAdapter) in CarouselPicker cannot be applied to (com.firebase.ui.database.FirebaseRecyclerAdapter)
到目前为止我尝试过什么?
我将RecyclerView
更改为
<in.goodiebag.carouselpicker.CarouselPicker
android:id="@+id/carousel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:background="#CCC"
app:items_visible="three" />
我初始化了它:carouselPicker = (CarouselPicker) findViewById(R.id.carousel);
然后我尝试将.setAdapter()
与FireBaseAdapter
放在一起,但是它们不兼容...
And then I tried to put the .setAdapter()
with the FireBaseAdapter
one, but they are not compatible...
carouselPicker.setAdapter(adapter);
在这种情况下很难做
如果范围太广并且很难用ViewPager
和其他东西实现,是否有可能创建一个像旋转木马一样的RecyclerView
?您将这些项目水平放置,然后在中间看到一个大于其余部分的项目.
IN CASE IT'S HARD TO DO THAT READ THIS
If that's to broad and it's too dificult to make it with ViewPager
and those stuff, could be possible to create a RecyclerView
that acts like carousel? You have the items horizontally and then the item on the middle you see it bigger than rest.
推荐答案
CarouselPicker
实际上是一个ViewPager
,这有点让人难过.无论如何,RecyclerView适配器和PagerAdapter是两个完全不同的东西,不要混在一起.从本质上讲,您必须自定义-有两种选择.
The CarouselPicker
is actually a ViewPager
which is kinda sad. In any case, the RecyclerView adapter and PagerAdapter are two completely different things are don't mix. You'll essentially have to go custom—there are two options.
我认为第一个是更简单,更好的UX:只是不要使其实时.如果您要在仅与用户进行短暂交互的对话框中显示该视图,则当轮播更新时没有动画以及RecyclerView的所有优点时,会令人困惑.看起来像这样:
The first is simpler and better UX in my opinion: just don't make it realtime. If you're going to display that view in a dialog that the user is only going to be briefly interacting with, it'll be confusing when the carousel updates without animations and all the goodness of the RecyclerView. It would look something like this:
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
List<CarouselPicker.PickerItem> items = new ArrayList<>();
for (DataSnapshot child : snapshot.getChildren()) {
CardPOJO card = child.getValue(CardPOJO.class);
// Use card
items.add(new CarouselPicker...);
}
mCarouselPicker.setAdapter(new CarouselPicker.CarouselViewAdapter(Activity.this, items, 0));
}
// ...
}
如果您真的希望它是实时的,则可以像这样使用FirebaseArray
:
If you really want it to be realtime, you can use a FirebaseArray
like so:
final ObservableSnapshotArray<CardPOJO> cards = new FirebaseArray(query, new ClassSnapshotParser<>(CardPOJO.class));
cards.addChangeEventListener(new ChangeEventListener() {
@Override
public void onDataChanged() {
List<CarouselPicker.PickerItem> items = new ArrayList<>();
for (CardPOJO card : cards) {
// Use card
items.add(new CarouselPicker...);
}
mCarouselPicker.setAdapter(new CarouselPicker.CarouselViewAdapter(Activity.this, items, 0));
}
})
希望这会有所帮助!
这篇关于用于FirebaseAdapter的自定义适配器,可连接到HorizontalScrollView或Carousel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!