将ViewHolder模式与FirebaseListAdapter一起使用

时间:2017-10-03 08:51:44

标签: android firebase

我想在我的firebase列表适配器中使用视图持有者模式。 firebase适配器看起来与具有转换器视图的适配器略有不同,因为在populateView方法中,view参数不能为null并且始终具有值,因此我正在寻找实现视图的最佳方法持有人模式。

这是我的适配器类:

  static class LimitingSpliterator<T> implements Spliterator<T> {

    private int limit;

    private final Supplier<T> generator;

    private LimitingSpliterator(Supplier<T> generator, int limit) {
        this.limit = limit;
        this.generator = generator;
    }

    static <T> LimitingSpliterator<T> of(Supplier<T> supplier, int limit) {
        return new LimitingSpliterator<>(supplier, limit);
    }

    @Override
    public boolean tryAdvance(final Consumer<? super T> consumer) {
        Objects.requireNonNull(consumer);
        if (limit > 0) {
            --limit;
            generator.get();
            consumer.accept(generator.get());
            return true;
        }
        return false;
    }

    @Override
    public void forEachRemaining(final Consumer<? super T> consumer) {
        while (limit > 0) {
            consumer.accept(generator.get());
            --limit;
        }
    }

    @Override
    public LimitingSpliterator<T> trySplit() {
        int half = limit >> 2;
        limit = limit - half;
        return new LimitingSpliterator<>(generator, half);
    }

    @Override
    public long estimateSize() {
        return limit << 2;
    }

    @Override
    public int characteristics() {
        return SIZED;
    }
}

2 个答案:

答案 0 :(得分:0)

这是View Holder

public class TaskViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView title,desc,points,remaining;
    public ImageView image;
    public ItemClickListener clickListener;
    public TaskViewHolder(View itemView) {
        super(itemView);

        title = (TextView) itemView.findViewById(R.id.task_title);
        desc = (TextView) itemView.findViewById(R.id.task_description);
        points = (TextView) itemView.findViewById(R.id.task_point);
        remaining = (TextView) itemView.findViewById(R.id.task_remaining_entries);
        image = (ImageView) itemView.findViewById(R.id.task_image);
        itemView.setOnClickListener(this);
    }


    public void setClickListener(ItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    public void onClick(View view) {

        Toast.makeText(view.getContext(), "clicked on task item", Toast.LENGTH_SHORT).show();
        clickListener.onClick(view,getAdapterPosition(),false);
    }


}

以下是我在RecyclerView

中加载数据的方法
public void loadData(){  
 database = FirebaseDatabase.getInstance();
        tasks = database.getReference("Tasks");
        adapter = new FirebaseRecyclerAdapter<TaskPOJO, TaskViewHolder>(TaskPOJO.class, R.layout.task_item, TaskViewHolder.class, tasks) {
            @Override
            protected void populateViewHolder(TaskViewHolder viewHolder, final TaskPOJO model, int position) {


                Log.wtf("TEstScript1", "populateViewHolder: "+model.getTitle() );
                viewHolder.title.setText(model.getTitle());
                viewHolder.desc.setText(model.getDescripttion()+"\n");
                viewHolder.remaining.setText(model.getRemaining()+"/"+model.getPoint());
                viewHolder.points.setText("Points "+model.getRemaining());
                Glide.with(viewHolder.title.getContext())
                        .load(model.getImage())
                        .into(viewHolder.image);


                viewHolder.setClickListener(new ItemClickListener() {
                    @Override
                    public void onClick(View view, int position, boolean isLongClick) {
                        Toast.makeText(getActivity(), "" + model.getTitle(), Toast.LENGTH_SHORT).show();

                        Intent TaskPOJODetail = new Intent(getActivity(), Main.class);
                        TaskPOJODetail.putExtra("value","detail");
                        TaskPOJODetail.putExtra("taskId",adapter.getRef(position).getKey());
                        startActivity(TaskPOJODetail);
                    }
                });

            }
        };
        recyclerViewTasks.setAdapter(adapter);
    }

这是我的task_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:elevation="8dp"
    android:clipChildren="true"
    android:clipToPadding="true"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="130dp"
        android:orientation="horizontal"
        >
        <ImageView
            android:layout_width="110dp"
            android:layout_height="130dp"
            android:id="@+id/task_image"
            android:src="@drawable/test_tast"
            android:scaleType="centerCrop"
            />
        <View
            android:layout_width="4dp"
            android:layout_height="match_parent"
            android:background="@color/colorAccent"
            android:layout_alignParentRight="true"


            />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/task_image"
                android:orientation="vertical"
                android:layout_marginTop="8dp"
                android:layout_marginLeft="8dp"
                >
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                     >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Title of Task"
                    android:textSize="18sp"
                    android:textColor="@color/colorAccent"
                    android:id="@+id/task_title"
                    />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="30/300"
                        android:textSize="18sp"
                        android:layout_marginRight="16dp"
                        android:textColor="@color/colorAccent"
                        android:layout_alignParentRight="true"

                        android:id="@+id/task_remaining_entries"
                        />


            </RelativeLayout>

                <me.biubiubiu.justifytext.library.JustifyTextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Loren Ipsum is Simply dummy text of the printing and type settin industry. Developers and designers frequently use this\n "
                    android:layout_marginRight="16dp"
                    android:maxLines="3"
                    android:id="@+id/task_description"
                    android:lineSpacingMultiplier="1.1"

                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Point 2345345"
                    android:textColor="@android:color/white"
                    android:background="@color/colorAccent"
                    android:layout_gravity="right"
                    android:paddingRight="16dp"
                    android:paddingTop="8dp"
                    android:paddingBottom="8dp"
                    android:paddingLeft="16dp"
                    android:layout_marginTop="8dp"
                    android:id="@+id/task_point"
                    />
        </LinearLayout>
    </RelativeLayout>
</android.support.v7.widget.CardView>

希望这会对你有所帮助。

答案 1 :(得分:0)

您可以在下面找到示例作为参考,

@objc func pdfPageChanged() {
    for index in 0..<doc!.pageCount{
        if let page = doc?.page(at: index){
            let annotations = page.annotations
            for annotation in annotations{
                print("Annotation Name :: \(annotation.fieldName ?? "")")
                if annotation.fieldName == "firstName"{
                    annotation.setValue("David", forAnnotationKey: .widgetValue)
                    page.removeAnnotation(annotation)
                    page.addAnnotation(annotation)
                }else if annotation.fieldName == "checkBox"{
                    annotation.buttonWidgetState = .onState
                    page.removeAnnotation(annotation)
                    page.addAnnotation(annotation)
                }
            }
        }
    }
}

并创建自定义适配器

public class RecipeHolder extends RecyclerView.ViewHolder{
    private static final String TAG = RecipeHolder.class.getSimpleName();
    public TextView recipeName;
    public ImageView recipeImage;
    public RecipeHolder(View itemView) {
        super(itemView);
        recipeName = (TextView)itemView.findViewById(R.id.recipe_name);
        recipeImage = (ImageView)itemView.findViewById(R.id.recipe_image);
    }
}

有关详情,请参阅FirebaseRecyclerView Adapter