滚动位置在列表视图的顶部
Posted
技术标签:
【中文标题】滚动位置在列表视图的顶部【英文标题】:Scroll position to be on top in listview 【发布时间】:2017-08-17 21:09:58 【问题描述】:layout.xml:
<ListView
android:id="@+id/lv"
android:layout_
android:layout_
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll" />
片段代码:
public class MediumFragment extends Fragment
ListView lv;
private int tabNumber = 1;
private DatabaseReference mDatabaseReference;
ArrayList<MissionClass> missionList;
EasyMediumHardArrayAdapter adapter;
private static String TAG = "MediumFragment";
private int positionNum;
private String idNum;
boolean checkingId = false;
int firstCompletelyVisiblePos;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
// Set the layout that you want to display
View v = inflater.inflate(R.layout.fragment_medium, container, false);
lv = (ListView) v.findViewById(R.id.lv);
missionList = new ArrayList<MissionClass>();
lv.setSelection(0);
adapter = new EasyMediumHardArrayAdapter(getActivity(), R.layout.list_item_easy_medium_hard, missionList);
lv.setAdapter(adapter);
//initializing database reference
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
// Retrieve and display data in listview
retrieveData();
// Check number of available mission
checkUnlockMission();
// To perform onclick event handler
lvListener();
return v;
private void retrieveData()
// Retrieve the id 7 - 12 datas from Mission table
mDatabaseReference.child("Mission").orderByKey().startAt("7").endAt("12").addListenerForSingleValueEvent(new ValueEventListener()
@Override
public void onDataChange(DataSnapshot snapshot)
for (DataSnapshot child : snapshot.getChildren())
missionList.add(child.getValue(MissionClass.class));
adapter.notifyDataSetChanged();
@Override
public void onCancelled(DatabaseError databaseError)
Log.e(TAG, databaseError.getMessage());
);
每当我启动应用程序时,stackFromBottom="true" 都会使滚动位置处于底部。我希望 stackFromBottom="true" 加上滚动位置位于顶部(listview position = 0 )。我可以知道怎么做吗?我试过 lv.smoothscrolltoposition() 但它没有帮助。
【问题讨论】:
【参考方案1】:在您的ListView
上致电setSelectionAfterHeaderView()
以滚动到顶部。
【讨论】:
评论不用于扩展讨论;这个对话是moved to chat。以上是关于滚动位置在列表视图的顶部的主要内容,如果未能解决你的问题,请参考以下文章
RecyclerView - 如何平滑滚动到嵌套滚动视图内某个位置的项目顶部?