3

我有一个 ListView ,其中每个项目都是由一些组成的ImageViewsTextViews我希望当我点击一个特定的ImageView,一些代码将被执行,我应该把这个代码放在哪里onItemClick

public class EspaceClientUplodedProducts extends Activity implements OnItemClickListener{

List<Produit> lesProduits= new ArrayList<Produit>();
ListView lvListe;
ProductsDataSource produitSource;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.espaceclientuploads);

    produitSource=new ProductsDataSource(this);
    produitSource.open();
    lesProduits=produitSource.getAllUploads();

    lvListe= (ListView)findViewById(R.id.UploadListView);
    UploadedAdapter adapter = new UploadedAdapter(this, lesProduits);
    lvListe.setAdapter(adapter);
    lvListe.setOnItemClickListener(this);
}



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

}


@Override
protected void onPause() {
    produitSource.close();
    super.onPause();
}

getView 代码:

 public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    if (convertView==null)
    {
        holder=new ViewHolder();
        convertView = inflater.inflate(R.layout.espaceclientuploadsource, null);
        holder.nomduProduit = (TextView)convertView.findViewById(R.id.UploadedProductName);
        holder.prixDuProduit = (TextView)convertView.findViewById(R.id.UplodedProductPrice);
        holder.imageDuProduit = (ImageView)convertView.findViewById(R.id.Uplodimage);
        holder.status = (ImageView)convertView.findViewById(R.id.UploadedStatus);
        holder.delete=(ImageView)convertView.findViewById(R.id.ImageViewDelete);
        convertView.setTag(holder);
    }

    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    Bitmap bitmapImage = BitmapFactory.decodeFile(path+File.separator+lesProduits.get(position).getImage());

    Drawable drawableImage = new BitmapDrawable(bitmapImage);
    holder.imageDuProduit.setImageDrawable(drawableImage);
    holder.nomduProduit.setText(lesProduits.get(position).getNomDuProduit());
    holder.prixDuProduit.setText(lesProduits.get(position).getPrixDuProduit());
    holder.delete.setImageResource(R.drawable.delete);

    switch (lesProduits.get(position).getStatus())
    {

    case 3://annonce Accepté
    holder.status.setImageResource(R.drawable.accepte); 
    break;

    case 2://annonce en Attente
    holder.status.setImageResource(R.drawable.enattente);   
    break;

    case 1://annonce refusé
    holder.status.setImageResource(R.drawable.refuse);
    break;

    case 0://produit vendu
    holder.status.setImageResource(R.drawable.vendu);
    break;
    }
    return convertView;
}
4

3 回答 3

2

假设你getView有一个textView和一个imageView

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.xml, parent, false);
    // setup view bindings
    TextView textView = view.findViewById(R.id.textView);
    ImageView imageView = view.findViewById(R.id.imageView);

    setClickListener(textView, position, parent);
    setClickListener(imageView, position, parent);
}

private void setClickListener(View view, final int position, final ViewGroup parent){
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // this part is important, it lets ListView handle the clicks
            ((ListView) parent).performItemClick(v, position, 0);
        }
    });
}

现在在ActivityFragment

ListView listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        long viewId = view.getId();
        if (viewId == R.id.textView) {
            Toast.makeText(Activity.this, "TextView Clicked", Toast.LENGTH_SHORT).show();
        } else if (viewId == R.id.imageView) {
            Toast.makeText(Activity.this, "ImageView Clicked", Toast.LENGTH_SHORT).show();
        }
    }
});
于 2019-01-24T22:28:17.240 回答
1

你把这段代码放进去

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

}

或者你可以写类似的东西

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, final View view,
          int position, long id) {
        final String item = (String) parent.getItemAtPosition(position);
        view.animate().setDuration(2000).alpha(0)
            .withEndAction(new Runnable() {
              @Override
              public void run() {
                list.remove(item);
                adapter.notifyDataSetChanged();
                view.setAlpha(1);
              }
            });
      }

    });
于 2013-05-21T15:47:03.033 回答
1

UploadedAdapterin 中,getView()您必须创建ImageViews.

只需将 设置onClickListener为 that 的对象并根据 each 的 idImageViews处理中的单击事件。onClickImageView

于 2013-05-21T15:47:10.273 回答