首页 > 解决方案 > 如何使用 FirebaseListAdapter 维护包含扩展 ListView 的 ScrollView 的滚动位置

问题描述

我有一个包含 ExpandableHeightListView 的 ScrollView 片段:

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;

public class ExpandableHeightListView extends ListView
{
    boolean expanded = false;

    public ExpandableHeightListView(Context context)
    {
        super(context);
    }

    public ExpandableHeightListView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ExpandableHeightListView(Context context, AttributeSet attrs,
                                    int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        if (isExpanded())
        {
            // Calculate entire height by providing a very large height hint.
            // View.MEASURED_SIZE_MASK represents the largest height possible.
            int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                    MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    }
}

此列表视图从我的 firebase 实时数据库中填充它的项目

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_customer_welcome, container, false);
        
        appointmentLV = (ExpandableHeightListView) view.findViewById(R.id.appointmentListView);
        ((ExpandableHeightListView) appointmentLV).setExpanded(true);
        final RelativeLayout noAppointments = view.findViewById(R.id.border);
        final LinearLayout appointments = view.findViewById(R.id.appointmentsListV);
        final String userId = Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid();
        final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users").child("Customers").child(userId).child("Appointments");
        ref.addListenerForSingleValueEvent(new ValueEventListener()
        {
            @Override
            public void onDataChange(@NonNull final DataSnapshot dataSnapshot)
            {
                if (dataSnapshot.exists())
                {
                   //show the appointments
                    appointments.setVisibility(View.VISIBLE);
                    noAppointments.setVisibility(View.GONE);
                    Query query = FirebaseDatabase.getInstance().getReference().child("Users").child("Customers").child(userId).child("Appointments").orderByKey().startAt("2021-01-01").endAt("2021-12-31");
                    FirebaseListOptions<Appointment> options = new FirebaseListOptions.Builder<Appointment>()
                            .setLayout(R.layout.appointment_layout)
                            .setLifecycleOwner(getActivity())
                            .setQuery(query, Appointment.class)
                            .build();
                    FirebaseListAdapter<Appointment> adapter = new FirebaseListAdapter<Appointment>(options)
                    {
                        @Override
                        protected void populateView(View v, final Appointment model, final int position)
                        {
                            // populate view
                        }

                        @Override
                        public void onError(DatabaseError error) {
                            super.onError(error);
                            Toast toast = Toast.makeText(getActivity().getApplicationContext(),error.toString(), Toast.LENGTH_SHORT);
                            toast.show();
                        }
                    };

                    appointmentLV.setAdapter(adapter);
                }else{
                   // don't show the appointments
                    appointments.setVisibility(View.GONE);
                    noAppointments.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError)
            {
                //throw databaseError.toException();
                Log.d("error", databaseError.getMessage());
            }
        });

问题是,似乎每次我离开应用程序并返回它时,滚动位置都会随着列表重新填充自身而移回页面顶部。如果我关闭应用程序并返回,我怎么能做到这一点,滚动位置保持不变?

在此处输入图像描述

标签: androidfirebasescrollviewexpandablelistview

解决方案


推荐阅读