我正在使用sherlock库。我的问题是我无法在不同的按钮点击时更改片段。
这是我的代码:
public class AmecmainActivity extends FragmentActivity {
private ImageButton legislativebutton;
private ImageButton About;
private ImageButton OutageReport;
private ImageButton ViewPageReportLeft;
private ImageView ViewPageReportRight;
private ImageButton actionnetwork;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.amecmain);
actionnetwork=(ImageButton)findViewById(R.id.Actionnetwork);
actionnetwork.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bundle b = new Bundle();
b.putString("action", "actionnetwork");
Intent intent = new Intent(v.getContext(),
FragmentChangeActivity.class);
intent.putExtras(b);
startActivity(intent);
}
});
ViewPageReportLeft.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle b = new Bundle();
b.putString("html", "htmlcontent");
Intent intent = new Intent(v.getContext(),
FragmentChangeActivity.class);
intent.putExtras(b);
startActivity(intent);
}
});
}
在这里,我使用的是通用功能
public class FragmentChangeActivity extends SlidingFragmentActivity {
private Fragment mContent;
public FragmentChangeActivity() {
super();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent in = getIntent();
Bundle b = in.getExtras();
String name= b.getString("action");
//Here i am getting the values through intent so that i can give that name
// set the fragment.
// set the Above View
if (savedInstanceState != null)
mContent = getSupportFragmentManager().getFragment(b, "action");
if (mContent==name)// Here i am setting fragments i dnot know to check the
// condition here mContent is fragment and i am giving
// string how to check the condition.
mContent = new Fragment1(); //here i am setting fragment
// set the Above View
setContentView(R.layout.content_frame);
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, mContent).commit();
// set the Behind View
setBehindContentView(R.layout.menu_frame);
getSupportFragmentManager().beginTransaction()
.replace(R.id.menu_frame, new ColorMenuFragment()).commit();
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "mContent", mContent);
}
public void switchContent(Fragment fragment) // Here is switchcontent method which
// will change the fragment content
{
mContent = fragment;
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment).commit();
getSlidingMenu().showContent();
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menuIcon:
toggle();
break;
case android.R.id.home:
getFragmentManager().executePendingTransactions();
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
}