简体   繁体   中英

Scroll position to be on top in listview

layout.xml:

<ListView
    android:id="@+id/lv"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stackFromBottom="true"

    android:transcriptMode="alwaysScroll" />

fragment code:

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());
        }
    });
}

The stackFromBottom="true" makes the scroll position to be bottom whenever I launched the application. I want stackFromBottom="true" plus the scroll position to be on top (listview position = 0 ). May i know how to do it? I tried lv.smoothscrolltoposition() but it doesnt help.

ListView上调用setSelectionAfterHeaderView()以滚动到顶部。

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