更改Android中所选列表项的布局

前端之家收集整理的这篇文章主要介绍了更改Android中所选列表项的布局前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要创建一个ListView并仅为所选行显示更详细的布局,以便向客户显示更多信息.我尝试的是以下内容
newsListView.setAdapter(new NewsListAdapter(this,news));
newsListView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent,View view,int position,long id) {

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

        //Here I tried to change layout of this row only
        view = inflater.inflate(R.layout.listitem_news_selected,null);
        TextView info = (TextView) view.findViewById(R.id.info);

        NewsData news = (NewsData) parent.getItemAtPosition(position);
        info.setText(news.getInfo());
    }
});

这是NewsListAdapter:

public class NewsListAdapter extends BaseAdapter {

    List<NewsData> items;
    Activity context;

    /* private view holder class */
    private class ViewHolder {
        TextView title;
        ImageView image;
    }

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

        ViewHolder holder = null;

        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listitem_news,null);
            holder = new ViewHolder();

            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.image = (ImageView) convertView.findViewById(R.id.imageView1);            

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

        NewsData news = items.get(position);

        holder.title.setText(news.getTitle());
        new DownloadImageTask(holder.image).execute(news.getImgUrl());

        return convertView;
    }
}

In this question,类似的问题被问到,但它没有帮助我.我猜我需要用Adapter做一些事情,但我被困在这里.任何想法将不胜感激.

解决方法

你正在做什么甚至不会改变ListView项目的视图,你正在膨胀一个未知的视图.

解:

在适配器中创建一个方法来设置所选项的位置:

public void selectedItem(int position)
{
   this.position = position; //position must be a global variable
}

当位置等于点击项位置时,在getView中为视图充气

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

    ViewHolder holder = null;

    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.listitem_news,null);
        holder = new ViewHolder();

        holder.title = (TextView) convertView.findViewById(R.id.title);
        holder.image = (ImageView) convertView.findViewById(R.id.imageView1);            

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

    NewsData news = items.get(position);

    holder.title.setText(news.getTitle());
    new DownloadImageTask(holder.image).execute(news.getImgUrl());

    if(this.position == position)
    {
        view2 = mInflater.inflate(R.layout.listitem_news_selected,null);
        TextView info = (TextView) view2.findViewById(R.id.info);

        info.setText(news.getInfo());

        return view2;
    }


    return convertView;
}

在你的onItemClick中使用它

NewsListAdapter adapter = new NewsListAdapter(this,news);
newsListView.setAdapter(adapter );
newsListView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent,long id) {

          adapter.selectedItem(position);
          adapter.notifyDataSetChange();
    }
});

始终有适配器的引用/对象,因此您可以在单击它时刷新listView.

猜你在找的Android相关文章