I am trying to reverse my recyclerview items but neither setReverseLayout nor Collection.reverse() works

huangapple go评论71阅读模式
英文:

I am trying to reverse my recyclerview items but neither setReverseLayout nor Collection.reverse() works

问题

以下是您提供的内容的翻译部分:

我有一个应用程序,尝试在网格视图中从最新添加的项目显示到较早的项目,使用GridLayoutManager从Firebase数据库获取项目,setReverseLayout可以工作,但当项目不均匀或项目总数为奇数时,它会在顶部留下一个空的空间,就像省略了一个项目,但实际上没有。我尝试过使用collections.reverse,但它实际上没有将项目从最新的项目开始排列,更像是对其进行了洗牌,并且我仍然在顶部有一个间隙问题。没有错误消息,应用程序运行正常,但它没有以期望的方式显示项目。

这是我尝试过的两种方法的问题截图:

I am trying to reverse my recyclerview items but neither setReverseLayout nor Collection.reverse() works

这是我的回收器适配器代码:

package com.example.twelve.Adapters;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.twelve.HomeActivity;
import com.example.twelve.Model.HomeProducts;
import com.example.twelve.Products_Page_Activity;
import com.example.twelve.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    List<HomeProducts> lstproducts = new ArrayList<>();
    Context ncontext;

    public RecyclerAdapter(List<HomeProducts> lstproducts, Context ncontext) {
        this.lstproducts = lstproducts;
        this.ncontext = ncontext;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(ncontext).inflate(R.layout.home_product_items, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        holder.cardview_pname.setText(lstproducts.get(position).getPname());
        holder.cardview_category.setText(lstproducts.get(position).getCategory());
        holder.cardview_price.setText(lstproducts.get(position).getPrice());
        Picasso.get().load(lstproducts.get(position).getImageurllist().get(0)).into(holder.cardview_image);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(), Products_Page_Activity.class);
                intent.putExtra("pid", lstproducts.get(position).getPid());
                intent.putExtra("prev_activity", "Home");
                view.getContext().startActivity(intent);
            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView cardview_image;
        public TextView cardview_pname, cardview_category, cardview_price;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cardview_image = itemView.findViewById(R.id.rec_imgview);
            cardview_pname = itemView.findViewById(R.id.rec_textviewname);
            cardview_category = itemView.findViewById(R.id.rec_textviewcategory);
            cardview_price = itemView.findViewById(R.id.rec_textprice);

        }
    }
}

将产品添加到适配器的代码:

//产品被添加到列表中,并传递给适配器
DatabaseReference pref = FirebaseDatabase.getInstance().getReference("HomeProducts");
pref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for(DataSnapshot dataSnapshot : snapshot.getChildren()){
            HomeProducts Hproducts = dataSnapshot.getValue(HomeProducts.class);
            listtems.add(Hproducts);
            Collections.reverse(listtems);
        }
        rcyAdapter = new RecyclerAdapter(listtems, HomeActivity.this);
        rcyAdapter.notifyDataSetChanged();
        recyclerView.setAdapter(rcyAdapter);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {

    }
});
英文:

I have an app trying to show its items from the latest item added to earlier ones in a grid view with GridLayoutManager, getting the items from firebase database, setReverseLayout works but when the items are not uniform or items total is an odd number, it leaves a vacant space for on item at the top like it omitted an item but it didnt, i tried using collections.reverse but it didnt really arrange the items from latest items, more like shuffled it and i still have that gap problem at the top. There is no error message, the app runs fine but its not displaying items the desired way.

Here is a screenshot of the problem with both methods I tried

I am trying to reverse my recyclerview items but neither setReverseLayout nor Collection.reverse() works

Here is my recycler adapter code

package com.example.twelve.Adapters;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.twelve.HomeActivity;
import com.example.twelve.Model.HomeProducts;
import com.example.twelve.Products_Page_Activity;
import com.example.twelve.R;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
public class RecyclerAdapter extends RecyclerView.Adapter&lt;RecyclerAdapter.ViewHolder&gt; {
List&lt;HomeProducts&gt; lstproducts = new ArrayList&lt;&gt;();
Context ncontext;
public RecyclerAdapter(List&lt;HomeProducts&gt; lstproducts, Context ncontext) {
this.lstproducts = lstproducts;
this.ncontext = ncontext;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(ncontext).inflate(R.layout.home_product_items, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
holder.cardview_pname.setText(lstproducts.get(position).getPname());
holder.cardview_category.setText(lstproducts.get(position).getCategory());
holder.cardview_price.setText(lstproducts.get(position).getPrice());
Picasso.get().load(lstproducts.get(position).getImageurllist().get(0)).into(holder.cardview_image);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), Products_Page_Activity.class);
intent.putExtra(&quot;pid&quot;, lstproducts.get(position).getPid());
intent.putExtra(&quot;prev_activity&quot;, &quot;Home&quot;);
view.getContext().startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return lstproducts.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView cardview_image;
public TextView cardview_pname, cardview_category, cardview_price;
public ViewHolder(@NonNull View itemView) {
super(itemView);
cardview_image = itemView.findViewById(R.id.rec_imgview);
cardview_pname = itemView.findViewById(R.id.rec_textviewname);
cardview_category = itemView.findViewById(R.id.rec_textviewcategory);
cardview_price = itemView.findViewById(R.id.rec_textprice);
}
}
}

Code to add products to the adapter

//Products are added in a list and passed to the adapter
DatabaseReference pref = FirebaseDatabase.getInstance().getReference(&quot;HomeProducts&quot;);
pref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for(DataSnapshot dataSnapshot : snapshot.getChildren()){
HomeProducts Hproducts = dataSnapshot.getValue(HomeProducts.class);
listtems.add(Hproducts);
Collections.reverse(listtems);
}
rcyAdapter = new RecyclerAdapter(listtems, HomeActivity.this);
rcyAdapter.notifyDataSetChanged();
recyclerView.setAdapter(rcyAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});

答案1

得分: 0

你应该使用 setStackFromEnd:

GridLayoutManager manager = new GridLayoutManager(this, 2);

manager.setStackFromEnd(true);

英文:

you should use setStackFromEnd :

GridLayoutManager manager = new GridLayoutManager(this, 2);

manager.setStackFromEnd(true);

答案2

得分: 0

尝试将 Collections.reverse(listtems);for 循环中移到其结束大括号后面,以确保在完整获取集合中所有项之后再进行反转。

英文:

try to get Collections.reverse(listtems); out of the for loop .. just after its end brace to complete getting all the items of the collection first and then reverse it

huangapple
  • 本文由 发表于 2020年9月19日 16:23:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63966748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定