我一直在试图让这个工作失败,但我无法挽救我的生命,我希望你们其中一个人知道该怎么做。基本上,我有一个在惰性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;
}
}
答案 0 :(得分:2)
您可以为Adapter
适配器创建自定义ListView
,并在每个包含您想要点击的图片的OnCLickListeners
中添加ImageView
。有关创建自定义适配器的帮助,请参阅here。
像这样(在自定义适配器中):
@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);
}
答案 1 :(得分:2)
你有50个不同的特定子活动用于一个通用的工作...让我们解决这个和我们将为每一行中的两个图像提供一个点击监听器。
要开始,您可以这样定义这些JPG的String数组:
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
反之亦然。这不是完美的,但它应该让你朝着正确的方向前进。希望有所帮助!
答案 2 :(得分: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();
}
});