我抽屉导航存在一些问题。我尝试了很多,并搜索了很多,但没有找到解决方案。 我正在使用抽屉导航。你必须用碎片填充它的框架布局。 这工作正常,所以例如: 我在抽屉导航中选择了一些东西,它的框架布局通过按钮和文字视图加载了片段(在我的应用程序中的startdisplay)。 现在我点击其中一个片段按钮,它应该在片段java类(startdisplay.java)中调用actionlistener,但是却没有找到这些动作侦听器(android studio也告诉我这些从未使用过)。我发现应用程序正在查看抽屉导航类(我的应用程序中的mainmenuactivity)。抽屉导航,它的碎片按钮没有找到合适的动作侦听器
我的问题是,如果有一种方式,我的应用程序直接在片段Java类,因为我不能把我所有的代码放在一个类(mainmenuactivity)。当然,这不会是我必须放在那里的唯一片段。
这里是代码: 的Startdisplay片段的XML:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.touristenfahrerforum.MarcelMoiser.Fragments.Startdisplay">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="114dp"
android:onClick="startButton"
android:text="Start"
android:textSize="50sp" />
....
的Java类Startdisplay片段:
public class Startdisplay extends Fragment
{
....
private Activity activity;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_startdisplay, container, false);
loadTrackText = (TextView)view.findViewById(R.id.loadTrackText);
loadModeText = (TextView)view.findViewById(R.id.loadModeText);
loadBestlapText = (TextView)view.findViewById(R.id.loadBestlapText);
fileService = new Fileservice();
activity = this.getActivity();
return view;
}
....
/**
* Actionlistener für den Start Button
*/
public void startButton(View view) //!!!!!!!!!!!!!!This method should be called when the button is pressed <-------------------------------------------------------------
{
if(track != null)
{
if(((LocationManager) activity.getSystemService(activity.LOCATION_SERVICE)).isProviderEnabled(LocationManager.GPS_PROVIDER))
{//GPS ist aktiviert
Intent intent = new Intent(activity, MainActivity.class);
intent.putExtra(V.TRACK_EXTRA, track); //Hier könnte man vll auch nur den String statt dem Objekt übergeben <------
intent.putExtra(V.MODE_EXTRA, mode);
intent.putExtra(V.BESTLAP_EXTRA, bestlap);
startActivity(intent);
}
else{}//Meldung geben das das GPS nicht aktiviert ist
}
else{}//else es muss erst eine Strecke geladen werden
}
....
的MAINMENU XML(抽屉布局):
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_menu"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111" />
</android.support.v4.widget.DrawerLayout>
和Mainmenuactivity(应用程序搜索startBut的位置吨的ActionListener):
public class MainMenuActivity extends Activity
{
private String[] menuItems;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private CharSequence mTitle;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
//Standartwerte für die Einstellungen erzeugen
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
mTitle = getTitle();
menuItems = new String[]{"Peter","Susi","Elephant"};
mDrawerLayout = (DrawerLayout) findViewById(R.id.main_menu);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
//Holt sich den Wert für die Umrechnung zwischen PX und DP
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
V.LOGICAL_DENSITY = metrics.density;
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, menuItems));
// Set the list's click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}
/* The click listner 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);
}
}
/** Swaps fragments in the main content view */
private void selectItem(int position)
{
// Create a new fragment and specify the planet to show based on position
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new Startdisplay())
.commit();
// Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(menuItems[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title)
{
mTitle = title;
getActionBar().setTitle(mTitle);
}
}
我只是测试,所以的菜单项有没有意义,它总是加载Startdisplay片段,无论我点击。就像我已经说过的那样,应用程序应该在Startdisplay.java中使用actionlistener,并且不应该在MainMenuActivity.java中搜索它。 在此先感谢
如果你希望有人回答你的问题,请提供你的代码。 – 2014-09-28 14:35:33
好的,我添加了代码。因为它太多了,我认为如果我能够正确解释问题会更容易。 – 2014-09-28 15:50:50