简体   繁体   English

如何在recyclerview中首先显示最近添加的项目

[英]How to show recently added items first in recyclerview

I'm creating an app similar to the instagram app but only admin can add contents.我正在创建一个类似于 instagram 应用程序的应用程序,但只有管理员可以添加内容。 i used python django for my backend but when i add new item to my django database, it comes last on the recyclerview.我将 python django 用于我的后端,但是当我将新项目添加到我的 django 数据库时,它在 recyclerview 上排在最后。 I want it to be in a way that, when i add new item, it comes first when you open my app like the way instagram shows recent items first.我希望它的方式是,当我添加新项目时,它会在您打开我的应用程序时首先出现,就像 instagram 首先显示最近的项目一样。 This is my custom Adapter class这是我的自定义适配器 class

public class DealAdapter extends RecyclerView.Adapter<DealAdapter.DealHolder> {

Context context;
ArrayList<Deal> arrayList;


public DealAdapter(Context context,  ArrayList<Deal> arrayList) {
    this.context = context;
    this.arrayList = arrayList;
}

@Override
public DealHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View v = inflater.inflate(R.layout.deal_cardview, parent, false);
    DealHolder dealHolder = new DealHolder(v);
    return dealHolder;
}

@Override
public void onBindViewHolder(DealHolder holder, int position) {
    Deal deal = arrayList.get(position);
    holder.title.setText(deal.getTitle());
    Picasso.with(context).
            load(deal.getImage()).
            placeholder(R.drawable.loading).
            error(android.R.drawable.stat_notify_error).
            into(holder.image);

}

@Override
public int getItemCount() {
    if(arrayList != null) {
        return arrayList.size();
    }else {
        return 0;
    }
}


public class DealHolder extends RecyclerView.ViewHolder {
   TextView title;
   ImageView image;

   public DealHolder(View itemView) {
       super(itemView);
       title = (TextView) itemView.findViewById(R.id.tvTitle);
       image = (ImageView) itemView.findViewById(R.id.ivImage);
   }
 }
}

This is my main Activity class这是我的主要活动 class

public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
String TAG = "MainActivity";
ImageButton ibMore;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.rvRecyclerview);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    String url = "http://192.168.43.2/api/deals/?format=json";
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    ArrayList<Deal> userList = new JsonConverter<Deal>().
                            toArrayList(response, Deal.class);
                    DealAdapter adapter = new DealAdapter(getApplicationContext(), userList);
                    recyclerView.setAdapter(adapter);

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d(TAG, error.toString());
                    Toast.makeText(getApplicationContext(), "Something Went wrong: " + "'" + error + "'", Toast.LENGTH_LONG).show();

                }
            });

    MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);

  }
}

Okay so this is what worked for me. 好的,这对我有用。 After getting JSON and converting to ArrayList, i called it in "Collections.reverse(arrayList);" 获取JSON并转换为ArrayList之后,我在“ Collections.reverse(arrayList);”中调用了它。 before adding it to the adapter as shown below. 如下所示,将其添加到适配器中。

new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                ArrayList<Deal> userList = new JsonConverter<Deal>().
                        toArrayList(response, Deal.class);

                Collections.reverse(userList);

                DealAdapter adapter = new DealAdapter(getApplicationContext(), userList);
                recyclerView.setAdapter(adapter);

            }

Max Billionaire, 马克斯·亿万富翁

If your "userList" contain single item then do like this 如果您的“ userList”包含单个项目,则应这样做

adapter.add(0, userList.get(0));
adapter.notifyItemInserted(0);

if you have adapter instance then just refresh the adapter like this 如果您有适配器实例,则只需像这样刷新适配器

adapter.notifyDataSetChanged();

instead 代替

DealAdapter adapter = new DealAdapter(getApplicationContext(), userList);
recyclerView.setAdapter(adapter);

To display the recent added data to the top of the list do this 要将最近添加的数据显示在列表顶部,请执行以下操作

   yourArrayListOfData.add(0, newAddedData);
   adapter.notifyDataSetChanged();

Believe me, only this single line worked for me.相信我,只有这一条线对我有用。 Use this:用这个:

Collections.reverse(list);

Where list will be your list containing any type of object其中list将是您的列表,其中包含任何类型的 object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM