我正在尝试实施Picasso来帮助将图片加载到GridView
。目前,下面的代码运行没有任何问题,但GridView
项只是空白。我知道特定ImageView
实际存在,因为如果我选择它并按住,selected_state
drawable就会被激活,正如您在this屏幕截图中看到的那样。
你能帮我找到问题吗?谢谢!
GalleryFragment.java
:
//in onCreateView()
v = inflater.inflate(R.layout.fragment_gallery, parent, false);
GridView gridView = (GridView) v.findViewById(R.id.fragmentGalleryGridview);
gridView.setAdapter(new GalleryAdapter(getActivity()));
gridView.setOnScrollListener(new GalleryScrollListener(getActivity()));
return v;
GalleryAdapter.java
延伸BaseAdapter
:
public GalleryAdapter(Context mContext)
{
this.mContext = mContext;
inflater = LayoutInflater.from(mContext);
//get ArrayList<String> mPaths from SQLite database
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
SquareImageView view = (SquareImageView) convertView;
if (view == null)
{
view = new SquareImageView(mContext);
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
Picasso.with(mContext)
.load(mPaths.get(position))
.fit()
.into(view);
return view;
}
SquareImageView.java
:
public class SquareImageView extends ImageView
{
//three constructors
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
}
}
fragment_gallery.xml
:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eeeeee"
android:layout_marginTop="?android:attr/actionBarSize">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentGalleryGridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:columnWidth="100dp"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:drawSelectorOnTop="true"
android:stretchMode="columnWidth"/>
</FrameLayout>
答案 0 :(得分:0)
这就是我使用GridLayout成功实现Picasso库的方法...... GridView要求您对项目使用ListAdapter,我遇到了同样的问题但是使用GridLayout解决了它代替:
public class ImageHandler {
private static Picasso instance;
public static Picasso getSharedInstance(Context context)
{
if(instance == null)
{
instance = new Picasso.Builder(context).executor(Executors.newSingleThreadExecutor()).memoryCache(Cache.NONE).indicatorsEnabled(true).build();
return instance;
}
else
{
return instance;
}
}
}
在我实现这个类后,我是如何访问代码并将图像加载到我的ImageView中,ImageView是GridLayout的一部分。
// imString = "http://filepath.file.jpg" as example
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
GridLayout grid = (GridLayout) findViewById(R.id.viewAllGrid);
grid.canScrollVertically(1);
ImageButton image;
for(int i = 0; i < itemsList.size(); i++){
aContainer= (LinearLayout) inflater.inflate(R.layout.layout_container, null);
image = (ImageButton) aContainer.findViewById(R.id.imageViewOrButton);
ImageHandler.getSharedInstance(getApplicationContext()).load(imString).skipMemoryCache().fit().into(image);
grid.addView(aContainer);
}
这是layout_container
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:id="@+id/Container"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
android:layout_width="100dp"
android:layout_height="130dp"
android:background="@null"
android:id="@+id/seriesThumbnail"/>
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"
android:textColor="#ffffffff"
android:id="@+id/title"
android:autoText="false"
android:ellipsize="end"
android:maxLines="1" />
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="@+id/publisher"
android:text="Francis Chan"
android:maxLines="1"
android:ellipsize="end" />
</LinearLayout>
这是viewAllGrid布局架构
<GridLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="20dp"
android:id="@+id/viewAllGrid"
android:layout_gravity="center_horizontal"
android:columnCount="3"
android:layout_weight=".8"
android:orientation="horizontal">
</GridLayout>
答案 1 :(得分:0)
我遇到了同样的问题,因为我错过了权限
<!-- This is required for Picasso to work. -->
<uses-permission android:name="android.permission.INTERNET"/>
<!-- The following permissions are OPTIONAL. -->
<!-- Used to adjust the work load depending on the type of network the device is using. -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>