使用导航后退按钮

时间:2017-07-19 16:37:09

标签: android listview scroll navigationbar

我有Listview。它有+200项。我在所有页面中添加了后退按钮操作栏。它不舒服。如果我按设备的后退按钮Listview的滚动位置来到相同的位置但是,如果我按导航后退按钮Listview的滚动位置进入起始位置。如何使用导航后退按钮保存Listview位置?

MainActivity.java

public class MainActivity extends AppCompatActivity {
 ListView listView;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setTitle("Car");


    listView = (ListView) findViewById(R.id.Listview);

    final String[] values = new String[]{"audi", "bmw", "chevrolet", "citroen", "dacia", "ferrari", "fiat", "ford", "honda", "hyundai", "jaguar", "lamborghini", "lotus", "mazda", "mercedes", "mitsubishi", "opel", "peugeot", "renault", "toyota", "END"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, values);
    listView.setAdapter(adapter);



    bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);

    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            int id = item.getItemId();

            if (id == R.id.id_motorcycle) {

                Intent intentmoto = new Intent(MainActivity.this, motorcycle.class);
                startActivity(intentmoto);


                return true;

            }


            return false;
        }
    });

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            if (position == 0) {
                Intent myintent = new Intent(view.getContext(), audi.class);
                startActivityForResult(myintent, 0);
            }
            if (position == 1) {
                Intent myintent = new Intent(view.getContext(), bmw.class);
                startActivityForResult(myintent, 1);
            }
            if (position == 2) {
                Intent myintent = new Intent(view.getContext(), chevrolet.class);
                startActivityForResult(myintent, 2);
            }
            if (position == 3) {
                Intent myintent = new Intent(view.getContext(), citroen.class);
                startActivityForResult(myintent, 3);
            }
            if (position == 4) {
                Intent myintent = new Intent(view.getContext(), dacia.class);
                startActivityForResult(myintent, 4);

            }
            if (position == 5) {
                Intent myintent = new Intent(view.getContext(), ferrari.class);
                startActivityForResult(myintent, 5);
            }
            if (position == 6) {
                Intent myintent = new Intent(view.getContext(), fiat.class);
                startActivityForResult(myintent, 6);
            }
            if (position == 7) {
                Intent myintent = new Intent(view.getContext(), ford.class);
                startActivityForResult(myintent, 7);
            }
            if (position == 8) {
                Intent myintent = new Intent(view.getContext(), honda.class);
                startActivityForResult(myintent, 8);
            }
            if (position == 9) {
                Intent myintent = new Intent(view.getContext(), hyundai.class);
                startActivityForResult(myintent, 9);
            }
            if (position == 10) {
                Intent myintent = new Intent(view.getContext(), jaguar.class);
                startActivityForResult(myintent, 10);
            }
            if (position == 11) {
                Intent myintent = new Intent(view.getContext(), lamborgini.class);
                startActivityForResult(myintent, 11);
            }
            if (position == 12) {
                Intent myintent = new Intent(view.getContext(), lotus.class);
                startActivityForResult(myintent, 12);
            }
            if (position == 13) {
                Intent myintent = new Intent(view.getContext(), mazda.class);
                startActivityForResult(myintent, 13);
            }
            if (position == 14) {
                Intent myintent = new Intent(view.getContext(), mercedes.class);
                startActivityForResult(myintent, 14);
            }
            if (position == 15) {
                Intent myintent = new Intent(view.getContext(), mitsubishi.class);
                startActivityForResult(myintent, 15);
            }
            if (position == 16) {
                Intent myintent = new Intent(view.getContext(), opel.class);
                startActivityForResult(myintent, 16);
            }
            if (position == 17) {
                Intent myintent = new Intent(view.getContext(), peugeot.class);
                startActivityForResult(myintent, 17);
            }
            if (position == 18) {
                Intent myintent = new Intent(view.getContext(), renault.class);
                startActivityForResult(myintent, 18);
            }
            if (position == 19) {
                Intent myintent = new Intent(view.getContext(), toyota.class);
                startActivityForResult(myintent, 19);

            }



        }


    });


}






@Override
public void onStop() {
    super.onStop();











}

的AndroidManifest.xml

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".audi"
        android:parentActivityName=".MainActivity" />
    <activity
        android:name=".bmw"
        android:parentActivityName=".MainActivity" />
    <activity
        android:name=".chevrolet"
        android:parentActivityName=".MainActivity"/>
    <activity
        android:name=".citroen"
        android:parentActivityName=".MainActivity"/>
    <activity
        android:name=".dacia"
        android:parentActivityName=".MainActivity"/>
    <activity
        android:name=".ferrari"
        android:parentActivityName=".MainActivity"/>
    <activity
        android:name=".fiat"
        android:parentActivityName=".MainActivity"/>
    <activity
        android:name=".ford"
        android:parentActivityName=".MainActivity"/>
    <activity
        android:name=".honda"
        android:parentActivityName=".MainActivity"/>
    <activity
        android:name=".hyundai"
        android:parentActivityName=".MainActivity"/>
    <activity
        android:name=".jaguar"
        android:parentActivityName=".MainActivity"/>
    <activity
        android:name=".lamborgini"
        android:parentActivityName=".MainActivity"/>
    <activity
        android:name=".lotus"
        android:parentActivityName=".MainActivity"/>
    <activity
        android:name=".mazda"
        android:parentActivityName=".MainActivity"
        />
    <activity
        android:name=".mercedes"
        android:parentActivityName=".MainActivity"/>
    <activity
        android:name=".mitsubishi"
        android:parentActivityName=".MainActivity"/>
    <activity
        android:name=".opel"
        android:parentActivityName=".MainActivity"/>
    <activity
        android:name=".peugeot"
        android:parentActivityName=".MainActivity" />
    <activity android:name=".renault"
        android:parentActivityName=".MainActivity"/>
    <activity android:name=".toyota"
        android:parentActivityName=".MainActivity"/>
    <activity android:name=".motorcycle">

    </activity>
</application>

+200 pages.java

getSupportActionBar().setDisplayShowHomeEnabled(true);

1 个答案:

答案 0 :(得分:0)

见:

Parcelable state;

@Override
public void onPause() {    
    // Save ListView state @ onPause
    Log.d(TAG, "saving listview state @ onPause");
    state = listView.onSaveInstanceState();
    super.onPause();
}
...

@Override
public void onViewCreated(final View view,     Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    // Set new items
    listView.setAdapter(adapter);
    ...
    // Restore previous state (including selected item index and scroll position)
    if(state != null) {
        Log.d(TAG, "trying to restore listview state..");
        listView.onRestoreInstanceState(state);
    }
}