gpt4 book ai didi

java - PreferenceFragment 与 AppCompatActivity Android

转载 作者:搜寻专家 更新时间:2023-11-01 08:37:57 24 4
gpt4 key购买 nike

我正在尝试使用选项卡创建 Preferencefragment 或 PreferenceActivity。其实我做不到,我的代码很简单:主要 Activity

public class MainActivity extends AppCompatActivity {

private ViewPagerAdapter mSectionsPagerAdapter;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSectionsPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

viewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(viewPager);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FirstFragment(), "ONE");
adapter.addFragment(new SecondFragment(), "TWO");
adapter.addFragment(new ThirdFragment(), "THREE");
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);
}
}
}

其中一个 fragment

public class FirstFragment extends Fragment{
public FirstFragment () {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);

return view;
}

}

如您所见,它是空的。现在,我需要用 preferencefragment 替换 fragment 。但我无法理解如何使用选项卡实现它们。我该怎么做?

最佳答案

不久前,这个would not have been possible ,由于 PreferenceFragments 不能很好地与支持库一起工作。使用最新版本的支持 v7 库,我们现在有 PreferenceFragmentCompat类,它将在 AppCompatActivity 中工作,也将与您的 ViewPager 和 FragmentPagerAdapter 一起工作。

由于您在 android.support.v4.app.FragmentPagerAdapter 中使用支持库 fragment ,因此这是您当前配置的唯一选项。请注意,可以将 TabLayout 与不支持的 fragment 一起使用,但是您必须使用 android.support.v13.app.FragmentPagerAdapter 类,它允许使用不支持的 fragment 。

因此,为了保持您当前的配置并使其正常工作,请让您的 PreferenceFragment 扩展 PreferenceFragmentCompat:

import android.os.Bundle; 
import android.support.v7.preference.PreferenceFragmentCompat;
import android.view.View;

public class MySettingsPrefFragment extends PreferenceFragmentCompat {

public MySettingsPrefFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.fragment_settings_pref);
}

@Override
public void onCreatePreferences(Bundle bundle, String s) {

}
}

添加一个与您的 PreferenceFragment 相对应的新选项卡:

private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new FirstFragment(), "ONE");
adapter.addFragment(new SecondFragment(), "TWO");
adapter.addFragment(new ThirdFragment(), "THREE");
adapter.addFragment(new MySettingsPrefFragment(), "SETTINGS"); //added
viewPager.setAdapter(adapter);
}

而且,应该就是这样!

关于java - PreferenceFragment 与 AppCompatActivity Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35142878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com