2015-03-13 2 views
2

Прежде чем вопрос будет проголосован как возможный дубликат this, this или this У меня есть ящик для перемещения материалов. У меня есть следующие элементы, как видно из изображений A и B. Я хотел добавить функцию scrollview, чтобы прокрутка элементов позволяла пользователю прокручивать весь макет, включая gridView, как видно на изображении A. Однако при включении прокрутки я получаю проблемы в B, где отображается только одна строка. Я исследовал Google и S/O, и решения не сработали. У решений не было <android.support.v4.widget.DrawerLayout в качестве родительского представления, чтобы вы могли представить, как я застрял. Все решения или работа вокруг будут оценены. enter image description hereКак добавить gridView в scrollView в android

activity_main.xml:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    android:id="@+id/drawer_layout" 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent"> 
 
<ScrollView 
 
    android:layout_width="match_parent" 
 
    android:layout_height="match_parent"> 
 
    <RelativeLayout 
 
     android:layout_width="fill_parent" 
 
     android:layout_height="match_parent" 
 
     tools:context="com.snappy.stevekamau.cosmeticsapp.MainActivity"> 
 

 
     <include 
 
      android:id="@+id/app_bar" 
 
      layout="@layout/app_bar"></include> 
 

 

 
     <ImageView 
 
      android:id="@+id/imageTop" 
 
      android:layout_width="match_parent" 
 
      android:layout_height="wrap_content" 
 
      android:layout_alignParentTop="true" 
 
      android:layout_below="@+id/app_bar" 
 

 
      android:layout_marginLeft="3dp" 
 
      android:layout_marginRight="3dp" 
 

 
      android:src="@drawable/fairnessteaser" /> 
 

 
     <Button 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:layout_alignRight="@+id/imageTop" 
 
      android:layout_below="@+id/app_bar" 
 
      android:background="@drawable/red_button" 
 
      style="@style/button_text" 
 
      android:layout_alignBaseline="@+id/imageTop" 
 
      android:text="Explore" /> 
 

 
     <TextView 
 
      android:id="@+id/whatsNew" 
 
      android:layout_width="wrap_content" 
 
      android:layout_height="wrap_content" 
 
      android:layout_below="@+id/imageTop" 
 
      android:text="What's new" /> 
 

 
     <GridView 
 
      android:id="@+id/grid" 
 
      android:layout_width="fill_parent" 
 
      android:layout_height="fill_parent" 
 
      android:layout_below="@+id/whatsNew" 
 
      android:columnWidth="100dp" 
 
      android:gravity="center" 
 
      android:horizontalSpacing="3dp" 
 
      android:listSelector="@drawable/list_row_selector" 
 
      android:numColumns="3" 
 
      android:padding="3dp" 
 
      android:stretchMode="columnWidth" 
 
      android:verticalSpacing="10dp" /> 
 

 

 
    </RelativeLayout> 
 
</ScrollView> 
 
    <fragment 
 
     android:id="@+id/fragment_navigation_drawer" 
 
     android:name="com.snappy.stevekamau.cosmeticsapp.NavigationDrawerFragment" 
 
     android:layout_width="@dimen/nav_drawer_width" 
 
     android:layout_height="match_parent" 
 
     android:layout_gravity="start" 
 
     app:layout="@layout/fragment_navigation_drawer" 
 
     tools:layout="@layout/fragment_navigation_drawer"></fragment> 
 

 
</android.support.v4.widget.DrawerLayout>

MainAcvtivity.java

MainAcvtivity.java 
 

 
public class MainActivity extends ActionBarActivity { 
 

 
    private Toolbar toolbar; 
 
    private static final String TAG = MainActivity.class.getSimpleName(); 
 

 
    // Movies json url 
 
    private static final String url = "http://api.androidhive.info/json/movies.json"; 
 
    private ProgressDialog pDialog; 
 
    private List<Movie> movieList = new ArrayList<Movie>(); 
 
    private GridView gridView; 
 
    private CustomListAdapter adapter; 
 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_main); 
 
     toolbar = (Toolbar) findViewById(R.id.app_bar); 
 
     setSupportActionBar(toolbar); 
 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 
 
     NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment) 
 
       getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer); 
 
     drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar); 
 

 

 
     gridView = (GridView) findViewById(R.id.grid); 
 
     adapter = new CustomListAdapter(this, movieList); 
 
     gridView.setAdapter(adapter); 
 

 
     pDialog = new ProgressDialog(this); 
 
     // Showing progress dialog before making http request 
 
     pDialog.setMessage("Loading..."); 
 
     pDialog.show(); 
 

 

 

 
     // Creating volley request obj 
 
     JsonArrayRequest movieReq = new JsonArrayRequest(url, 
 
       new Response.Listener<JSONArray>() { 
 
        @Override 
 
        public void onResponse(JSONArray response) { 
 
         Log.d(TAG, response.toString()); 
 
         hidePDialog(); 
 

 
         // Parsing json 
 
         for (int i = 0; i < response.length(); i++) { 
 
          try { 
 

 
           JSONObject obj = response.getJSONObject(i); 
 
           Movie movie = new Movie(); 
 
           movie.setTitle(obj.getString("title")); 
 
           movie.setThumbnailUrl(obj.getString("image")); 
 
           movie.setRating(((Number) obj.get("rating")) 
 
             .doubleValue()); 
 
           movie.setYear(obj.getInt("releaseYear")); 
 

 
           // Genre is json array 
 
           JSONArray genreArry = obj.getJSONArray("genre"); 
 
           ArrayList<String> genre = new ArrayList<String>(); 
 
           for (int j = 0; j < genreArry.length(); j++) { 
 
            genre.add((String) genreArry.get(j)); 
 
           } 
 
           movie.setGenre(genre); 
 

 
           // adding movie to movies array 
 
           movieList.add(movie); 
 

 
          } catch (JSONException e) { 
 
           e.printStackTrace(); 
 
          } 
 

 
         } 
 

 
         // notifying list adapter about data changes 
 
         // so that it renders the list view with updated data 
 
         adapter.notifyDataSetChanged(); 
 
        } 
 
       }, new Response.ErrorListener() { 
 
      @Override 
 
      public void onErrorResponse(VolleyError error) { 
 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
 
       hidePDialog(); 
 

 
      } 
 
     }); 
 

 
     // Adding request to request queue 
 
     AppController.getInstance().addToRequestQueue(movieReq); 
 
    } 
 

 
    @Override 
 
    public void onDestroy() { 
 
     super.onDestroy(); 
 
     hidePDialog(); 
 
    } 
 

 
    private void hidePDialog() { 
 
     if (pDialog != null) { 
 
      pDialog.dismiss(); 
 
      pDialog = null; 
 
     } 
 
    } 
 

 

 
    @Override 
 
    public boolean onCreateOptionsMenu(Menu menu) { 
 
     // Inflate the menu; this adds items to the action bar if it is present. 
 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
 
     return true; 
 
    } 
 

 
    @Override 
 
    public boolean onOptionsItemSelected(MenuItem item) { 
 
     // Handle action bar item clicks here. The action bar will 
 
     // automatically handle clicks on the Home/Up button, so long 
 
     // as you specify a parent activity in AndroidManifest.xml. 
 
     int id = item.getItemId(); 
 

 
     //noinspection SimplifiableIfStatement 
 
     if (id == R.id.action_settings) { 
 
      return true; 
 
     } 
 

 
     return super.onOptionsItemSelected(item); 
 
    } 
 
}

+0

Вы открыли и видите инструмент разработки * показать границу *, который покажет вам границу, маржу, отступы и т. Д. – Turtle

+0

@ Черепаха, я боюсь, я боюсь, что я сделал это –

ответ

2

Я думал, что я мог бы добавить это для всех, кто приходит в поисках полного ответа .После реализующего @innershows ответ, я создал класс InnerGridView и сделал ссылку на моей onCreate() в моем MainActivity как: InnerGridView gridView = (InnerGridView) findViewById(R.id.grid); adapter = new CustomListAdapter(this, movieList); gridView.setAdapter(adapter); Тогда на моем activty_main:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     tools:context="com.snappy.stevekamau.cosmeticsapp.MainActivity"> 

     <include 
      android:id="@+id/app_bar" 
      layout="@layout/app_bar"></include> 


     <ImageView 
      android:id="@+id/imageTop" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_below="@+id/app_bar" 

      android:layout_marginLeft="3dp" 
      android:layout_marginRight="3dp" 

      android:src="@drawable/fairnessteaser" /> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignRight="@+id/imageTop" 
      android:layout_below="@+id/app_bar" 
      android:background="@drawable/red_button" 
      style="@style/button_text" 
      android:layout_alignBaseline="@+id/imageTop" 
      android:text="Explore" /> 

     <TextView 
      android:id="@+id/whatsNew" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/imageTop" 
      android:text="What's new" /> 

     <com.snappy.stevekamau.cosmeticsapp.InnerGridView 
      android:id="@+id/grid" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_below="@+id/whatsNew" 
      android:columnWidth="100dp" 
      android:gravity="center" 
      android:horizontalSpacing="3dp" 
      android:listSelector="@drawable/list_row_selector" 
      android:numColumns="3" 
      android:padding="3dp" 
      android:stretchMode="columnWidth" 
      android:verticalSpacing="10dp" /> 


    </RelativeLayout> 
</ScrollView> 
<fragment 
    android:id="@+id/fragment_navigation_drawer" 
    android:name="com.snappy.stevekamau.cosmeticsapp.NavigationDrawerFragment" 
    android:layout_width="@dimen/nav_drawer_width" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    app:layout="@layout/fragment_navigation_drawer" 
    tools:layout="@layout/fragment_navigation_drawer"></fragment> 

3

Может быть, вы пе это настраиваемый gridView.

общественного класса InnerGridView расширяет GridView {

public InnerGridView(Context context) { 
    super(context); 
} 

public InnerGridView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public InnerGridView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    // Max Height 
    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, 
      MeasureSpec.AT_MOST); 


    super.onMeasure(widthMeasureSpec, expandSpec); 
} 

}

+0

Затем я добавляю пользовательский gridview для замены моего вида сетки? –

+0

Это сработало спасибо. –

Смежные вопросы