我有一个 ListView,其中每个项目都由一些
ImageViews
和 TextViews
组成,
我希望当我点击特定的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;
}
假设您的
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);
}
});
}
现在在
Activity
或 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();
}
}
});
在
UploadedAdapter
中的 getView()
中,您必须创建 ImageViews
的对象。
只需将
onClickListener
设置为该ImageViews
的对象,并根据每个onClick
的id处理ImageView
中的点击事件即可。
您将此代码放入
@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);
}
});
}
});