我有NavigationDrawer
根据菜单上的选定值调用其他页面。
它是通过片段加载的。
MainActivity.Java
package medapp.app;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
{
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mActions;
@Override
protected void onCreate(Bundle savedInstanceState) {
ListView listView;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mActions = getResources().getStringArray(R.array.action_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
//mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mActions));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
public void onItemClick(AdapterView<?> adapter, View view,
int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
return super.onPrepareOptionsMenu(menu);
}
/* */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
//}
}
/* The click listener for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new ActionFragment();
Bundle args = new Bundle();
args.putInt(ActionFragment.ARG_ACTION1_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mActions[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
HERE是我的 Fragment.Java ,它为每个选项调用xml布局。
package medapp.app;
import android.app.*;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import org.json.JSONArray;
import org.json.JSONException;
public class ActionFragment extends Fragment
{
public static final String ARG_ACTION1_NUMBER = "Fragment_layout";
public JSONArray blogData;
public ActionFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
int i = getArguments().getInt(ARG_ACTION1_NUMBER);
if (i == 0)
{
rootView = inflater.inflate(R.layout.fragment_layout, container, false);
}
else if(i == 1)
{
PostActivity getBlogPostTask = new PostActivity();
getBlogPostTask.execute();
rootView = inflater.inflate(R.layout.fragment_articles, container, false);
}
else if(i == 2)
{
rootView = inflater.inflate(R.layout.fragment_news, container, false);
PostActivity getBlogPostTask = new PostActivity();
getBlogPostTask.execute();
}
else if(i == 3)
{
rootView = inflater.inflate(R.layout.fragment_signin, container, false);
}
return rootView;
}
public void updatelist() {
if (blogData == null)
{
// TODO: update list when null.
}
else
{
try
{
Log.d("TAG", blogData.toString(2));
} catch (JSONException e) {
Log.e("TAG", "Exception Caught: ", e);
}
}
}
}
现在我创建了layout_listitem.xml
我要在fragment_article.xml
内加载作为列表视图中的项目。
知道如何在layout_listitem
的listview中将fragment_article.xml
展示作为布局设计。
我在互联网上找到的所有教程都要求在主要活动中调用fragment_article.xml
。
答案 0 :(得分:0)
您可以像ListView
这样获得fragment_articles
:
PostActivity getBlogPostTask = new PostActivity();
getBlogPostTask.execute();
rootView = inflater.inflate(R.layout.fragment_articles, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.list); // assuming that the id of your listview in xml file is "list"
现在,您需要创建一个扩展BaseAdapter
或ArrayAdapter
的适配器类。在该适配器的getView方法中,您需要为layout_listitem
充气。
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.layout_listitem, null);
}
.....................
return view;
}
之后,将自定义适配器设置为列表视图的数据源。
Adapter adapter = new MyCustomAdapter(...);
listView.setAdapter(adapter);
答案 1 :(得分:0)
如果您知道fragment_article.xml为您提供了ListView.java,那么以下代码可以解决您的问题。
else if(i == 1)
{
PostActivity getBlogPostTask = new PostActivity();
getBlogPostTask.execute();
rootView = inflater.inflate(R.layout.fragment_articles, container, false);
ListView listView = (ListView) rootView;
//convert JSONArray to ArrayList<JSONObject>
List<JSONOject> data = //convert blogData
ArrayAdapter<JSONObject> adapter = new ArrayAdapter<JSONObject(getActivity(), R.layout.layout_listitem.xml, data);
listView.setAdapter(adapter);
}
没有必要在这里写更多代码。这取决于您计划如何加载数据。
您需要了解进一步增强功能的内容: