2014-05-06 140 views
0

我正在開發基於Tabbar導航模型的Android應用程序。 我的應用程序的一個視圖包含一個「3列」gridview,它包含10個元素,我只能看到9個元素,我無法滾動。GridView不滾動

這是我的代碼:

MoreFragment.java

public class MoreFragment extends Fragment implements 
    AdapterView.OnItemClickListener { 

static final String EXTRA_MAP = "map"; 

static final LauncherIcon[] ICONS = { 
     new LauncherIcon(R.drawable.casino, "Casino", "casino.png"), 
     new LauncherIcon(R.drawable.cinema, "Cinema", "cinema.png"), 
     new LauncherIcon(R.drawable.quad, "Quad", "quad.png"), 
     new LauncherIcon(R.drawable.tennis, "Tennis", "tennis.png"), 
     new LauncherIcon(R.drawable.golf, "Golf", "golf.png"), 
     new LauncherIcon(R.drawable.club, "Club", "club.png"), 
     new LauncherIcon(R.drawable.jazz, "Jazz Bar", "jazz.jpg"), 
     new LauncherIcon(R.drawable.pool, "Pool", "pool.png"), 
     new LauncherIcon(R.drawable.ic_7, "Map" ,"ic_7.png"), 
     new LauncherIcon(R.drawable.fono,"Contact" ,"fono.png") 


}; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.gridview, null); 

    GridView gridview = (GridView) view.findViewById(R.id.dashboard_grid); 
    gridview.setAdapter(new ImageAdapter(getActivity())); 
    gridview.setOnItemClickListener(this); 


    gridview.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      return event.getAction() == MotionEvent.ACTION_MOVE; 
     } 
    }); 
    return view; 
} 

@Override 
public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 

    FragmentManager fm2 = getActivity().getSupportFragmentManager(); 
    FragmentTransaction ft2 = fm2.beginTransaction(); 
    Fragment fragment = null; 

    switch (position) { 

     case 0: 
      fragment = new Generic_SimplePage(getResources().obtainTypedArray(R.array.entertainement_titles).getResourceId(0,-1),getResources().obtainTypedArray(R.array.entertainement_desc).getResourceId(0,-1),getResources().obtainTypedArray(R.array.entertainement_backgrounds).getResourceId(0,-1)); 
      break; 
     case 1: 
      fragment = new Generic_SimplePage(getResources().obtainTypedArray(R.array.entertainement_titles).getResourceId(1,-1),getResources().obtainTypedArray(R.array.entertainement_desc).getResourceId(1,-1),getResources().obtainTypedArray(R.array.entertainement_backgrounds).getResourceId(1,-1)); 
      break; 
     case 2: 
      fragment = new Generic_SimplePage(getResources().obtainTypedArray(R.array.entertainement_titles).getResourceId(2,-1),getResources().obtainTypedArray(R.array.entertainement_desc).getResourceId(2,-1),getResources().obtainTypedArray(R.array.entertainement_backgrounds).getResourceId(2,-1)); 
      break; 
     case 3: 
      fragment = new Generic_SimplePage(getResources().obtainTypedArray(R.array.entertainement_titles).getResourceId(3,-1),getResources().obtainTypedArray(R.array.entertainement_desc).getResourceId(3,-1),getResources().obtainTypedArray(R.array.entertainement_backgrounds).getResourceId(3,-1)); 
      break; 
     case 4: 
      fragment = new Generic_SimplePage(getResources().obtainTypedArray(R.array.entertainement_titles).getResourceId(4,-1),getResources().obtainTypedArray(R.array.entertainement_desc).getResourceId(4,-1),getResources().obtainTypedArray(R.array.entertainement_backgrounds).getResourceId(4,-1)); 
      break; 
     case 5: 
      fragment = new Generic_SimplePage(getResources().obtainTypedArray(R.array.entertainement_titles).getResourceId(5,-1),getResources().obtainTypedArray(R.array.entertainement_desc).getResourceId(5,-1),getResources().obtainTypedArray(R.array.entertainement_backgrounds).getResourceId(5,-1)); 
      break; 
     case 6: 
      fragment = new Generic_SimplePage(getResources().obtainTypedArray(R.array.entertainement_titles).getResourceId(6,-1),getResources().obtainTypedArray(R.array.entertainement_desc).getResourceId(6,-1),getResources().obtainTypedArray(R.array.entertainement_backgrounds).getResourceId(6,-1)); 
      break; 
     case 7: 
      fragment = new Generic_SimplePage(getResources().obtainTypedArray(R.array.entertainement_titles).getResourceId(7,-1),getResources().obtainTypedArray(R.array.entertainement_desc).getResourceId(7,-1),getResources().obtainTypedArray(R.array.entertainement_backgrounds).getResourceId(7,-1)); 
      break; 
     case 8: 
      fragment = new MapFragment(); 
      break; 
     case 9 : 
     fragment = new ContactFragment(); 
      break; 
    default: 
     break; 

    } 
    if (fragment != null) { 
     ft2.replace(R.id.activity_main_content_fragment, fragment); 
     ft2.addToBackStack(fragment.getClass().getName()); 
     ft2.commit(); 

    } 

} 

static class LauncherIcon { 
    final String text; 
    final int imgId; 
    final String map; 

    public LauncherIcon(int imgId, String text, String map) { 
     super(); 
     this.imgId = imgId; 
     this.text = text; 
     this.map = map; 
    } 

} 

static class ImageAdapter extends BaseAdapter { 
    private Context mContext; 

    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    @Override 
    public int getCount() { 
     return ICONS.length; 
    } 

    @Override 
    public LauncherIcon getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    static class ViewHolder { 
     public ImageView icon; 
     public TextView text; 
    } 

    // Create a new ImageView for each item referenced by the Adapter 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     ViewHolder holder; 
     if (v == null) { 
      LayoutInflater vi = (LayoutInflater) mContext 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      v = vi.inflate(R.layout.dashboard_icon, null); 
      holder = new ViewHolder(); 
      holder.text = (TextView) v 
        .findViewById(R.id.dashboard_icon_text); 
      holder.icon = (ImageView) v 
        .findViewById(R.id.dashboard_icon_img); 
      v.setTag(holder); 
     } else { 
      holder = (ViewHolder) v.getTag(); 
     } 

     holder.icon.setImageResource(ICONS[position].imgId); 
     holder.text.setText(ICONS[position].text); 

     return v; 
    } 
} 

}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@android:color/white" 
    android:weightSum="1.0" 
    > 
    <FrameLayout 
     android:id="@+id/activity_main_content_fragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/radiogroup" 
     > 
    </FrameLayout> 

    <RadioGroup 
    android:id="@+id/radiogroup" 
    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    android:layout_alignParentBottom="true" 
    android:orientation="horizontal" 
    android:background="@drawable/navbar_background" 
    > 
    <RadioButton 
     android:id="@+id/btnPres" 
     style="@style/navbar_button" 
     android:drawableTop="@drawable/navbar_presselector" 
     android:text="Presentation" 
     android:layout_marginLeft="5dp" 
    /> 
    <RadioButton 
     android:id="@+id/btnRooms" 
     style="@style/navbar_button" 
     android:drawableTop="@drawable/navbar_roomsselector" 
     android:text="Rooms" 
    /> 
    <RadioButton 
     android:id="@+id/btnRestaurants" 
     style="@style/navbar_button" 
     android:drawableTop="@drawable/navbar_restaurantsselector" 
     android:text="Restaurants" 
     android:layout_marginLeft="5dp" 
    /> 
    <RadioButton 
     android:id="@+id/btnReservation" 
     style="@style/navbar_button" 
     android:drawableTop="@drawable/navbar_reservationselector" 
     android:text="Reservation" 
     android:layout_marginLeft="5dp" 
    /> 

    <RadioButton 
     android:id="@+id/btnMore" 
     style="@style/navbar_button" 
     android:drawableTop="@drawable/navbar_moreselector" 
     android:text="More" 
     android:layout_marginLeft="5dp" 
    /> 
</RadioGroup> 



<LinearLayout 
    android:id="@+id/floatingmenu" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_marginLeft="30dp" 
    android:layout_marginRight="30dp" 
    android:background="@drawable/laysemitransparentwithborders" 
    android:orientation="vertical" 
    android:layout_marginBottom="-4dp" 
    android:visibility="gone" 
> 

    <View 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:background="#ff999999" 
    /> 

</LinearLayout> 
</RelativeLayout> 

gridview.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/b_ground6"> 

<GridView 
    android:id="@+id/dashboard_grid" 
    android:layout_width="fill_parent" 
    android:numColumns="3" 
    android:columnWidth="50px" 
    android:layout_height="fill_parent" 
    android:listSelector="@drawable/list_selector" 
    android:stretchMode="columnWidth" 
    android:verticalSpacing="10.0dip" 
    android:horizontalSpacing="10.0dip" 
    android:layout_centerInParent="true" 
    style="@style/dashboard" 
    /> 


    </LinearLayout> 

回答

1

如果你想要網格視圖,你爲什麼要設置onTouchListener?

我認爲這個問題是在這裏,試評THI一段代碼和測試

// Hack to disable GridView scrolling 
    gridview.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      return event.getAction() == MotionEvent.ACTION_MOVE; 
     } 
    }); 
+0

完美!我沒有提到! – wissem46