android - 如何使用 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());
}
});
问题是,似乎每次我离开应用程序并返回它时,滚动位置都会随着列表重新填充自身而移回页面顶部。如果我关闭应用程序并返回,我怎么能做到这一点,滚动位置保持不变?
解决方案
推荐阅读
- python - 烧瓶中python中的Jsonify字典
- javascript - REACT connect 4 游戏 - 尝试在 setState 上重置网格但导致“未定义”
- swift - LLVM 编译器是否足够聪明,可以删除不可访问的 if 语句中的代码?
- node.js - 在数据库中按 ID 查找用户 socket.io 套接字 id
- c++ - 为什么默认析构函数会改变移动语义?
- r - 并排的多个图形不会用情节内容关闭 TeX 的图形块
- python - django.db.utils.OperationalError: (1698, "Access denied for user 'root'@'localhost'")
- if-statement - 我们可以在Azure数据工厂的IF条件活动中使用FOREACH的参数吗
- python - 您将如何将重新排序的列表重新排序为以前的形式?
- scala - Pureconfig 错误:找不到 pureconfig.ConfigReader[A] 的隐式实例