【问题标题】:Setting the first item in the listview as default for android将listview中的第一项设置为android的默认值
【发布时间】:2014-05-26 22:26:44
【问题描述】:

我已经尝试在网上寻找一个好的解决方案,但我似乎找不到。

有一个帐户列表视图。所选帐户的背景颜色为浅蓝色,但仅在按下时才会显示。这是我从中获取代码的地方:Android ListView selected item stay highlighted

在 onCreate 方法中,我有一个 onItemClickListener 方法,如果我执行 listview.setSelection(0);在 onItemClick 方法之上,默认项是列表视图中的第一项。系统知道列表中的第一个帐户是当前选择的项目,但我如何直观地显示它?

基本上,我想以某种方式执行 view.setSelected(true);列表视图中的第一项。

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parentAdapter,
                    View view, int position, long id) {
                view.setSelected(true);
                Account account= accounts.get(position);
                accountNumber = account.getAccountNumber();
            }
        });

谢谢!

编辑:这是我们自定义适配器的 getView

public final View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    rowView = inflater.inflate(rowResourceId, parent, false);
    textView = (TextView) rowView.findViewById(R.id.textView);
    textView.setTextColor(Color.BLACK);
    textView.setText(objects.get(position).debug());
    return rowView;
}

【问题讨论】:

标签: android listview android-listview


【解决方案1】:

正确的方法是使用自定义数组适配器并覆盖 getView 方法。对于第一个项目,您将设置背景:

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

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

    rowView = inflater.inflate(rowResourceId, parent, false);

    if(position == 0){
        //you might remove this check if your develop for new api's only
        int sdk = android.os.Build.VERSION.SDK_INT;
        if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
            rowView.setBackgroundDrawable();
        } else {
            rowView.setBackground();
        }
    }

    textView = (TextView) rowView.findViewById(R.id.textView);
    textView.setTextColor(Color.BLACK);
    textView.setText(objects.get(position).debug());

    return rowView;
}

在此处阅读更多相关信息:Vogella - Using lists in android 此解决方案可能比强制选择更好。如果选择是由用户执行的操作,您实际上只想突出显示第一行,因此请在您的 get view 方法中对其进行操作。

【讨论】:

  • 但是请看,我的 view.setSelected 负责为我更改背景颜色……我们正在使用自定义适配器。我现在将发布当前 getView 的代码。
  • @user1883614 对,不要强制选择,在您的 getView 方法中,您可以尝试我在回答中建议的方法,为位置 0 设置背景
  • 什么是行布局?获取资源()?和drawable.ready?抱歉,eclipse 将这些标记为错误...
  • 嗯,几乎成功了!谢谢!我会多尝试一下,让它完全符合我的需要。
  • 想通了,但我不能发布几个小时,但我会发布我的解决方案!谢谢!
【解决方案2】:

所以我在代码方面作弊并对其进行了硬编码,但它现在可以工作了!

对于自定义适配器中的getView:

if (position == 0) {
        textView.setBackgroundColor(Color.rgb(173, 211, 224));
}

在实际activity的onCreate方法中,对于setOnItemClickListener:

if (position != 0) {
View rowView = listview.getChildAt(0);
    TextView textView = (TextView) rowView.findViewById(R.id.textView);
textView.setBackgroundColor(Color.rgb(227, 236, 239));
} else {
    View rowView = listview.getChildAt(0);
    TextView textView = (TextView) rowView.findViewById(R.id.textView);
textView.setBackgroundColor(Color.rgb(173, 211, 224));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    相关资源
    最近更新 更多