2015-12-02 88 views
0

由於某種原因,此活動在操作欄中有後退箭頭,但當它被點擊時,它沒有任何反應,甚至沒有看到點擊。我有其他類似的活動,但工作正常。下面是一些東西的代碼刪除:ActionBar上的Android後退箭頭圖標無法正常工作

public class LanguageActivity extends ActionBarActivity { 
     private static final String TAG = "LanguageActivity"; 

     @InjectView(R.id.listView) 
     RecyclerView mRecyclerView; 

     @State 
     String selectedLang; 

     LangViewHolder selectedHolder = null; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      Icepick.restoreInstanceState(this, savedInstanceState); 
      setContentView(R.layout.recyclerview); 
      ButterKnife.inject(this); 

      Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); 
      setSupportActionBar(toolbar); 
      getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

      setTitle(R.string.ad_title_settings_lang); 

      selectedLang = PreferencesFacade.getInstance().getCurrentLang(); 

      mRecyclerView.setLayoutManager(new TrueWrapContentLinearLayoutManager(this)); 

      List<Pair<String, String>> langList = new LinkedList<>(); 

      langList.add(Constants.Languages.US); 
      langList.add(Constants.Languages.LATIN_AMERICA_SPANISH); 
      mRecyclerView.setAdapter(new RecycleViewMappedArrayAdapter(R.layout.view_language_item, new LangViewHolder(mRecyclerView), langList)); 
     } 

     @Override 
     protected void onSaveInstanceState(Bundle outState) { 
      Icepick.saveInstanceState(this, outState); 
      super.onSaveInstanceState(outState); 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      switch (item.getItemId()){ 
       case android.R.id.home: 
        onBackPressed(); 
        return true; 
      } 
      return super.onOptionsItemSelected(item); 
     } 

     @Override 
     public void finish() { 
      super.finish(); 
      PreferencesFacade.getInstance().setCurrentLang(selectedLang); 
      Log.v(TAG, "Finishing?"); 
     } 
     public class LangViewHolder extends RecycleViewMappedArrayAdapter.ViewHolder<Pair<String, String>>{ 
    ... 
     } 
    } 

清單片段:

<activity android:name=".activities.LanguageActivity" 
     android:parentActivityName=".activities.MainActivity"> 
     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value=".activities.MainActivity"/> 
    </activity> 

回答

1

這似乎是爲我工作:

Toolbar tBar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(tBar); // sets the Toolbar as the actionbar 

tBar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp)); 

tBar.setNavigationOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      onBackPressed(); // calls the System onBackPressed method 
     } 

    }); 

更新:

getDrawable(INT ID)是貶值,所以你應該改變這條線代碼:

int currentapiVersion = android.os.Build.VERSION.SDK_INT; 
if (currentapiVersion >= Build.VERSION_CODES.LOLLIPOP){ 

    tBar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp, getApplicationContext().getTheme())); 

} else { 

    tBar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp)); 

} 
+0

好吧,雖然工具欄是可見的,但它被我在它下面的recycleview所覆蓋...... – arinte

1

你爲什麼不擴大AppCompatActivity(我認爲actionbaractivity已經過時......) ?

工具條工具欄=(欄)findViewById(R.id.the_id);

setSupportActionBar(toolbar);

getSupportActionBar()。setDisplayHomeAsUpEnabled(true);

//試試這個,而不是

toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      onBackPressed(); 
     } 
    }); 
+0

I extended AppC ompatActivity,並大致做了你和@edwinj所說的,但它仍然不起作用。看起來這個drawable是禁用的。 – arinte

+0

加入這個怎麼樣? getSupportActionBar()setDisplayShowHomeEnabled(真)。 – 2015-12-06 09:35:19

相關問題