0
我想完成的是:TabLayout with Tab selected counter
當用戶單擊Tab 1時,它應該遞增計數器並將其顯示在片段上。我原本以爲,使用tabLayout.setOnTabSelectedListener
怎麼這不起作用,因爲它給了我以下錯誤; "setOnTabSelectedListener is deprecated
。我也嘗試過getId
,但是這並沒有奏效,因爲當用戶點擊標籤時我無法註冊。
請幫忙!
當用戶點擊Tanav Sharma
時,它應該增加一個顯示的計數器。
package tanav.sharma;
import android.support.v4.app.Fragment;
import android.content.DialogInterface;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
import tanav.sharma.R;
import tanav.sharma.OneFragment;
import tanav.sharma.TwoFragment;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class TanavActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.ic_action_pro,
R.drawable.ic_action_web
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tanav);
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/**
* This button is responsible for taking the user back
* to the main activity. Preventing the user from pressing
* the back key multiple times.
*/
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager)findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout)findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
private void setupTabIcons(){
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
}
private void setupViewPager(ViewPager viewPager){
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new OneFragment(), getApplicationContext().getString(R.string.tanav));
adapter.addFragment(new TwoFragment(), "TWO");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
/**
* This function is responsible for prompting the user with a dialog box,
* once the hit the back key on the app. Asking for confirmation if they,
* really want to exit the app. If user selects Yes, the app will quit.
* If the user selects cancel, the app will continue to run.
*/
@Override
public void onBackPressed() {
CharSequence exit = getApplicationContext().getString(R.string.exit);
CharSequence exit_message = getApplicationContext().getString(R.string.exit_message);
new AlertDialog.Builder(this)
.setTitle(exit)
.setMessage(exit_message)
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
TanavActivity.super.onBackPressed();
}
}).create().show();
}
}
使用'addOnTabSelectedListener()',而不是'setOnTabSelectedListener()'。此外,這可能只是一個警告,而不是一個錯誤。 –
你也許可以添加一個普通的'onClickListener'就是你想要的... –
@ cricket_007請看最新的代碼 –