使用FirebaseRecyclerAdapter排序项目的位置 [英] Sorting items position using FirebaseRecyclerAdapter

查看:121
本文介绍了使用FirebaseRecyclerAdapter排序项目的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,用户可以使用一个特殊的标准,即 orderByChild

轻松地对内容进行排序。在瑞士

  .orderByChild(Tag_country)。equalTo(Physics_Switzerland);如果他们在瑞士寻找任何文件,那么



  .orderByChild(Country)。equalTo(Switzerland); 

用户可以根据发布日期,浏览次数或金额。
$ b $ pre $ .orderByChild(Views)//按最高视图排序列表
.orderByChild(Brains)//按大多数喜欢排序列表
.orderByChild(TimeStamp)//按最新帖子排序列表\\ b



但是,用户可能不会多次对内容进行排序,因为我们可能都知道

< blockquote>

您一次只能使用一个顺序方式。


所以,像
<$ c $这样的东西OrderByChild(Tag_country).equal(Physics_Switzerland)。OrderByChild(Views);



/ p>

我一直试图根据文档被查看的次数对已经指定的查询进行排序,也就是按照降序排列,瑞士从最高点开始到最低点



FirebaseRecycler如何调整

  final Query DBquery = FirebaseDatabase.getInstance()。getReference()。child(PDF)。child(Content)
.orderByChild(tag_Country)。equalTo(Chemistry_Switzerland);

FirebaseRecyclerAdapter< pdfGstrings,Pdf_BlogViewHolder> fbRecyclerAdapter;
fbRecyclerAdapter = new FirebaseRecyclerAdapter< pdfGstrings,Pdf_BlogViewHold呃>(
pdfGstrings.class,
R.layout.pdf_g_card,
Pdf_BlogViewHolder.class,
DBquery
){

PdfRecycler。 setAdapter(fbRecyclerAdapter);
fbRecyclerAdapter.notifyDataSetChanged();

视图如何被填充

 @Override 
protected void populateViewHolder(final Pdf_BlogViewHolder viewHolder,final pdfGstrings model,final int position){
final String post_key = getRef(position).getKey();
渲染(viewHolder,model,post_key);
}
};

我可以通过
$ b $访问每个Pdf Views b viewHolder.ViewCounts



查询



我该如何操纵\订购商品的位置,客户端,并基于一个整数值?这里是 viewHolder.ViewCounts



的整数值例子:将单元格与单元格顶部的325个视图对齐以编程方式显示92个视图。





儿童结构

  Pdf 
内容
-Kd5YcEuE1yKTSdpaZi7
Brains:2,
Country:Switzerland,
Tag_country:Chemistry_Switzerland,
TimeStamp:12849127519,
Views:92,

-Kd5YcoijSDujaxpaZsE
Brains:12,
Country:Switzerland,
Tag_country:Chemistry_Switzerland ,
TimeStamp14349127519
Views:352,

-Kd5YHDIUscDujaxpaZ13
Brains:1,
Country:Malaysia,
Tag_country:Mathematics_Malaysia,
TimeStamp14341221519
Views:218,
-Kd5YHDIUJAdhxaxpas93
Brains 91,
国家:阿根廷,
Tag_country:Archaeology_Argentine,
时间戳:14949127519,
Views:928,


解决方案

在我的查询中, p>

OrderByChild(Tag_country_views)



安静简单:


  • 使用增量方法将整数保留在子结构中
  • 创建另一个值,它将Views值包装成一个字符串 String.valueOf(vh.Tag +_+ vh.Country +_+ vh.ViewsCount)

  • 不要单独使用 OrderByChild ,可以考虑使用 startAt limitTo 以及


例如:用户是否会筛选联合王国最常见的数学文档?然后,所有需要做的是

$ p $ code OrderByChild(Tag_Country_Views)
.startAt(Mathematics_United Kingdoms
.limitToFirst(DocumentsCount);

(结果)



另外还有一个小技巧需要,计数应该,最好开始在 1,000,000,000 的意见,而它的增量仍然在 1 +





除以10亿,并使用模数运算符将揭示实际的查看次数

int CarrotCake = views%1000000000;


In my application, users can easily sort the content with one special criterion, that is orderByChild.

if they hunt for physics document in Switzerland

    .orderByChild("Tag_country").equalTo("Physics_Switzerland");

if they hunt for any document in Switzerland

    .orderByChild("Country").equalTo("Switzerland");

Users can sort contents based on their publish date, the number of times they have been viewed or the amount of brains\likes a single post has.

.orderByChild("Views") //  sort list by highest views
.orderByChild("Brains") //  sort list by most likes\brains 
.orderByChild("TimeStamp") //  sort list by newest post\edit

However, a user may not sort the content more than once, as we may all know

You can only use one order-by method at a time. Calling an order-by method multiple times in the same query throws an error.

So something like OrderByChild("Tag_country).equal("Physics_Switzerland").OrderByChild("Views");

will actually crash your app

I've been attempting to sort an already specified query based on the number of times a document has been viewed. That is, in a descending order, show me physics documents in Switzerland starting from the highest to the lowest views

How the FirebaseRecycler is being adjusted

 final Query DBquery = FirebaseDatabase.getInstance().getReference().child("PDF").child("Content")
                .orderByChild("tag_Country").equalTo("Chemistry_Switzerland");

        FirebaseRecyclerAdapter<pdfGstrings, Pdf_BlogViewHolder> fbRecyclerAdapter;
        fbRecyclerAdapter = new FirebaseRecyclerAdapter<pdfGstrings,  Pdf_BlogViewHolder>(
                pdfGstrings.class,
                R.layout.pdf_g_card,
                Pdf_BlogViewHolder.class,
                DBquery
        ) {

        PdfRecycler.setAdapter(fbRecyclerAdapter);
        fbRecyclerAdapter.notifyDataSetChanged();

How the view is being populated

 @Override
            protected void populateViewHolder(final Pdf_BlogViewHolder viewHolder, final pdfGstrings model, final int position) {
                final String post_key = getRef(position).getKey();
                Render(viewHolder, model, post_key);
            }
        };

I am able to access each Pdf Views count by

viewHolder.ViewCounts

The query

How may I manipulate\reorder the items positions, client-side, and based on an integer value? Here the integer value of viewHolder.ViewCounts

Example : Align the cell with 325 views on top of the cell with 92 views, programmatically.

Children Structure

Pdf
   Content
      -Kd5YcEuE1yKTSdpaZi7    
          "Brains" : 2,
          "Country" : "Switzerland",
          "Tag_country" : "Chemistry_Switzerland",
          "TimeStamp" : 12849127519,
          "Views" : 92,

      -Kd5YcoijSDujaxpaZsE    
          "Brains" : 12,
          "Country" : "Switzerland",
          "Tag_country" : "Chemistry_Switzerland",
          "TimeStamp" 14349127519
          "Views" : 352,

     -Kd5YHDIUscDujaxpaZ13   
          "Brains" : 1, 
          "Country" : "Malaysia",
          "Tag_country" : "Mathematics_Malaysia",
          "TimeStamp" 14341221519
          "Views" : 218,
     -Kd5YHDIUJAdhxaxpas93    
          "Brains" 91,
          "Country" : "Argentine",
          "Tag_country" : "Archaeology_Argentine",
          "TimeStamp": 14949127519,
          "Views" : 928,

解决方案

In my Query I was struggling concating an integer inside a string

OrderByChild("Tag_country_views")

The solution was quiet simple :

  • Keep the integer in your child structure with it's incrementing method
  • Create another value, which wrap the Views value into a String String.valueOf(vh.Tag+"_"+vh.Country+"_"+vh.ViewsCount)
  • Instead of OrderByChild alone, consider using startAt and limitTo as well

for instance: Would a user filter for the most seen mathematical document in the United Kingdoms? then, all that needs to be done is

 OrderByChild("Tag_Country_Views")
 .startAt("Mathematics_United Kingdoms)
 .limitToFirst(DocumentsCount);

(result)

Also there is one minor trick needed, the count ought to, preferably, begin at 1,000,000,000 views, while it's incrementation remains at 1+

This way, querying for String_value is feasible.

Dividing by one billion and using the modulus operator will reveal the actual view count

int CarrotCake = views % 1000000000;

这篇关于使用FirebaseRecyclerAdapter排序项目的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆