Android网格视图图像:显示文本

时间:2016-02-13 05:07:15

标签: android

我在片段中准备了一个显示图像的网格视图,当您点击它们时,它会在另一个活动中显示图像。我希望图像的名称显示在它下面,因为我采用了textview,但它显示了位置。如何在该textview上显示从网格视图中选择的照片名称。下面是代码片段。

full_image_layout.xml

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

    <ImageView android:id="@+id/full_image_view"
        android:layout_width="500dp"
        android:layout_height="500dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/image_title"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

FullImageFragment

package com.androidbelieve.HIT_APP;

import android.app.Activity;

/**
 * Created by Akash on 2/13/2016.
 */
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class FullImageFragment extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.full_image_layout);

        // get intent data
        Intent i = getIntent();

        // Selected image id
        int position = i.getExtras().getInt("id");
        ImageAdapter imageAdapter = new ImageAdapter(this);

        ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
        imageView.setImageResource(imageAdapter.mThumbIds[position]);
        TextView textView = (TextView) findViewById(R.id.image_title);
        textView.setText(String.valueOf(position));
    }

}

GalleryFragment

package com.androidbelieve.HIT_APP;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

/**
 * Created by Ratan on 7/29/2015.
 */
public class GalleryFragment extends Fragment {

    private GridView gridView;
    private ImageView imageView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View view = inflater.inflate(R.layout.fragment_gallery_grid, container,
                false);
        GridView gridView = (GridView) view.findViewById(R.id.grid_view);
        gridView.setAdapter(new ImageAdapter(view.getContext()));

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent(getActivity(),FullImageFragment.class);
                i.putExtra("id",position);
                startActivity(i);
            }
        });

        return view;
    }
}

请帮帮我!!!

5 个答案:

答案 0 :(得分:1)

ImageAdapter

package com.androidbelieve.HIT_APP;

/**
 * Created by Akash on 2/13/2016.
 */
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 java.util.ArrayList;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;


    // Keep all Images in array
    public Integer[] mThumbIds = {
            R.drawable.akash, R.drawable.akash,
            R.drawable.akash , R.drawable.akash,

    };

    // Constructor
    public ImageAdapter(Context c){
        mContext = c;
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
        return imageView;
    }

}

答案 1 :(得分:0)

  gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


            Intent i = new Intent(getActivity(),FullImageFragment.class);
            i.putExtra("id",position);
            i.putExtra("image_name",arrayImageName[position]); // get your selected image name and pass it
            startActivity(i);
        }
    });

在图像显示类中,

    String name = i.getExtras().getString("image_name");
    TextView textView = (TextView) findViewById(R.id.image_title);
    textView.setText(name);

答案 2 :(得分:0)

你可以这样做:

NSNull

在下一个活动中:

// Keep all Images in array
    public Integer[] mThumbIds = {
            R.drawable.akash, R.drawable.akash,
            R.drawable.akash , R.drawable.akash,

    };

 public String[] mThumbNames = {
           "akash 0", "akash 1","akash 2", "akash 3",

    };

修改1:

TextView textView = (TextView) findViewById(R.id.image_title);
textView.setText(imageAdapter.mThumbNames[position])

希望这会对你有所帮助。

答案 3 :(得分:0)

enter image description here

我想你想要这样的东西?

答案 4 :(得分:0)

Hiren Patel已经回答了这个问题。我只是把它分步。

1)创建'CustomClass',它将作为POJO类来构建您的图像名称,资源引用。 2)使用extends ArrayAdapter创建一个适配器并将'CustomClass'作为参数 3)在您的活动中,创建该类的列表并加载数据(图像名称,资源ID等) 4)用上面的列表初始化你的适配器(也可以是数组) 5)在适配器的getView方法上访问该值