gpt4 book ai didi

android - Fragment UI 使用抽屉导航

转载 作者:行者123 更新时间:2023-11-30 02:50:15 26 4
gpt4 key购买 nike

因此,我在项目的 MainActivity 中具有抽屉导航功能,并且我的抽屉导航链接链接到一个 fragment ,但我不知道如何向其添加 UI。特别是 findViewById(r.id.code) 之类的东西,以及尝试设置 TextView 时出现的错误,例如 unreachable code 这是我目前所遇到的错误。

主要 Activity

    package yc.android.yourchallenger;

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.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

private String [] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mNavigationDrawerItemTitles = getResources().getStringArray(R.array.navigation_drawer_items_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);

// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.nav_drawer, mNavigationDrawerItemTitles));
// Set the list's click listener
mDrawerList.setOnItemClickListener((OnItemClickListener) new DrawerItemClickListener());

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_launcher, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {

/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
}

/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
}
};

// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}

@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);
mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...

return super.onOptionsItemSelected(item);
}

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) {
Fragment fragment = null;

switch (position){
case 0: fragment = new CreateFragment();
break;

case 1: fragment = new ReadFragment();
break;

case 2: fragment = new HelpFragment();
break;

default:
break;
}
/* Bundle args = new Bundle();
args.putInt("key", position);
fragment.setArguments(args);*/
if(fragment != null){
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
// Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
}

@Override
public void setTitle(CharSequence title){
mTitle = title;
getActionBar().setTitle(mTitle);
}


}

这是我的一个 fragment 包 yc.android.yourchallenger;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ReadFragment extends Fragment {
public ReadFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_create, container, false);
TextView view = findViewById(R.id.view);
return rootView;
}

最佳答案

没有理由让在 onCreateView() 中创建的 View 成为静态的(关于 Droidy)。只需在 onCreateView() 之后的 Fragment 生命周期内的任何地方调用 getView()

在你的例子中,你所需要的只是改变

TextView view = findViewById(R.id.view);

TextView view = (TextView) rootView.findViewById(R.id.view);

您可以在任何 ViewGroup 上调用 findViewById(int) 来查找它的子级。在 Activity 中调用它是一种使用 setContentView() 中设置的 View 的便捷方法。

因此,如果您想稍后在 Fragment 生命周期中找到 View ,您可以这样做:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
TextView view = (TextView) getView().findViewById(R.id.view);
}

您还可以在 onViewCreated(View view, Bundle savedInstanceState) 中实例化 View ,其中 Root View 是第一个参数。

关于android - Fragment UI 使用抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24316678/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com