2016-01-29 2 views
1

Я пытаюсь создать gridview с изображениями, используя библиотеку Picasso. Я создаю фрагмент с gridview и устанавливаю адаптер в gridview, но не вижу ничего на дисплее. Вот мой адаптер код:Создание GridView с изображениями Picasso в Android

public class GridViewAdapter extends ArrayAdapter { 
    private Context context; 
    private LayoutInflater inflater; 

    private String[] imageUrls; 

    public GridViewAdapter(Context context, String[] imageUrls) { 
     super(context, R.layout.grid_item, imageUrls); 

     this.context = context; 
     this.imageUrls = imageUrls; 

     inflater = LayoutInflater.from(context); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (null == convertView) { 
      convertView = inflater.inflate(R.layout.grid_item, parent, false); 
     } 

     Picasso 
       .with(context) 
       .load(imageUrls[position]) 
       .fit() // will explain later 
       .into((ImageView) convertView); 

     return convertView; 
    } 
} 

Вот код моего фрагмента

public class ViewMoviesFragment extends Fragment { 

    GridViewAdapter adapter; 
    public ViewMoviesFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
      String[] eatFoodyImages = { 
       "http://i.imgur.com/rFLNqWI.jpg", 
       "http://i.imgur.com/C9pBVt7.jpg", 
       "http://i.imgur.com/rT5vXE1.jpg", 
       "http://i.imgur.com/aIy5R2k.jpg", 
       "http://i.imgur.com/MoJs9pT.jpg", 
       "http://i.imgur.com/S963yEM.jpg", 
       "http://i.imgur.com/rLR2cyc.jpg", 
       "http://i.imgur.com/SEPdUIx.jpg", 
       "http://i.imgur.com/aC9OjaM.jpg", 
       "http://i.imgur.com/76Jfv9b.jpg", 
       "http://i.imgur.com/fUX7EIB.jpg", 
       "http://i.imgur.com/syELajx.jpg", 
       "http://i.imgur.com/COzBnru.jpg", 
       "http://i.imgur.com/Z3QjilA.jpg", 
     }; 
     View v= inflater.inflate(R.layout.grid_fragment, container, false); 
     GridView gridview = (GridView) v.findViewById(R.id.poster_view); 
     gridview.setAdapter(new GridViewAdapter(getActivity(), eatFoodyImages)); 



     return v; 
    } 
} 

Мой код вида деятельности:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_activity); 

     FragmentManager fm = getSupportFragmentManager(); 
     Fragment fragment = fm.findFragmentById(R.id.fragment_container); 

     if (fragment == null) { 
      fragment = new ViewMoviesFragment(); 
      fm.beginTransaction() 
        .add(R.id.fragment_container, fragment) 
        .commit(); 
     } 
    } 
} 

main_activity.xml

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</FrameLayout> 

grid_it em.xml

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</FrameLayout> 

grid_fragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
    <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/poster_view" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:numColumns="2"> 
    </GridView> 

</FrameLayout> 

Когда я запускаю мое приложение я просто белое окно. AndroidManifest.xml имеют доступ к интернет-разрешению. Может ли кто-нибудь помочь мне показывать изображения в gridview?

+0

@ g2o Вы уверены, что OP говорит о 'picasso' вместо Googles' picasa'? –

+0

@LJ в коде Picasso.with (контекст), поэтому я хочу, что это библиотека Пикассо –

ответ

0

Проблема заключается в вашей GridViewAdapter

Picasso 
      .with(context) 
      .load(imageUrls[position]) 
      .fit() // will explain later 
      .into((ImageView) convertView); 

convertView является Framelayout

+0

Я удалил fit(), и теперь он работает, но все же теперь я не могу получить, как изменить размер моих изображений без соответствия – user6005