我想在我的应用程序中将URL显示为图库。 我用这个作为参考。
http://www.androidhive.info/2016/04/android-glide-image-library-building-image-gallery-app/
但不显示图像。
这是我的 GalleryActivity
public class GalleryActivity extends AppCompatActivity {
String strServerResponse;
ProgressDialog nDialog;
private GalleryAdapter mAdapter;
private RecyclerView recyclerView;
private ArrayList<Image> images;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
images = new ArrayList<>();
new NetCheck().execute();
}
private class NetCheck extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
nDialog = new ProgressDialog(GalleryActivity.this);
nDialog.setMessage("Loading..");
nDialog.setTitle("Please Wait");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.e("Post exec calleld", "dfds");
nDialog.dismiss();
mAdapter = new GalleryAdapter(getApplicationContext(), images);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpRequest = new HttpPost(
uril);
httpRequest.setHeader("Content-Type", "application/json");
JSONObject json = new JSONObject();
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding("UTF-8");
se.setContentType("application/json");
httpRequest.setEntity(se);
HttpResponse httpRes = httpClient.execute(httpRequest);
java.io.InputStream inputStream = httpRes.getEntity()
.getContent();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream);
BufferedReader reader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inputStream.close();
strServerResponse = sb.toString();
Log.e("Server Response", "" + strServerResponse.toString());
if (strServerResponse != null) {
try {
JSONObject jsonObj = new JSONObject(strServerResponse);
String status = jsonObj.getString("status");
if (status.equals("true")) {
String result = jsonObj.getString("images");
Log.e("images", "" + eee);
JSONArray arr1 = new JSONArray(result );
JSONObject jsonObj1 = arr1.getJSONObject(0);
Image image = new Image();
for (int i = 0; i < arr1.length(); i++) {
JSONObject jobjj11 = arr1
.getJSONObject(i);
String name = jobjj11.optString("name");
Log.e("nurl",""+name);
image.setSmall(""+name);
images.add(image);
Log.e("images.tostring",""+images.toString());
}
}
else if (status.equals("false")){
Handler handler = new Handler(Looper.getMainLooper());
handler.post(
new Runnable() {
@Override
public void run() {
AlertDialog alertDialog = new AlertDialog.Builder(
GalleryActivity.this).create();
alertDialog.setMessage("No imagesFound");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
}
);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(
new Runnable() {
@Override
public void run() {
AlertDialog alertDialog = new AlertDialog.Builder(
GalleryActivity.this).create();
alertDialog.setMessage("Error Connecting to Server");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
});
//Log.e("ServiceHandler", "Couldn't get any data from the url");
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
这是我的 GalleryAdapter
public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.MyViewHolder> {
private List<Image> images;
private Context mContext;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView thumbnail;
public MyViewHolder(View view) {
super(view);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
}
}
public GalleryAdapter(Context context, List<Image> images) {
Log.e("ingalleryadapter", "aaddd");
mContext = context;
this.images = images;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.gallery_thumbnail, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Image image = images.get(position);
Glide.with(mContext).load(image.getSmall())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.thumbnail);
}
@Override
public int getItemCount() {
return images.size();
}
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private GalleryAdapter.ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final GalleryAdapter.ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
}
}
不显示图像。这有什么不对?
答案 0 :(得分:2)
尝试添加对override()的调用,看看是否修复了它。这种情况的一种常见方式是,如果您使用的视图不具有固定大小和/或取决于其内容的大小。 Glide试图等到没有固定大小的视图通过布局。在某些情况下,视图最终等待Glide和Glide最终等待视图,从而导致负载无声地永远不会启动。
如果覆盖有效,请在视图中考虑固定大小,或者将xml粘贴到此处,以便我们尝试建议其他替代方法。
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200) // resizes the image to these dimensions (in pixel). does not respect aspect ratio
.into(imageViewResize);
有关详细信息,请访问:Images fail to load in recyclerview #482
答案 1 :(得分:0)
检查图像列表是否有值。通过将日志放入getItemCount()方法并检查图像列表的大小来检查。
答案 2 :(得分:0)
我和Glide有类似的问题。我有2种方法使情况变得更好。
1.添加holder.itemview.setTag("smthng unique")
因为Glide使用标记来建立关系。
2.调整图像大小,如果图像的像素数量很大,有时无法加载,因此需要降级像素。
我希望这对你有用。