我正在尝试新的活动,意图显示图像和名称,但我不知道如何将监听器添加到代码中,有人会帮助我如何将监听器放入代码中?我想点击detailsActivity视图,我在下面有detailsActivity.xml。
MainActivity.java
package net.simplifiedcoding.androidcustomgridview;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
//Web api url
public static final String DATA_URL = "http://www.simplifiedcodingreaders.16mb.com/superheroes.php";
//Tag values to read from json
public static final String TAG_IMAGE_URL = "image";
public static final String TAG_NAME = "name";
//GridView Object
private GridView gridView;
//ArrayList for Storing image urls and titles
private ArrayList<String> images;
private ArrayList<String> names;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
images = new ArrayList<>();
names = new ArrayList<>();
gridView = (GridView) findViewById(R.id.gridView);
gridView.setOnItemClickListener(this);
//Calling the getData method
getData();
}
private void getData(){
//Showing a progress dialog while our app fetches the data from url
final ProgressDialog loading = ProgressDialog.show(this, "Please wait...","Fetching data...",false,false);
//Creating a json array request to get the json from our api
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(DATA_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Dismissing the progressdialog on response
loading.dismiss();
//Displaying our grid
showGrid(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);
//Creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding our request to the queue
requestQueue.add(jsonArrayRequest);
}
private void showGrid(JSONArray jsonArray){
//Looping through all the elements of json array
for(int i = 0; i<jsonArray.length(); i++){
//Creating a json object of the current index
JSONObject obj = null;
try {
//getting json object from current index
obj = jsonArray.getJSONObject(i);
//getting image url and title from json object
images.add(obj.getString(TAG_IMAGE_URL));
names.add(obj.getString(TAG_NAME));
} catch (JSONException e) {
e.printStackTrace();
}
}
//Creating GridViewAdapter Object
GridViewAdapter gridViewAdapter = new GridViewAdapter(this,images,names);
//Adding adapter to gridview
gridView.setAdapter(gridViewAdapter);
}
}
GridViewApapter.java
package net.simplifiedcoding.androidcustomgridview;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import java.util.ArrayList;
public class GridViewAdapter extends BaseAdapter {
//Imageloader to load images
private ImageLoader imageLoader;
//Context
private Context context;
//Array List that would contain the urls and the titles for the images
private ArrayList<String> images;
private ArrayList<String> names;
public GridViewAdapter (Context context, ArrayList<String> images, ArrayList<String> names){
//Getting all the values
this.context = context;
this.images = images;
this.names = names;
}
@Override
public int getCount() {
return images.size();
}
@Override
public Object getItem(int position) {
return images.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Creating a linear layout
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
//NetworkImageView
NetworkImageView networkImageView = new NetworkImageView(context);
//Initializing ImageLoader
imageLoader = CustomVolleyRequest.getInstance(context).getImageLoader();
imageLoader.get(images.get(position), ImageLoader.getImageListener(networkImageView, R.mipmap.ic_launcher, android.R.drawable.ic_dialog_alert));
//Setting the image url to load
networkImageView.setImageUrl(images.get(position),imageLoader);
//Creating a textview to show the title
TextView textView = new TextView(context);
textView.setText(names.get(position));
//Scaling the imageview
networkImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
networkImageView.setLayoutParams(new GridView.LayoutParams(200,200));
//Adding views to the layout
linearLayout.addView(textView);
linearLayout.addView(networkImageView);
//Returnint the layout
return linearLayout;
}
}
我的detailsActivity xml
<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="wrap_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="net.simplifiedcoding.imageuploadsample.detailsActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageViewFull"
android:layout_alignParentTop="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/TextView"
android:text="text"
/>
</RelativeLayout>
答案 0 :(得分:0)
您只需在网格视图上添加ItemClickListener,即可在网格视图中获取所点击项目的位置。在其中,您可以打开“详细信息活动”并使用意图传递数据。
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});}
你会找到一个很好的例子here。
答案 1 :(得分:0)
您需要在Intents的帮助下将图片网址和图片名称传递到您的网格视图点击事件的详情活动: -
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent intent = new Intent(getBaseContext(),
DetailActivity.class);
intent.putExtra("ImageName", names.get(arg2));
intent.putExtra("ImageURL", images.get(arg2));
startActivity(intent);
}
});
,您的详细活动应如下所示: -
public class DetailActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activty_detail);
Bundle extras = getIntent().getExtras();
if (extras != null) {
((TextView) findViewById(R.id.TextView)).setText(extras
.getString("ImageName"));
ImageLoader imageLoader = CustomVolleyRequestQueue
.getInstance(this).getImageLoader();
// If you are using normal ImageView
imageLoader.get(extras.getString("ImageURL"), new ImageListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Log.e(TAG, "Image Load Error: " + error.getMessage());
}
@Override
public void onResponse(ImageContainer response, boolean arg1) {
if (response.getBitmap() != null) {
// load image into imageview
((ImageView) findViewById(R.id.imageViewFull))
.setImageBitmap(response.getBitmap());
}
}
});
}
}
}
我已经测试过您的代码显然您使用的基本网址存在问题我已经对其进行了增强并在GitHub上进行了更新,您可以在此处找到它
https://github.com/hiteshsahu/Volley-Image-Loading-Sample
由于您的网址不正确,我使用示例网址来显示此类图片
和详细活动看起来像这样
我强烈建议您使用片段而不是活动。希望它能帮到你