【问题标题】:Changing View on List Item Click更改列表项的视图单击
【发布时间】:2012-10-27 11:56:06
【问题描述】:

我有一个ListView 和一个onClicklListener

ListView 有一行 Layout,比如 /res/listitem_a

现在在任何列表项的onClickevent 之后,我想更改 只有那个列表项要说/res/listitem_b..

任何关于我应该如何进行的帮助。

【问题讨论】:

  • 你尝试了什么.. ??

标签: android list android-listview android-view


【解决方案1】:

使用 BaseAdapter 并在 getView 调用中进行修改。

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.custom_layout, null);
            // Creates a ViewHolder and store references to the two children views
            // we want to bind data to.
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.text);

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

        // Change text size
        holder.text.setTextAppearance(context,R.style.customStyle);

        return convertView;
    }

    static class ViewHolder {
        TextView text;
    }

并且您可以在 getView 调用中使用位置变量来更改特定行。希望对您有所帮助!!!

【讨论】:

  • 那么你在哪里实现我的第二个布局,必须在 onClick 之后显示?
  • 您的第二个布局将仅用于 Single raw 对吗?您可以通过上述方法自行管理布局。你能具体说明你已经实施了什么吗?以便我们为您提供建议。
【解决方案2】:

您可以使用 ViewFlipper 作为行的布局。使用 ViewFlipper,您可以指定任意数量的布局,并在发生某些事情时在它们之间翻转(例如单击事件)。 Here 是一个很好的 ViewFlipper 教程。

此外,您应该实现一个自定义适配器,扩展 BaseAdapter,并覆盖 getView 方法。

@Override
public View getView(int position, View view, ViewGroup parent) {
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.your_row_layout, null);  //this will inflate the layout into each row
    }

    //from here on, assign the information to display to the layout widgets

希望我对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 2015-06-10
    • 2018-11-17
    • 1970-01-01
    相关资源
    最近更新 更多