0

我一直在努力让这个工作,但我无法挽救我的生命,我希望你们中的一个人知道如何去做。基本上,我有一个在惰性 ListView 中加载的自然图片列表,但我的问题是我只能使该行可点击。我需要使单个图像可点击。其他一切都像魅力一样,我非常感谢您的帮助。

活动:

public class AmainActivityNature extends Activity {

    ListView list;
    LazyAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        list=(ListView)findViewById(R.id.list);
        adapter=new LazyAdapter(this, mStrings, mphotos);
        list.setAdapter(adapter);

        Button bLeft = (Button) findViewById(R.id.buttonLeft);
        bLeft.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent mainIntentOne = new Intent(AmainActivityNature.this, AmainActivityNature.class);
                AmainActivityNature.this.startActivity(mainIntentOne);

                /* Finish splash activity so user cant go back to it. */
                AmainActivityNature.this.finish();

                /* Apply our splash exit (fade out) and main
                   entry (fade in) animation transitions. */
                overridePendingTransition(R.anim.animation_enterl, R.anim.animation_leaver);


            }   
            });
        Button bRight = (Button) findViewById(R.id.buttonRight);
        bRight.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent("com.testing.image.AMAINACTIVITYNATURE"));
                finish();
            }
            });

list.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                 switch (arg2)
                 {
                    case 0:
                     Intent newActivity0 = new Intent(AmainActivityNature.this,NatureTwoOne.class);     
                     startActivity(newActivity0);
                    break;
                    case 1:
                     Intent newActivity1 = new Intent(AmainActivityNature.this,NatureTwoTwo.class);     
                     startActivity(newActivity1);
                    break;
                    case 2:
                     Intent newActivity2 = new Intent(AmainActivityNature.this,NatureTwoThree.class);     
                     startActivity(newActivity2);
                    break;
                    case 3:
                     Intent newActivity3 = new Intent(AmainActivityNature.this,NatureTwoFour.class);     
                     startActivity(newActivity3);
                    break;
                    case 4:
                         Intent newActivity4 = new Intent(AmainActivityNature.this,NatureTwoFive.class);     
                         startActivity(newActivity4);
                        break;
                        case 5:
                         Intent newActivity5 = new Intent(AmainActivityNature.this,NatureTwoSix.class);     
                         startActivity(newActivity5);
                        break;
                        case 6:
                         Intent newActivity6 = new Intent(AmainActivityNature.this,NatureTwoSeven.class);     
                         startActivity(newActivity6);
                        break;
                        case 7:
                         Intent newActivity7 = new Intent(AmainActivityNature.this,NatureTwoEight.class);     
                         startActivity(newActivity7);
                        break;
                        case 8:
                             Intent newActivity8 = new Intent(AmainActivityNature.this,NatureTwoNine.class);     
                             startActivity(newActivity8);
                            break;
                            case 9:
                             Intent newActivity9 = new Intent(AmainActivityNature.this,NatureTwoTen.class);     
                             startActivity(newActivity9);
                            break;
                            case 10:
                             Intent newActivity10 = new Intent(AmainActivityNature.this,NatureTwoEleven.class);     
                             startActivity(newActivity10);
                            break;
                            case 11:
                             Intent newActivity11 = new Intent(AmainActivityNature.this,NatureTwoTwoelve.class);     
                             startActivity(newActivity11);
                            break;
                            case 12:
                                 Intent newActivity12 = new Intent(AmainActivityNature.this,NatureTwoThirteen.class);     
                                 startActivity(newActivity12);
                                break;
                                case 13:
                                 Intent newActivity13 = new Intent(AmainActivityNature.this,NatureTwoFourteen.class);     
                                 startActivity(newActivity13);
                                break;
                                case 14:
                                 Intent newActivity14 = new Intent(AmainActivityNature.this,NatureTwoFifteen.class);     
                                 startActivity(newActivity14);
                                break;
                                case 15:
                                 Intent newActivity15 = new Intent(AmainActivityNature.this,NatureTwoSixteen.class);     
                                 startActivity(newActivity15);
                                break;
                                case 16:
                                     Intent newActivity16 = new Intent(AmainActivityNature.this,NatureTwoSeventeen.class);     
                                     startActivity(newActivity16);
                                    break;
                                    case 17:
                                     Intent newActivity17 = new Intent(AmainActivityNature.this,NatureTwoEighteen.class);     
                                     startActivity(newActivity17);
                                    break;
                                    case 18:
                                     Intent newActivity18 = new Intent(AmainActivityNature.this,NatureTwoNineteen.class);     
                                     startActivity(newActivity18);
                                    break;
                                    case 19:
                                         Intent newActivity19 = new Intent(AmainActivityNature.this,NatureTwoTwoenty.class);     
                                         startActivity(newActivity19);
                                        break;
                                        case 20:
                                         Intent newActivity20 = new Intent(AmainActivityNature.this,NatureTwoTwoentyone.class);     
                                         startActivity(newActivity20);
                                        break;
                                        case 21:
                                         Intent newActivity21 = new Intent(AmainActivityNature.this,NatureTwoTwoentytwo.class);     
                                         startActivity(newActivity21);
                                        break;
                                        case 22:
                                         Intent newActivity22 = new Intent(AmainActivityNature.this,NatureTwoTwoentythree.class);     
                                         startActivity(newActivity22);
                                        break;
                                        case 23:
                                             Intent newActivity23 = new Intent(AmainActivityNature.this,NatureTwoTwoentyfour.class);     
                                             startActivity(newActivity23);
                                            break;
                                            case 24:
                                             Intent newActivity24 = new Intent(AmainActivityNature.this,NatureTwoTwoentyfive.class);     
                                             startActivity(newActivity24);
                                            break;
                    default:
                      // Nothing do!
                  }
            }
        });

        Button b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(listener);
    }

    @Override
    public void onDestroy()
    {
        list.setAdapter(null);
        super.onDestroy();
    }

    public OnClickListener listener=new OnClickListener(){
        @Override
        public void onClick(View arg0) {
            adapter.imageLoader.clearCache();
            adapter.notifyDataSetChanged();
        }
    };

    private String[] mphotos={
            "http:testingsite.com/naturemain/2.jpg",
            "http:testingsite.com/naturemain/4.jpg",
            "http:testingsite.com/naturemain/6.jpg",
            "http:testingsite.com/naturemain/8.jpg",
            "http:testingsite.com/naturemain/10.jpg",
            "http:testingsite.com/naturemain/12.jpg",
            "http:testingsite.com/naturemain/14.jpg",
            "http:testingsite.com/naturemain/16.jpg",
            "http:testingsite.com/naturemain/18.jpg",
            "http:testingsite.com/naturemain/20.jpg",
            "http:testingsite.com/naturemain/22.jpg",
            "http:testingsite.com/naturemain/24.jpg",
            "http:testingsite.com/naturemain/26.jpg",
            "http:testingsite.com/naturemain/28.jpg",
            "http:testingsite.com/naturemain/30.jpg",
            "http:testingsite.com/naturemain/32.jpg",
            "http:testingsite.com/naturemain/34.jpg",
            "http:testingsite.com/naturemain/36.jpg",
            "http:testingsite.com/naturemain/38.jpg",
            "http:testingsite.com/naturemain/40.jpg",
            "http:testingsite.com/naturemain/42.jpg",
            "http:testingsite.com/naturemain/44.jpg",
            "http:testingsite.com/naturemain/46.jpg",
            "http:testingsite.com/naturemain/48.jpg",
            "http:testingsite.com/naturemain/50.jpg"
    };

    private String[] mStrings={
            "http:testingsite.com/naturemain/1.jpg",
            "http:testingsite.com/naturemain/3.jpg",
            "http:testingsite.com/naturemain/5.jpg",
            "http:testingsite.com/naturemain/7.jpg",
            "http:testingsite.com/naturemain/9.jpg",
            "http:testingsite.com/naturemain/11.jpg",
            "http:testingsite.com/naturemain/13.jpg",
            "http:testingsite.com/naturemain/15.jpg",
            "http:testingsite.com/naturemain/17.jpg",
            "http:testingsite.com/naturemain/19.jpg",
            "http:testingsite.com/naturemain/21.jpg",
            "http:testingsite.com/naturemain/23.jpg",
            "http:testingsite.com/naturemain/25.jpg",
            "http:testingsite.com/naturemain/27.jpg",
            "http:testingsite.com/naturemain/29.jpg",
            "http:testingsite.com/naturemain/31.jpg",
            "http:testingsite.com/naturemain/33.jpg",
            "http:testingsite.com/naturemain/35.jpg",
            "http:testingsite.com/naturemain/37.jpg",
            "http:testingsite.com/naturemain/39.jpg",
            "http:testingsite.com/naturemain/41.jpg",
            "http:testingsite.com/naturemain/43.jpg",
            "http:testingsite.com/naturemain/45.jpg",
            "http:testingsite.com/naturemain/47.jpg",
            "http:testingsite.com/naturemain/49.jpg"
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        super.onCreateOptionsMenu(menu);
        MenuInflater menuinflater = getMenuInflater();
        menuinflater.inflate(R.menu.main_menu, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
                switch(item.getItemId()){
                case R.id.inflate:

                    startActivity(new Intent("com.testing.image.ABOUTUS"));
                    return true;
                }

                return false;
            }
}

行 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="150dip"
        android:layout_height="120dip"
        android:scaleType="centerCrop"
        android:src="@drawable/backgroundimage" />

    <TextView
        android:layout_width="20dip"
        android:layout_height="1dip" />

    <TextView
        android:layout_width="1dip"
        android:layout_height="150dip" />

    <ImageView
        android:id="@+id/photo"
        android:layout_width="150dip"
        android:layout_height="120dip"
        android:scaleType="centerCrop"
        android:src="@drawable/stub" />

    <TextView
        android:id="@+id/text"
        android:layout_width="1dip"
        android:layout_height="1dip" />

</LinearLayout>

主要 XML:

<ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_weight="1"
        />

子活动:

public class NatureTwoOne extends Activity {

    ImageView image;
    private class BackgroundTask extends AsyncTask<String, Void, Bitmap> {
        protected Bitmap doInBackground(String...url) {
            //--- download an image ---
            Bitmap bitmap = DownloadImage(url[0]);
            return bitmap;
        }
        protected void onPostExecute(Bitmap bitmap) {
            ImageView image = (ImageView) findViewById(R.id.imageView1);
            bitmaptwo=bitmap;
            image.setImageBitmap(bitmap);
        }
    }

    private InputStream OpenHttpConnection(String urlString) throws IOException {
        InputStream in = null;
        int response= -1;
        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();
        if (!(conn instanceof HttpURLConnection ))
            throw new IOException("Not an HTTP connection");
        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK){
                in = httpConn.getInputStream();
            }
        }
        catch (Exception ex) {
            throw new IOException("Error connecting");
        }
        return in;
    }

    private Bitmap DownloadImage(String URL)
    {
        Bitmap bitmap = null;
        InputStream in = null;
        try {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        }
        catch (IOException e1){
            Toast.makeText(this,e1.getLocalizedMessage(),
                    Toast.LENGTH_LONG).show();

            e1.printStackTrace();
        }
        return bitmap;
    }
    public static Bitmap bitmaptwo;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.wallpaper);
        new BackgroundTask().execute("http:testingsite.com/natureone/1.jpg");
        Button setWallpaper = (Button) findViewById(R.id.button3);
        setWallpaper.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                WallpaperManager wManager;
                Toast noSong = Toast.makeText(NatureTwoOne.this, "Background Set", Toast.LENGTH_SHORT);
                noSong.show();
                try {
                    // bitmap = BitmapFactory.decodeFile(null);
                    wManager = WallpaperManager.getInstance(getApplicationContext());
                    wManager.setBitmap(bitmaptwo);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

        Button bLeft = (Button) findViewById(R.id.buttonLeft);
        bLeft.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent mainIntentOne = new Intent(NatureTwoOne.this, NatureTwoFifty.class);
                NatureOneOne.this.startActivity(mainIntentOne);
                if(bitmaptwo != null){
                    bitmaptwo.recycle();
                }                              
                /* Finish splash activity so user cant go back to it. */
                NatureOneOne.this.finish();

                /* Apply our splash exit (fade out) and main
                   entry (fade in) animation transitions. */
                overridePendingTransition(R.anim.animation_enterl, R.anim.animation_leaver);
            }   
        });
        Button bRight = (Button) findViewById(R.id.buttonRight);
        bRight.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                startActivity(new Intent("com.testing.image.NATURETWOTWO"));
                if(bitmaptwo != null){
                    bitmaptwo.recycle();
                }

                finish();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if(bitmaptwo != null){
                bitmaptwo.recycle();
            }
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        super.onCreateOptionsMenu(menu);
        MenuInflater menuinflater = getMenuInflater();
        menuinflater.inflate(R.menu.main_menu, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case R.id.inflate:
            startActivity(new Intent("com.testing.image.ABOUTUS"));
            return true;
        }
        return false;
    }
}

适配器:

public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private String[] data;
    private String[] dataone;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader;

    public LazyAdapter(Activity a, String[] d, String[] mphotos) {
        activity = a;
        data=d;
        dataone = mphotos;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.length;
    }

    public int getCountOne() {
        return dataone.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.item, null);

        TextView text=(TextView)vi.findViewById(R.id.text);;
        ImageView image=(ImageView)vi.findViewById(R.id.image);
        ImageView photos=(ImageView)vi.findViewById(R.id.photo);
        text.setText("item "+position);
        imageLoader.DisplayImage(data[position], image);
        imageLoader.DisplayImage(dataone[position], photos);
        return vi;
    }
}
4

3 回答 3

2

Adapter您可以为您的ListView适配器创建一个自定义并添加OnCLickListeners每个ImageView包含您希望可点击的图像的图像。有关创建自定义适配器的帮助,请参见此处

像这样的东西(在您的自定义适配器内):

@Override
public View getView(int position, View inView, ViewGroup parent) {

View v = inView;
ViewHolder viewHolder; //Use a viewholder for sufficent use of the listview

if (v == null) {
    LayoutInflater inflater = (LayoutInflater) adaptersContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inflater.inflate(R.layout.list_item, null);
    viewHolder = new ViewHolder();
    viewHolder.image = (ImageView) v.findViewById(R.id.ImageView);
    v.setTag(viewHolder);
} else {
    viewHolder = (ViewHolder) v.getTag();
}

    .....

viewHolder.image.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        //Click on imageView
    }i
});

v.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        //Click on listView row, or use an OnItemClickListener as you do already, it will have the same effect
    }
});

    .....

return (v);
}
于 2012-07-25T17:28:14.517 回答
2

对于一个通用的工作,您有 50 个不同的特定子活动......让我们解决这个问题我们将为每行中的两个图像提供一个点击侦听器。

首先,您可以这样定义这些 JPG 字符串数组:

public String[] mPhotos = new String[25];
public String[] mStrings = new String[25];

public void onCreate(Bundle savedInstances) {
    ...

    String prefix = "http:testingsite.com/natureone/";
    String suffix = ".jpg";
    for(int index = 1; index <= 25; index++) {
        mPhotos[index - 1] = prefix + (index * 2) + suffix; // evens: 2 - 50
        mStrings[index - 1] = prefix + (index * 2 - 1) + suffix; // odds: 1 - 49
    }
}

如果您曾经添加更多图像或更改内容,那么您现在的维护工作就会减少。

接下来创建适当的 Intent,让我们在 LazyAdapter 中创建一个通用的 OnClickListener 作为类变量:

OnClickListener mListener = new OnClickListener() {
    public void onClick(View view) {
        Intent intent = new Intent(activity, ViewActivity.class);
        intent.putExtra("Image Index", (Integer) view.getTag());
        activity.startActivity(intent);
    }
};

并在您的 LazyAdapter 中更改 getView():

...
text.setText("item "+position);

imageLoader.DisplayImage(data[position], image);
image.setTag(position + 1);
image.setOnClickListener(mListener);

imageLoader.DisplayImage(dataone[position], photos);
photos.setTag(position + 2);
photos.setOnClickListener(mListener);

return vi;

现在在您的子活动之一中更改这些并调用 ViewActivity(不要忘记更新您的清单!):

new BackgroundTask().execute("http:testingsite.com/natureone/1.jpg");

变成:

mIndex = getIntent().getIntExtra("Image Index", 1);
new BackgroundTask().execute("http:testingsite.com/natureone/" + mIndex + ".jpg");

bLeft可以变成:

bLeft.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        mIndex--;
        if(mIndex < 1)
            mIndex = 50;
        new BackgroundTask().execute("http:testingsite.com/natureone/" + mIndex + ".jpg");
    }
});

反之亦然bRight。这不会是完美的,但它应该让你朝着正确的方向前进。希望有帮助!

于 2012-07-25T19:57:00.060 回答
1

您应该覆盖 OnItemClickListener 方法而不是 OnClickListener

listView1.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        Toast.makeText(cxt, "You selected Item " + (position + 1), Toast.LENGTH_SHORT).show();
    }
});
于 2012-07-25T17:38:44.733 回答