android - 如何与列表中的一行中的对象交互

标签 android list textview

我的应用程序中有一个列表。列表中的每一行都有一个图像和一个 TextView 。

当单击相应的行时,我希望 textView 向右移动。

如何“获取”“setOnItemClickListener”内的textView

这是我的适配器:

public class MyAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
private TextView textView;
private ImageView icon;
public boolean first;

public MyAdapter(Context context, String[] values) {
    super(context, R.layout.rowlayout, values);
    this.context = context;
    this.values = values;
    this.first = true;
}

@SuppressWarnings("deprecation")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    textView = (TextView) rowView.findViewById(R.id.row_text_view);
    icon = (ImageView) rowView.findViewById(R.id.icon);
    if (first) {
        rowView.setBackgroundColor(Color.TRANSPARENT);
        textView.setBackgroundColor(Color.TRANSPARENT);
        icon.setBackgroundColor(Color.TRANSPARENT);
        first = false;
        rowView.setClickable(false);
        rowView.setEnabled(false);
    } else {
        rowView.setBackgroundColor(Color.WHITE);
        textView.setBackgroundColor(Color.WHITE);
        icon.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_launcher));
    }
    textView.setText(values[position]);

    return rowView;
}}

谢谢!

最佳答案

OnItemClickListener 具有具有以下签名的方法:

onItemClick(AdapterView<?> parent, View view, int position, long id)

因此您已经单击了用户所在的行。您现在可以从行获取 TextView :

view.findViewById(R.id.row_text_view)`.

关于android - 如何与列表中的一行中的对象交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13440302/

相关文章:

android - 我怎样才能让我的动画不那么生涩?

android - gradle4.5编译错误:无法添加任务

c# - 如何反转列表内容的显示顺序?

Android - ellipsize ="end"不显示三个点

android - 如何在显示时动态更改 Toast 通知中的文本?

android - 配置模拟器 DNS 设置

android - 如何将对象附加到多维列表

python - 使用python将配置文件转换为字典

android - 如何在TextView中创建垂直对齐的上标和下标

android - 如何将文本放在 ImageView 下