我需要创建一个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项的“ View ”,而是夸大了未知 View 。

解决方案:

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

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

当位置等于点击项位置时,在getView中使 View 膨胀
@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());

    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, View view, int position, long id) {

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

您的适配器始终具有一个reference/object,因此您可以在单击listView时刷新它。

关于android - 在Android中更改所选列表项的布局,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25347465/

10-10 14:59