我正在實現ActionBar選項卡以顯示3個選項卡,其中每個選項卡都包含一個GridLayout。問題出現在視圖向下滾動時,隱藏的項目出現,但順序以某種方式混亂。屏幕最初只顯示4個項目(在2列網格中),但當第5個顯示時,顯示項目1,而第6個顯示正確。第7和第8項也是不正確的,並且錯誤類似地顯示在其他標籤上。滾動選項卡也會顛簸順序。Android GridView顯示不正確的項目和片段顯示在另一個片段頂部
當我實施onItemClickListener上網格中的每個項目,這應該與一個表示較大視圖替換片段的第二個問題出現了。它不是替換視圖,而是位於顯示器的頂部,底部顯示片段。
我無法弄清楚什麼是錯的。這裏是我的代碼
主要活動:
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
ViewPager mViewPager;
public static String[] mTabItems;
public static int[] mTabImages;
public static GridView mGridview;
public static String[][] itemName = new String[3][8];
public static int [][] itemPics = new int[3][8];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
final ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab().setText(mAppSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
int i = 0;
for (int tab = 1; tab<4; tab++){
for (int item = 1; item<9; item++){
String image = "tab" + tab + "_" + item;
int imageIden = getResources().getIdentifier(image, "drawable","com.tabgriddrawer");
itemPics[tab-1][item-1] = imageIden;
itemName[tab-1][item-1] = image;
i++;
}
}
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment frag = new SectionFragment();
Bundle arguments = new Bundle();
arguments.putInt(SectionFragment.ARG_TAB_NUMBER, i);
frag.setArguments(arguments);
return frag;
}
@Override
public int getCount() {
return 3;//set 3 tabs
}
@Override
public CharSequence getPageTitle(int position) {
return "Tab " + (position + 1);
}
}
public static class SectionFragment extends Fragment {
public static final String ARG_TAB_NUMBER = "tab_number";
public static Bundle detailArguments;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final Bundle arg = getArguments();
int tab = arg.getInt(ARG_TAB_NUMBER);
mTabItems = new String[8];
mTabImages = new int[8];
for (int m=0; m<8; m++){
mTabItems[m] = itemName[tab][m];
mTabImages[m] = itemPics[tab][m];
}
View rootView = inflater.inflate(R.layout.fragment_section_gridview, container, false);
CustomGrid adapter = new CustomGrid(getActivity().getApplicationContext(), mTabItems,mTabImages);
mGridview = (GridView) rootView.findViewById(R.id.fragment_grid_view);
mGridview.setAdapter(adapter);
mGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id){
int t = arg.getInt(ARG_TAB_NUMBER);
Fragment itemDetailFragment = new ItemDetailFragment();
detailArguments = new Bundle();
detailArguments.putString("ItemName", itemName[t][position]);
detailArguments.putInt("ImageId", itemPics[t][position]);
itemDetailFragment.setArguments(detailArguments);
FragmentManager fm = getFragmentManager();
Fragment itemFragment = fm.findFragmentById(R.id.pager);
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.hide(itemFragment);
ft.replace(R.id.drawer_layout, itemDetailFragment, "detail");
ft.commit();
}
});
return rootView;
}
}
}
定製的GridView適配器:
public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String[] libraryItem;
private final int[] imageId;
public CustomGrid(Context c,String[] libItem,int[] Imageid) {
mContext = c;
this.imageId = Imageid;
this.libraryItem = libItem;
}
@Override
public int getCount() {
return libraryItem.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.fragment_gridview_item, null);
TextView textView = (TextView) grid.findViewById(R.id.txtItemName);
ImageView imageView = (ImageView)grid.findViewById(R.id.imageItem);
textView.setText(libraryItem[position]);
imageView.setImageResource(imageId[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
項目詳細內容片斷:
public class ItemDetailFragment extends Fragment {
private Context mContext;
private String mItemDesc;
private int mImageId;
private String mItemName;
static int tab;
static int position;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_item_detail, container, false);
ImageView image = (ImageView)view.findViewById(R.id.itemimage);
image.setImageResource(mImageId);
TextView itemtext = (TextView)view.findViewById(R.id.itemName);
itemtext.setText(mItemName);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ActionBar actionBar = getActivity().getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
mImageId = (int)MainActivity.SectionFragment.detailArguments.getInt("ImageId");
mItemName = (String)MainActivity.SectionFragment.detailArguments.get("ItemName");
}
public void onBackPressed(){
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.main_frame, new MainActivity.SectionFragment());
ft.addToBackStack(null);
ft.commit();
}
}
主要XML
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- moved viewpager to inside drawer layout -->
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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>
的GridView XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/fragment_grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>