我正忙着制作我的第一个Android应用程序,但现在我真的需要一些帮助。
概念: 我有两个活动,一个主屏幕处理程序和一个子屏幕处理程序活动。主屏幕处理程序活动处理具有多个项目的列表视图。这些项目中的每一项都有一些链接到它的片段。每当用户点击列表视图中的项目时,属于所选项目的特定片段将被放入第二个活动的意图中,并且启动子屏幕处理程序。子屏幕处理程序接收放入intent中的特定片段。这些片段将通过使用viewpager显示给用户。
代码(到目前为止):
public class MainScreenHandler extends FragmentActivity {
private ArrayList<SubScreenFragments> subScreenFragmentList = new ArrayList<SubScreenFragments>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen_handler_layout);
subScreenFragmentList.add(new SubScreenFragments(getString(R.string.listView_downstops),new FragmentDownstopsOverview(), new FragmentDownstopsEffects(),
new FragmentDownstopsMeasure(), new FragmentDownstopsAdjust()));
String[] listViewNameArray = new String[subScreenFragmentList.size()];
for (int i = 0; i < listViewNameArray.length; i++) {
listViewNameArray[i] = subScreenFragmentList.get(i).getListViewName();
}
ListView listview = (ListView) findViewById(R.id.listView);
listview.setClickable(true);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.listview_row_layout, listViewNameArray);
listview.setAdapter(listAdapter);
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
FragmentManager fm = getSupportFragmentManager();
FragmentMainScreenDialog fragmentMainScreenDialog = new FragmentMainScreenDialog();
//Put string array, which contains the heading names from the fragments from the subScreenFragmentList, inside the bundle.
fragmentMainScreenDialog.show(fm,"fragmentMainScreenDialog");
return true;
}
});
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent myIntent = new Intent(getBaseContext(), SubScreenHandler.class);
Bundle myBundle = new Bundle();
myBundle.putSerializable("fragmentList", subScreenFragmentList.get(i).getFragments());
myIntent.putExtras(myBundle);
startActivity(myIntent);
}
});
}
}
class SubScreenFragments {
String listViewName;
Fragment fragmentTab1, fragmentTab2, fragmentTab3, fragmentTab4;
public SubScreenFragments(String listViewName, Fragment fragmentTab1, Fragment fragmentTab2, Fragment fragmentTab3, Fragment fragmentTab4) {
this.listViewName = listViewName;
this.fragmentTab1 = fragmentTab1;
this.fragmentTab2 = fragmentTab2;
this.fragmentTab3 = fragmentTab3;
this.fragmentTab4 = fragmentTab4;
}
public String getListViewName() {
return listViewName;
}
public ArrayList<Fragment> getFragments() {
ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
if (fragmentTab1 != null)
fragmentList.add(fragmentTab1);
if (fragmentTab2 != null)
fragmentList.add(fragmentTab2);
if (fragmentTab3 != null)
fragmentList.add(fragmentTab3);
if (fragmentTab4 != null)
fragmentList.add(fragmentTab4);
return fragmentList;
}
}
我的第二个活动:
public class SubScreenHandler extends FragmentActivity{
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sub_screen_handler_layout);
ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
fragmentList.addAll((ArrayList<Fragment>) getIntent().getSerializableExtra("fragmentList"));
for (Object item : fragmentList) {
if (!(item instanceof Fragment)) {
throw new ClassCastException("List contained non-Fragment objects: " + item.getClass().getCanonicalName());
}
}
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new myPagerAdapter(getSupportFragmentManager(), fragmentList));
viewPager.setOffscreenPageLimit(1); // Make sure the off screen page limit is set to 1
UnderlinePageIndicator underlinePageIndicator = (UnderlinePageIndicator) findViewById(R.id.indicator);
underlinePageIndicator.setViewPager(viewPager);
doFirstRun();
}
private void doFirstRun(){
final SharedPreferences sharedPreferences=getSharedPreferences("com.example.offroad_set_up_book", Context.MODE_PRIVATE);
if(!sharedPreferences.getBoolean("isTutorialChecked", false)){
View view = View.inflate(getBaseContext(), R.layout.checkbox_alert_dialog,null);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
checkBox.setText(getString(R.string.alertDialog_checkbox_text));
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checkBoxState) {
SharedPreferences.Editor editor=sharedPreferences.edit();
if(checkBoxState){
editor.putBoolean("isTutorialChecked", true);
editor.commit();
}
else{
editor.putBoolean("isTutorialChecked", false);
editor.commit();
}
}
});
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.alertDialog_tutorial));
builder.setCancelable(false);
builder.setView(view);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
}
class myPagerAdapter extends FragmentStatePagerAdapter {
ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
public myPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
super(fm);
fragmentList.addAll(fragments);
}
@Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
@Override
public int getCount() {
return fragmentList.size();
}
}
问题: 在主屏幕处理程序(用户看到具有不同可选项的列表视图)我现在正在实现一个长项单击侦听器。当调用此侦听器时,将打开一个片段对话框,其中包含另一个列表视图。此列表视图应包含所选项目中每个片段的标题。
所以我想当我在每个片段中指定字符串并在片段中创建一个getter时,我就能够提取字符串。这确实是“工作”。以下方式:
FragmentDownstopsOverview fragmentDownstopsOverview = new FragmentDownstopsOverview();
String string=fragmentDownstopsOverview.getFragmentTitle();
所以我想我可以在SubScreenFragments类中轻松实现它。这就是我尝试它的方式(注意我只是试着让它在SubScreenFragments对象的一个片段上工作,看看它是否有效:
class SubScreenFragments {
String listViewName;
Fragment fragmentTab1, fragmentTab2, fragmentTab3, fragmentTab4;
public SubScreenFragments(String listViewName, Fragment fragmentTab1, Fragment fragmentTab2, Fragment fragmentTab3, Fragment fragmentTab4) {
this.listViewName = listViewName;
this.fragmentTab1 = fragmentTab1;
this.fragmentTab2 = fragmentTab2;
this.fragmentTab3 = fragmentTab3;
this.fragmentTab4 = fragmentTab4;
}
// my other code
public String getTitle(){
return fragmentTab1.getFragmentTitle();
}
}
当然这没有用,因为fragmentTab1是一个Fragment对象,而不是一个FragmentDownstopsOverview对象,它确实包含我想要访问的特定getter(getFragmentTitle)(如果我正确解释它......仍然是新的haha )。
是否有人知道如何从传递给SubScreenFragments类的片段(如FragmentDownstopsOverview)访问getter。所以它看起来像这样:
public class MainScreenHandler extends FragmentActivity{
public void onCreate(....){
//code
SubScreenFragments subScreenFragments = new SubScreenFragments("Downstops", new FragmentDownstopsOverview, null,null,null);
String string = SubScreenFragments.getTitle();
//code
}
}
class SubScreenFragments {
String listViewName;
Fragment fragmentTab1, fragmentTab2, fragmentTab3, fragmentTab4;
public SubScreenFragments(String listViewName, Fragment fragmentTab1, Fragment fragmentTab2, Fragment fragmentTab3, Fragment fragmentTab4) {
this.listViewName = listViewName;
this.fragmentTab1 = fragmentTab1;
this.fragmentTab2 = fragmentTab2;
this.fragmentTab3 = fragmentTab3;
this.fragmentTab4 = fragmentTab4;
}
// my other code
public String getTitle(){
get the title from the fragment that is put inside 'fragmentTab1' (so in this case, get
the title (by a getter inside the fragment) from the fragment FragmentDownstopsOverview)
and return it.
}
}
我希望我能清楚地知道自己想要达到的目标,否则我会尝试更好地解释我的问题!
来自我的问候,一个想要学习android的19岁(以及许多其他不同的东西:))
答案 0 :(得分:0)
一种方法是实现自己的片段,“公共类MyFragment扩展Fragment ......” 然后实现一个setTitle和getTitle方法,您可以从该片段的任何实例调用该方法。
这是Fragments上的一篇好文章。