Android-关于ListView中的position

山顶洞人02 发布于 2014/04/28 18:54
阅读 1K+
收藏 0

要实现的功能是:通过左侧划出导航可以切换大类(我这是直接改变ActionBar的名字),Actionbar结合了ViewPager可以滑动,不同的导航不同的Actionbar的项对应不同的Fragment,总共有15个Fragment,现在碰到的问题是我只能将Actionbar中的arg0传入Fragment,导航中ListView的position无法传输

package com.example.android.navigationdrawerexample;


import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class MainActivity extends FragmentActivity implements TabListener{

private ViewPager mViewPager;
public static final int MAX_TAB_SIZE = 3;
private TabFragmentPagerAdapter mAdapter;
ActionBar mActionBar;

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;


    private String[] mPlanetTitles;
    private String[] initMenu;
    private static int temp = 1;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


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


        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mPlanetTitles));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
       
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
        
    initView();
    }


    /**
     * ListView的事件监听
     * @author Administrator
     *
     */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
   
 @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 
  switch (position) {
case 0:
MainActivity.setTemp(0);
getActionBar().getTabAt(0).setText("焦点");
getActionBar().getTabAt(1).setText("专题");
getActionBar().getTabAt(2).setText("发现");
break;
case 1:
MainActivity.setTemp(1);
getActionBar().getTabAt(0).setText("精品");
getActionBar().getTabAt(1).setText("分类");
getActionBar().getTabAt(2).setText("新锐");
       break;
case 2:
MainActivity.setTemp(2);
getActionBar().getTabAt(0).setText("精品");
getActionBar().getTabAt(1).setText("分类");
getActionBar().getTabAt(2).setText("新锐");
break;
case 3:
MainActivity.setTemp(3);
getActionBar().getTabAt(0).setText("飙升榜");
getActionBar().getTabAt(1).setText("最新榜");
getActionBar().getTabAt(2).setText("本地绑");
break;
default:
getActionBar().getTabAt(0).setText("个人设置");
getActionBar().getTabAt(1).setText("下载管理");
getActionBar().getTabAt(2).setText("本地管理");
break;
}
}
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

private void initView() {

mViewPager = (ViewPager) this.findViewById(R.id.pager);
mActionBar = getActionBar();
mActionBar.setDisplayHomeAsUpEnabled(false);
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageSelected(int arg0) {

mActionBar.setSelectedNavigationItem(arg0);
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {

}

@Override
public void onPageScrollStateChanged(int arg0) {
}
});

initMenu = new String[]{"焦点", "专题", "发现"};
//初始化 ActionBar
for(int i=0;i<MAX_TAB_SIZE;i++){
Tab tab = mActionBar.newTab();
tab.setText(initMenu[i]).setTabListener(this);
mActionBar.addTab(tab);
}
}

public static class TabFragmentPagerAdapter extends FragmentPagerAdapter{

public TabFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int arg0) {
Fragment ft = null;
int[] arrays = new int[]{arg0, MainActivity.getTemp()};
Bundle bundle = new Bundle();
bundle.putIntArray("tab1", arrays);
ft = new FragmentFactory();
ft.setArguments(bundle);
return ft;
}

@Override
public int getCount() {

return MAX_TAB_SIZE;
}
}
    
@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {

}

@Override
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {

}

public ListView getmDrawerList() {
return mDrawerList;
}
public static int getTemp() {
return temp;
}

public static void setTemp(int temp) {
MainActivity.temp = temp;
}
}







package com.example.android.navigationdrawerexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentFactory extends Fragment{

View[][] views = new View[3][5];

    //Fragment要通过重写onCreateView方法加载视图 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) { 
   
    Bundle bundle = getArguments();
   
   
views[0][0] = inflater.inflate(R.layout.fragment1, container,false); 
views[0][1] = inflater.inflate(R.layout.fragment2, container,false);
views[0][2] = inflater.inflate(R.layout.fragment3, container,false);
views[0][3] = inflater.inflate(R.layout.fragment4, container,false);
views[0][4] = inflater.inflate(R.layout.fragment5, container,false);
views[1][0] = inflater.inflate(R.layout.tab2_fragment1, container,false);
views[1][1] = inflater.inflate(R.layout.tab2_fragment2, container,false);
views[1][2] = inflater.inflate(R.layout.tab2_fragment3, container,false);
views[1][3] = inflater.inflate(R.layout.tab2_fragment4, container,false);
views[1][4] = inflater.inflate(R.layout.tab2_fragment5, container,false);
views[2][0] = inflater.inflate(R.layout.tab3_fragment1, container,false);
views[2][1] = inflater.inflate(R.layout.tab3_fragment2, container,false);
views[2][2] = inflater.inflate(R.layout.tab3_fragment3, container,false);
views[2][3] = inflater.inflate(R.layout.tab3_fragment4, container,false);
views[2][4] = inflater.inflate(R.layout.tab3_fragment5, container,false);
   
View view = views[bundle.getIntArray("tab1")[0]][bundle.getIntArray("tab1")[1]];
        return view;
    }
}
    


加载中
返回顶部
顶部