gpt4 book ai didi

java - 加载完整图像 Recyclerview Cardview

转载 作者:行者123 更新时间:2023-12-01 19:03:07 25 4
gpt4 key购买 nike

感谢@Mouaad Abdelghafour AITALI。我这样做是为了获得新的完整 ImageView 。不幸的是它没有显示完整的图像。我哪里可能出错了?我已经发布了 Main Activity 和 FullImageViewer Java 代码以及完整图像的 Xml。请指出我正确的方向。所有依赖关系都正常。

Recyclerview Cardview Full Image View

主要 Activity

package com.ny.fetchallimages;

import android.content.Intent;
import android.os.Bundle;
import org.json.JSONArray;
import java.util.ArrayList;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import java.util.List;

import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

List<DataAdapter> ListOfdataAdapter;

RecyclerView recyclerView;

String HTTP_JSON_URL = "http://ny.com/uploaddownload/fetchallimages.php";

String Image_URL_JSON = "image_data";

String Image_Name_JSON = "image_tag";

String Image_Time_JSON = "time";

JsonArrayRequest RequestOfJSonArray ;

RequestQueue requestQueue ;

View view ;

int RecyclerViewItemPosition ;

RecyclerView.LayoutManager layoutManagerOfrecyclerView;

RecyclerView.Adapter recyclerViewadapter;

ArrayList<String> ImageTitleNameArrayListForClick;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ImageTitleNameArrayListForClick = new ArrayList<>();

ListOfdataAdapter = new ArrayList<>();

recyclerView = (RecyclerView) findViewById(R.id.recyclerview1);

recyclerView.setHasFixedSize(true);

layoutManagerOfrecyclerView = new LinearLayoutManager(this);

recyclerView.setLayoutManager(layoutManagerOfrecyclerView);

JSON_HTTP_CALL();

// Implementing Click Listener on RecyclerView.
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {

GestureDetector gestureDetector = new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener() {

@Override public boolean onSingleTapUp(MotionEvent motionEvent) {

return true;
}

});
@Override
public boolean onInterceptTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {
view = Recyclerview.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
if(view != null && gestureDetector.onTouchEvent(motionEvent)) {
//Getting RecyclerView Clicked Item value.
RecyclerViewItemPosition = Recyclerview.getChildAdapterPosition(view);
Intent intent = new Intent(MainActivity.this, FullImageViewer.class );
intent.putExtra("img", "HTTP_JSON_URL");
startActivity(intent);

}
return false;
}
@Override
public void onTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {

}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
}
public void JSON_HTTP_CALL(){
RequestOfJSonArray = new JsonArrayRequest(HTTP_JSON_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
ParseJSonResponse(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});
requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(RequestOfJSonArray);
}
public void ParseJSonResponse(JSONArray array){
for(int i = 0; i<array.length(); i++) {

DataAdapter GetDataAdapter2 = new DataAdapter();

JSONObject json = null;
try {
json = array.getJSONObject(i);

GetDataAdapter2.setImageTitle(json.getString(Image_URL_JSON));
// Adding image title name in array to display on RecyclerView click event.
ImageTitleNameArrayListForClick.add(json.getString(Image_URL_JSON));
GetDataAdapter2.setImageUrl(json.getString(Image_URL_JSON));
} catch (JSONException e) {
e.printStackTrace();
}
ListOfdataAdapter.add(GetDataAdapter2);
}
recyclerViewadapter = new RecyclerViewAdapter(ListOfdataAdapter, this);
recyclerView.setAdapter(recyclerViewadapter);
}
}

全图像浏览器

package com.ny.fetchallimages;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;

public class FullImageViewer extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullimageviewer);
final ImageView img = findViewById(R.id.img);
String url = (String) getIntent().getSerializableExtra("img");

//Glide library to load image from URL
Glide.with(getApplicationContext())
.asBitmap()
.load(url)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
img.setImageBitmap(resource);
}

@Override
public void onLoadCleared(@Nullable Drawable placeholder) {

}
});
}
}

FULLIMAGEVIEWER.XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="@color/color1">


<ImageView
android:id="@+id/img"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true" />
</RelativeLayout>

最佳答案

创建一个新的空 Activity 并使用以下内容命名(例如FullImageViewer):

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true" />
</RelativeLayout>

JAVA:

public class FullImageViewer extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullimageviewer);
ImageView img = findViewById(R.id.img);
String url = (String) getIntent().getSerializableExtra("img");

//Glide library to load image from URL
Glide.with(getApplicationContext())
.asBitmap()
.load(url)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
img.setImageBitmap(resource);
}

@Override
public void onLoadCleared(@Nullable Drawable placeholder) {

}
});
}
}

在项目点击事件中,您需要将 URL 传递给 FullImageViewer Activity

 if(view != null && gestureDetector.onTouchEvent(motionEvent)) {
Intent intent = new Intent(MainActivity.this, FullImageViewer.class);
intent.putExtra("img", "YOUR IMAGE URL HERE");
startActivity(intent);
}
<小时/>

在您的 Gradle 文件中添加以下行:

  implementation 'com.github.bumptech.glide:glide:4.10.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'

关于java - 加载完整图像 Recyclerview Cardview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59604195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com