请告诉我如何从MainActivity.java打开A Fragment extends ListFragment?
这是我的代码:
MainActivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
ImageView imageView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ParseUser currentUser = ParseUser.getCurrentUser();
//String currentUserId = currentUser.getObjectId().toString().trim();
if (currentUser != null) {
// do stuff with the user
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
// final View headerLayout = navigationView.inflateHeaderView(R.layout.nav_header_main);
final View header = navigationView.getHeaderView(0);
TextView text = (TextView) header.findViewById(R.id.TxtUsernametitle);
TextView Tmail = (TextView)header.findViewById(R.id.textmail);
imageView1 = (ImageView)header.findViewById(R.id.profile_image2);
String Tusern = currentUser.getUsername();
String Tmails = currentUser.getEmail();
text.setText(Tusern);
Tmail.setText(Tmails);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Profile");
String UsiD = currentUser.getObjectId();
query.whereEqualTo("userID",UsiD);
query.getFirstInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
ParseFile fileObject = (ParseFile) object.get("picture");
if (object.get("picture") != null) {
String imgpath = fileObject.getUrl();
Toast.makeText(MainActivity.this, imgpath, Toast.LENGTH_LONG).show();
Glide.with(MainActivity.this).load(imgpath).into(imageView1);
} else {
imageView1.setImageResource(R.drawable.image);
}
} else {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame,new MainFragments()).commit();
} else {
// show the signup or login screen
Intent takeUsertoLogin = new Intent(MainActivity.this, LoginActivity.class);
startActivity(takeUsertoLogin);
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
FragmentManager fm2 =getFragmentManager();
int id = item.getItemId();
switch (id){
case R.id.Ads_page:
fm2.beginTransaction().replace(R.id.content_frame,new UpdateFragment()).commit();
break;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
FragmentManager fm =getFragmentManager();
int id = item.getItemId();
if (id == R.id.nav_camara) {
fm.beginTransaction().replace(R.id.content_frame,new ImportFragments()).commit();
} else if (id == R.id.nav_gallery) {
fm.beginTransaction().replace(R.id.content_frame,new MainFragments()).commit();
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onDestroy() {
super.onDestroy();
ParseUser.logOut();
}
}
MainFragments:
public class MainFragments extends ListFragment {
protected List<ParseObject> Ads;
Button btn_LogOut;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main,container, false);
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Ads");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> Adsobjects, ParseException e) {
if(e==null){
Ads=Adsobjects;
AdsAdapter adapter =new AdsAdapter(getListView().getContext(),Ads);
setListAdapter(adapter);
}else{
}
}
});
btn_LogOut=(Button)rootView.findViewById(R.id.buttonlogout);
btn_LogOut.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ParseUser.logOut();
updateDetail();
}
});
return rootView;
}
public void updateDetail() {
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
}
}
我想将此MainFragment打开到MainActivity。
我试过这个:
FragmentManager fm = getFragmentManager(); fm.beginTransaction()。replace(R.id.content_frame,new MainFragments())。commit();
如果MainFragment扩展了Fragment但我的ListView不起作用......
我们将非常感谢您的帮助! 问候, 马尔
答案 0 :(得分:0)
ListFragment有两个版本。一个内置在框架中(android.app.ListFragment),另一个是支持库(android.support.v4.app.ListFragment)的一部分。
使用支持库版本可以定位在引入Fragments之前的Android版本。你有两个选择:
如果您想继续使用支持库版本以获得最大兼容性,您需要通过执行以下操作来更改您在Activity中使用的FragmentManager:
FragmentManager fm = getSupportFragmentManager();
或者,如果您想使用框架版本,您只需将片段中的import语句更改为:
import android.app.ListFragment;