2017-07-19 101 views
-1

我有Listview。它有200個物品。我在所有頁面中添加了後退按鈕操作欄。它工作不舒適。如果我按下設備的後退按鈕Listview的滾動位置來到相同的位置,但是,如果我按導航後退按鈕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); 
+0

您有200多項活動嗎?哇!你的apk大小是多少? –

+0

它只有28 MB。 –

+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); 
    } 
} 
+0

我加了但還是不行。我認爲這不是關於bottomNavigationView。 –

+0

我想我誤解了你的問題。讓我更新我的答案 –

+0

好的。我在等你。 –