片段UI使用导航抽屉

时间:2014-06-19 21:41:09

标签: android user-interface android-fragments

所以我的项目的MainActivity中有导航抽屉功能,我的导航抽屉链接链接到片段,但我不知道如何添加UI。特别喜欢findViewById(r.id.code)的东西,并试图设置TextView给我的错误如unreachable code这就是我到目前为止所做的。

MainActivity

    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);
 }


}

这是我的一个片段         包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;
}

}

2 个答案:

答案 0 :(得分:1)

没有理由在onCreateView()静态创建视图(re:Droidy)。只需在getView()之后的片段生命周期内的任何位置调用onCreateView()

在您的示例中,您只需要更改

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

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

您可以在任何findViewById(int)上致电ViewGroup找到它的孩子。在Activity中调用它是一种使用setContentView()中的视图集的便捷方法。

因此,如果您想在Fragment生命周期的后期找到该视图,请执行以下操作:

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

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

答案 1 :(得分:0)

制作rootViewprivatestatic)并在默认构造函数之前声明它。然后在将布局扩展到rootView之后,可以使用rootView访问所有UI元素,如下所示。

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

getActivity()课程中使用this代替关键字Fragment 希望它有所帮助。