Android GridView显示不正确的项目和片段显示在另一个片段之上

时间:2014-07-04 06:50:08

标签: android gridview android-fragments

我正在实现ActionBar Tab以显示3个选项卡,每个选项卡都包含一个GridLayout。当视图向下滚动时出现问题,隐藏的项目出现但订单以某种方式加扰。屏幕最初仅显示4个项目(在2列网格中),但当第5个进入视图时,显示项目1,但第6个是正确的。第7和第8项也不正确,错误也类似地显示在其他选项卡上。滚动选项卡也会使订单混乱。

Jumbled Gridview order

第二个问题出现在我在网格中的每个项目上实现onItemClickListener时,它应该用一个显示更大视图的片段替换片段。它不是替换视图,而是位于显示屏的顶部,底部的片段显示出来。

Fragment overlayed

我无法弄清楚出了什么问题。这是我的代码

主要活动:

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>

1 个答案:

答案 0 :(得分:2)

我没看到你的整个代码。 。 。只有你的适配器中的getView方法,因为这是我怀疑问题所在。

当convertView不为null时,您只需返回convertView。 。 。这就是导致问题的原因。执行此操作时,您将返回适配器正在尝试从一个gridview位置回收到另一个gridview位置的视图。这就是为什么一个视图中的图像出现在另一个视图中的原因。相反,这样做:

@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);
      } else {
        grid = (View) convertView;
      }

        TextView textView = (TextView) grid.findViewById(R.id.txtItemName);
        ImageView imageView = (ImageView)grid.findViewById(R.id.imageItem);
        textView.setText(libraryItem[position]);
        imageView.setImageResource(imageId[position]);
  return grid;
}