从onCreate中的NavigationDrawer里面的ListView中选择第一项?

时间:2014-11-30 01:31:44

标签: android android-intent android-activity android-listview navigation-drawer

我正在编写支票簿Android应用程序,该应用程序显示导航抽屉内的银行帐户列表。应用程序的主屏幕显示基本帐户信息(名称,余额等)。

我能够实现onClick方法,以便在选择帐户时更新主屏幕,但我希望以应用程序打开时选择ListView中的第一项的方式实现此目的。

换句话说,当我启动应用程序时,我应该看到检查帐户信息,但我目前看到的所有内容都是默认值0,因为在启动时没有将Intent传递给Activity。我试图以编程方式选择主活动的onCreate方法中的第一个项目,但这不起作用。

修改

以下是我对主要活动的onCreate的代码:

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

    final View rootView = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);

    mDrawerListView = (ListView) rootView.findViewById(R.id.listview_account);

    // Get the Cursor that will be used to load accounts
    Cursor accounts = dataSource.getAccounts();

    // Make adapter.
    mAccountAdapter = new AccountAdapter(getActivity(), accounts, 0);

    mDrawerListView.setAdapter(mAccountAdapter);
    mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l){
            HeaderViewListAdapter headerViewAdapter = (HeaderViewListAdapter) adapterView.getAdapter();
            AccountAdapter adapter = (AccountAdapter) headerViewAdapter.getWrappedAdapter();
            Cursor cursor = adapter.getCursor();
            if(cursor != null && cursor.moveToPosition(i)){
                // Adjust main page
            }
        }
    });

    return rootView;
}

我尝试在调用setUp方法后以编程方式选择第一个项目,但这不起作用。这应该在主要活动还是片段中处理?

4 个答案:

答案 0 :(得分:3)

您是否看过example导航抽屉?作为旁注,在Android Studio中创建新项目时,您可以使用的模板之一是基于导航抽屉的应用程序。我建议你看看并熟悉它是如何工作的。

无论如何,我假设你正在使用那个拥有MainActivity的例子,当你在NavigationDrawer中选择项目时,你实际上是在替换MainActivity容器中的片段。例如,你可以做这样的事情(有些伪代码):

private ListView mNavigationList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // init your listview, etc.
    mNavigationList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            onNavigationItemSelected(position);
        }
    });

    // show the "first" fragment
    onNavigationItemSelected(0);
}

private void onNavigationItemSelected(int position) {
    FragmentManager manager = getFragmentManager();
    Fragment f;
    String tag;

    switch (position) {
    case 0:
        tag = YourFragment.TAG;
        f = manager.findFragmentByTag(tag);
        if (f == null) {
            f = YourFragment.newInstance();
        }
    // ...
    }

    FragmentTransaction t = manager.beginTransaction();
    t.replace(R.id.container, f, tag);
    t.addToBackStack(tag);
    t.commit();
}

您的主要活动布局可能包括FrameLayout和DrawerLayout。当您从抽屉的ListView中选择项目时,将触发onNavigationItemSelected,并使用您选择的片段替换FrameLayout容器。根据您的问题,当应用程序加载时,容器将替换为&#34;第一个&#34;片段。

答案 1 :(得分:1)

public class HomeActivity extends FragmentActivity {

FragmentContainerHandler fragObj = new FragmentContainerHandler(
        getSupportFragmentManager());

private DrawerLayout mdrawerlayout;
private ListView mListview;
private ImageView mActionbarButton;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
private ActionBarDrawerToggle mActionbartogle;
private TextView mtimertextview, headerTV;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    mListview = (ListView) findViewById(R.id.left_drawer);

    //set here array adapter
    mAccountAdapter = new AccountAdapter(getActivity(), accounts, 0);

    mListview.setAdapter(mAccountAdapter);

    mdrawerlayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    ActionBar obj = getActionBar();

    obj.setCustomView(R.layout.action_bar_custum);
    obj.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

    mActionbarButton = (ImageView) findViewById(R.id.actionbarButton);
    mActionbarButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mdrawerlayout.openDrawer(mListview);
            if (mdrawerlayout.isDrawerOpen(mListview)) {
                mdrawerlayout.closeDrawer(mListview);

            }
        }
    });
    mListview.setOnItemClickListener(new SelectItemFormListView());
    fragObj.replaceFragment(0);
}
@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) {

    int id = item.getItemId();
    return super.onOptionsItemSelected(item);
}
public class SelectItemFormListView implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        mdrawerlayout.closeDrawer(mListview);

        if (position == 5) {

            startActivity(new Intent(HomeActivity.this, MainAcivity.class));

            finish();
        } else {
            fragObj.replaceFragment(position);
        }
    }
}

}

public class FragmentContainerHandler {
FragmentManager fragmntMgr;

public FragmentContainerHandler(FragmentManager fragmntMgr) {
    this.fragmntMgr = fragmntMgr;
}

public void replaceFragment(int position) {
    Fragment fragment = null;
    if (position == 0) {
        fragment = new FindPeoples(this);
    }
    if (position == 4) {
        fragment = new ProfileView();
    }
    if (position == 5) {
        fragment = new Notification();
    }
    if (position == 6) {
        fragment = new Private_Message();
    }

    android.support.v4.app.FragmentTransaction ft = fragmntMgr
            .beginTransaction();

    // ft.setCustomAnimations(R.anim.rightsidein,0);
    List<Fragment> list = fragmntMgr.getFragments();
    if (list != null) {
        for (Fragment element : list) {

            if (element != null) {
                ft.remove(element);
                fragmntMgr.popBackStack();
            }
        }
    }
    ft.replace(R.id.content_frame, fragment, "fragment");
    ft.commit();
}

}

public class FindPost extends android.support.v4.app.Fragment {
private View rootview;

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

    rootview = inflater.inflate(R.layout.activity_find_post, container,
            false);

    return rootview;
}

}

答案 2 :(得分:0)

创建一个类来保存项目的数据。在项类中创建一个布尔变量isSelected。检查适配器isSelected中的getView。获取onCreate中listview中的第一项,并在第一项上将isSelected变量设置为true。

答案 3 :(得分:0)

将此属性放在listview上(在xml代码中)

android:choiceMode="singleChoice"

并在oncreate函数上编写此代码

arraylist.get(0).setSelected(true);

arrayAdapter.notifyDataSetChanged();

使用listview的通用类

public class ListData 
{
public String MenuTitle="";

public boolean Selected=false;
public String getMenuTitle() 
{
    return MenuTitle;
}
public void setMenuTitle(String menuTitle) 
{
    MenuTitle = menuTitle;
}
public boolean isSelected() 
{
    return Selected;
}
public void setSelected(boolean selected) 
{
    Selected = selected;
}
}