On a ListView item's child click

强颜欢笑 提交于 2019-12-24 16:17:03

问题


I have a ListView where each of its items is composed from some ImageViews and TextViews, i want that when i click on a specefic ImageView, some code will be executed, where should i put this code is it in onItemClick method?

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();
}

the getView code:

 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;
}

回答1:


In the UploadedAdapter in getView() you must be creating the object of the ImageViews.

Just set the onClickListener to the objects of that ImageViews and handle the click event in the onClick on the basis of the id of each ImageView.




回答2:


You put this code in

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

}

or you can write something like that

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);
              }
            });
      }

    });



回答3:


Lets say your getView has a textView and a 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);
        }
    });
}

Now in Activity or Fragment

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();
        }
    }
});


来源:https://stackoverflow.com/questions/16674000/on-a-listview-items-child-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!