简体   繁体   中英

How to make the recyclerview always show the top part of the view?

I have a recyclerview that gets populated with items and it has a nice animation when new items are inserted. But after certain amount of list the view gets pushed down and i only see the old items. Currently i have to scroll up to see the added items each time. Lets say i have 10 items in my recyclerview and the screen is full, when i add new item...i see the same 10 items and have to scroll up to see the new item. How do i always get the top view of the list?

@NonNull
    @Override
    public DashboardHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_item,parent,false);
        return new DashboardHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull DashboardHolder holder, int position) {
        Expense currentExpense = getItem(position);
        holder.title.setText(currentExpense.getTitle());
        holder.description.setText(currentExpense.getDescription());
        holder.amount.setText(String.valueOf(currentExpense.getAmount()));
    }

    class DashboardHolder extends RecyclerView.ViewHolder {
        private TextView title;
        private TextView description;
        private TextView amount;

        public DashboardHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.text_expense_title);
            description = itemView.findViewById(R.id.text_expense_description);
            amount = itemView.findViewById(R.id.text_expense_amount);
        }
    }```


  [1]: https://i.stack.imgur.com/093f8.jpg
        LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
        layoutManager.setReverseLayout(true);
        recyclerView.setLayoutManager(layoutManager);

Used to reverse item traversal and layout order.

maybe you want to test: layoutManager.setStackFromEnd(true); the list fills its content starting from bottom of the view.

or maybe you need to use recyclerView.scrollToPosition(position);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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