动态ListView有几个项目响应点击

时间:2013-12-19 12:50:06

标签: android listview dynamic android-listview

我有一个ListView,其中包含一个自定义适配器,当用户单击列表项时,该适配器会动态更改和扩展内容。当你向下滚动时,我的列表也会填充。

这是我的问题:每当我点击一个列表项时(此时我只是更改列表项的高度)它按预期工作,但当我向下滚动时,多个列表项会受到影响。假设我单击id为1的列表项,然后列出id为1,7,15的列表项等响应触摸事件,这也发生在列表填充更多数据之前,这不是我的问题所在。

MainFragment:

     @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ArrayList<EventObject> eo = new ArrayList<EventObject>();
    adapter = new ListAdapterMain(getActivity(), eo);

}
     @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.listfragment, container, false);
    lv = (ListView) rootView.findViewById(R.id.listViewListFragment);
    lv.setOnScrollListener(onScroll);
    lv.setAdapter(adapter);


    AsyncFetch async = new AsyncFetch();
    async.execute();

    lv.setOnItemClickListener(onClickList);

    return rootView;
}

    AdapterView.OnItemClickListener onClickList = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {

        Log.i("ID", "id: " + id);
        AbsListView.LayoutParams lvPars = (AbsListView.LayoutParams) v.getLayoutParams();
        lvPars.height = 500;
        v.setLayoutParams(lvPars);
        v.requestLayout();

    }
};

ListAdapter:

    public class ListAdapterMain extends ArrayAdapter<EventObject> {
private Context context;
private ArrayList<EventObject> oItems;

public ListAdapterMain(Context context, ArrayList<EventObject> objects) {
    super(context, R.layout.list_item_mainlist, objects);
    this.context = context;
    this.oItems = objects;
}


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

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(convertView == null){
    convertView = inflater.inflate(
            R.layout.list_item_mainlist, null);

    ViewHolder viewHolder = new ViewHolder();

    viewHolder.artist = (TextView) convertView
            .findViewById(R.id.textViewListItemArtist);
    viewHolder.venue = (TextView) convertView
            .findViewById(R.id.textViewListItemVenue);
    viewHolder.date = (TextView) convertView
            .findViewById(R.id.textViewListItemDate);
    viewHolder.bandPic = (ImageView) convertView.findViewById(R.id.imageViewListItemArtist);
    viewHolder.venuePic = (ImageView) convertView.findViewById(R.id.imageViewListItemVenue);

    convertView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) convertView.getTag();

String thisArtist = oItems.get(position).getArtist();
String thisVenue = oItems.get(position).getVenue();
String thisDate = oItems.get(position).getDate();
String thisArtistPic = oItems.get(position).getArtistPic();
String thisVenuePic = oItems.get(position).getVenuePic();

try {
    //Tue, 17 Sep 2013 11:34:01
    Date inputDate = new SimpleDateFormat("EEE, d MMM yyyy H:m:s", Locale.ENGLISH).parse(oItems.get(position).getDate());
    DateFormat outputDate = new SimpleDateFormat("d MMM yyyy", Locale.getDefault());
    thisDate = outputDate.format(inputDate);
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

holder.artist.setText(thisArtist);

holder.venue.setText(thisVenue);
holder.date.setText(thisDate);
if(!thisArtistPic.isEmpty())
UrlImageViewHelper.setUrlDrawable(holder.bandPic, thisArtistPic);
if(!thisVenuePic.isEmpty())
    UrlImageViewHelper.setUrlDrawable(holder.venuePic, thisVenuePic);

return convertView;
    }
       static class ViewHolder{
    TextView artist;
    TextView venue;
    TextView date;
    ImageView bandPic;
    ImageView venuePic;

      }
    }

1 个答案:

答案 0 :(得分:2)

我建议在适配器的getView()中执行此操作,而不是在onItemClick()中更改视图的大小。存储将要更改的视图的位置,使ListView无效并改为在getView()方法中执行调整大小。