在列表视图中查找 Imageview 的位置

Posted

技术标签:

【中文标题】在列表视图中查找 Imageview 的位置【英文标题】:Find position of an Imageview inside listview 【发布时间】:2017-06-29 23:17:11 【问题描述】:

我有一个列表视图,我通过 mysql 数据库填充行。

这是对我所做工作的看法:

您看到的这些名称来自我的数据库,而 PLAY ImageView 来自我在每一行中显示的布局列表(当我将名称添加到我的数据库中时)。

现在我的问题是当我为该播放按钮设置一个点击侦听器时,(目的是将其切换为暂停 ImageView 等)它也会从另一行更改另一个播放按钮,但不是全部其中。因此,我认为我没有得到这个职位,对吧?你能帮我解决这个问题吗!

这是我的片段:

public class XXXUpdatesFragment extends Fragment implements AsyncResponse
,AdapterView.OnItemClickListener

    View view ;
    private ArrayList<Product> productList;
    private ListView lvProduct;
    private FunDapter<Product> adapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        view = inflater.inflate(R.layout.xxx_updates_layout, container, false);
        ImageLoader.getInstance().init(UILConfig.config(getActivity()));

        lvProduct = (ListView)view.findViewById(R.id.lvProduct);

        PostResponseAsyncTask taskRead = new PostResponseAsyncTask(getActivity(), this);

        taskRead.execute("http://symphonyrecords.6te.net/product.php");

        return view;
     //onCreate


    @Override
    public void  processFinish(String s) 
        productList = new JsonConverter<Product>().toArrayList(s, Product.class);

        BindDictionary<Product> dict = new BindDictionary<Product>();
        dict.addStringField(R.id.tvName, new StringExtractor<Product>() 
            @Override
            public String getStringValue(Product product, int position) 
                return product.name;
            
        );

        dict.addStringField(R.id.tvDescription, new StringExtractor<Product>() 
            @Override
            public String getStringValue(Product product, int position) 
                return product.description;   
            
        );

        dict.addStringField(R.id.tvQty, new StringExtractor<Product>() 
            @Override
            public String getStringValue(Product product, int position) 
                return "" + product.qty;
            
        );


        adapter = new FunDapter<>(getActivity(), productList, R.layout.d_layout_list_d, dict);


        lvProduct.setAdapter(adapter);
        lvProduct.setOnItemClickListener(this);

    

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
        Product selectedProduct = productList.get(position);
        Toast.makeText(getActivity(),selectedProduct.name+"", Toast.LENGTH_SHORT).show();
       


    

这是我的产品类:

public class Product implements Serializable 
@SerializedName("pid")
public int pid;

@SerializedName("name")
public String name;

@SerializedName("qty")
public int qty;

@SerializedName("price")
public String description;

这是我的适配器:

public class FunDapter<T> extends BaseAdapter implements Filterable 



private boolean paused = true;


protected List<T> mDataItems;
protected List<T> mOrigDataItems;
protected LongExtractor<T> idExtractor;
protected final Context mContext;
private final int mLayoutResource;
private final BindDictionary<T> mBindDictionary;
private int oddColorRes;
private int evenColorRes;
private FunDapterFilter<T> funDapterFilter;
private Filter mFilter;


public FunDapter(Context context, List<T> dataItems, int layoutResource,
                 BindDictionary<T> dictionary) 
  this(context, dataItems, layoutResource, null, dictionary);


public FunDapter(Context context, List<T> dataItems, int layoutResource,
                 LongExtractor<T> idExtractor, BindDictionary<T> dictionary) 
    this.mContext = context;
    this.mDataItems = dataItems;
    this.mOrigDataItems = dataItems;
    this.mLayoutResource = layoutResource;
    this.idExtractor = idExtractor;
    this.mBindDictionary = dictionary;



public void updateData(List<T> dataItems) 
    this.mDataItems = dataItems;
    this.mOrigDataItems = dataItems;
    notifyDataSetChanged();


@Override
public int getCount() 
    if (mDataItems == null || mBindDictionary == null) return 0;

    return mDataItems.size();


@Override
public T getItem(int position) 
    return mDataItems.get(position);


@Override
public boolean hasStableIds() 
    if(idExtractor == null) return super.hasStableIds();
    else return true;


@Override
public long getItemId(int position) 
    if(idExtractor == null) return position;
    else return idExtractor.getLongValue(getItem(position), position);


@Override
public View getView(final int position, View convertView, ViewGroup parent) 
    View v = convertView;
    final GenericViewHolder holder;
    if (null == v) 
        LayoutInflater vi =
                (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(mLayoutResource, null);
        holder = new GenericViewHolder();
        holder.root = v;


        holder.playPause=(ImageView)v.findViewById(R.id.playPause); 
        holder.playPause.setOnClickListener(new View.OnClickListener() 

            @Override
            public void onClick(View arg0) 

                        if (paused) 
                            holder.playPause.setImageResource(R.drawable.ic_pause);
                            paused=false;
                        
                        else 
                            paused = true;
                            holder.playPause.setImageResource(R.drawable.ic_play);
                        

            );



        FunDapterUtils.initViews(v, holder, mBindDictionary);

        v.setTag(holder);


     else 
        holder = (GenericViewHolder) v.getTag();
    

    final T item = getItem(position);
    showData(item, holder, position);

    return v;



private void showData(T item, GenericViewHolder holder, int position) 

    if (oddColorRes > 0 && evenColorRes > 0) 
        if (position % 2 == 0) 
            holder.root.setBackgroundColor(mContext.getResources().getColor(evenColorRes));
         else 
            holder.root.setBackgroundColor(mContext.getResources().getColor(oddColorRes));
        
    

    FunDapterUtils.showData(item, holder, position, mBindDictionary);


public FunDapter<T> setAlternatingBackground(int oddColorRes, int evenColorRes) 

    if (oddColorRes <= 0 || evenColorRes <= 0) 
        throw new IllegalArgumentException("Color parameters are illegal");
    

    this.oddColorRes = oddColorRes;
    this.evenColorRes = evenColorRes;

    return this;


public void setIdExtractor(LongExtractor<T> idExtractor) 
    this.idExtractor = idExtractor;


@Override
public Filter getFilter() 
    return mFilter;


public void initFilter(FunDapterFilter<T> filter) 

    if (filter == null)
        throw new IllegalArgumentException("Cannot pass a null filter to FunDapter");

    this.funDapterFilter = filter;

    mFilter = new Filter() 

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) 

            List<T> list = (List<T>) results.values;

            if (results.count == 0) 
                resetData();
             else 
                mDataItems = list;
            

            notifyDataSetChanged();
        

        @Override
        protected FilterResults performFiltering(CharSequence constraint) 

            FilterResults results = new FilterResults();
            if (constraint == null || constraint.length() == 0) 

                results.values = mOrigDataItems;
                results.count = mOrigDataItems.size();
             else 

                List<T> filter =
                        funDapterFilter.filter(constraint.toString(), mOrigDataItems);

                results.count = filter.size();
                results.values = filter;

            

            return results;
          
      ;
  

  public void resetData() 
      mDataItems = mOrigDataItems;
     

  

我已经为此苦苦挣扎了一个星期,如果您能帮助我解决这个问题,我将不胜感激。

【问题讨论】:

如果有帮助,请查看***.com/questions/20611123/… @rabhis 答案可能会起作用(并且可能是解决您的问题的最快方法),但 Raghunandan 链接中的答案将是更好的方法。 【参考方案1】:
    holder.playPause=(ImageView)v.findViewById(R.id.playPause); 
    holder.playPause.setOnClickListener(new View.OnClickListener() 

        @Override
        public void onClick(View arg0) 

                    if (paused) 
                        holder.playPause.setImageResource(R.drawable.ic_pause);
                        paused=false;
                    
                    else 
                        paused = true;
                        holder.playPause.setImageResource(R.drawable.ic_play);
                    

        );

此块必须同时出现在 if 和 else 条件中。现在只有在创建视图时才能正确附加侦听器,并且在重用视图时代码无法附加侦听器。

    if (v==null) 
        //that code
     else 
        //that code 
    

【讨论】:

以上是关于在列表视图中查找 Imageview 的位置的主要内容,如果未能解决你的问题,请参考以下文章

获取列表视图中的图像视图位置

RecyclerView - 获取特定位置的视图

动画 2 个 imageViews 的位置切换

如何在 Baseadapter 中获得位置

如何自动中心裁剪ImageView Android?

Imageview 在滚动列表视图上自动更改