【问题标题】:How do I change background color of layout of list-view item using array-adapter?如何使用数组适配器更改列表视图项布局的背景颜色?
【发布时间】:2018-03-19 08:53:32
【问题描述】:

我有 Listview、ArrayAdapter 和 ArrayAdapter 的布局作为 ItemView。

现在,我已经设法更改了选定/单击项目布局的背景颜色。

But how can I change the background color to the original when another item is selected?

代码示例:

Listview listview;
int PREVIOUSLY_SELECTED_ID = -1;

        if (arrayList != null) {
        Collections.sort(arrayList);
        arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, arrayList){
            @NonNull
            @Override public View getView(final int position, final View convertView, @NonNull final ViewGroup parent) {

                LayoutInflater layoutInflater = getLayoutInflater();

                // This is the Layout File "listitem_layout.xml" i am inflating to arrayadapter.
                @SuppressLint({"ViewHolder", "InflateParams"}) final View view = layoutInflater.inflate(R.layout.listitem_layout, null, true);

                // This is the RelativeLayout in "listitem_layout.xml".
                final RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.relativelayout_selected_item);

                // This is onClick event of "relativelayout".
                relativeLayout.setOnClickListener(new View.OnClickListener() {
                    @Override public void onClick(View v) {

                        if (PREVIOUSLY_SELECTED_ID != position)
                        {
                            // Here, i am changing background color of relativelayout when item is clicked.
                            v.setBackgroundResource(R.color.tomatoLight);
                            if(PREVIOUSLY_SELECTED_ID != -1)
                            {
                                // Here, i want to change Previously Selected Item's Background Color to it's original(Which is 'Orange'). 
                                listView.getAdapter().getView(position,convertView,parent).setBackgroundResource(R.color.orange);
                            }
                            PREVIOUSLY_SELECTED_ID = position;
                        }
                        else
                        {
                            v.setBackgroundResource(R.color.orange);
                        }
                });

                return view;
            }
        };
        listView.setAdapter(arrayAdapter);
 }

【问题讨论】:

  • 您是否添加了任何标志来检测所选项目?
  • 是的 "SELECTED_ID" 是 int 标志,用于记住最后选择的 id。
  • 你的arrayList上有多少数据?
  • 您需要设置一个标志来检测数据列表中的选定项目。
  • 单个“SELECTED_ID”是不够的,应该有这些 id 的数组,在你的数组适配器中 if(SELECTED_ID[current]==selected)then color_selected else color_not_selected

标签: android listview android-arrayadapter


【解决方案1】:

您可以使用Listview的getChildAt()方法获取视图然后更改颜色。

     if (SELECTED_ID != -1) {
        View view1 = listView.getChildAt(SELECTED_ID -
                                  listView.getFirstVisiblePosition());
                            // Here, i want to change Previously Selected Item's Background Color to it's original(Which is 'Orange').
                            view1.setBackgroundResource(R.color.Orange);

                        }

希望这会有所帮助。

【讨论】:

  • 它确实对我有用,如果块其余部分相同,只需替换它,请告诉其他在做什么,如果先前和选择的位置相同,它应该保持选中状态,对吧?那么 else 也应该改为tomatoLight。
【解决方案2】:

您可以只跟踪上次单击的项目,然后通知适配器,以便它相应地设置背景

更新 使用OnItemClickListener 来捕获点击而不是使用视图本身

Listview listview;
int selectedPosition = -1;
if (arrayList != null) {
    Collections.sort(arrayList);
    arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, arrayList) {
        @NonNull
        @Override 
        public View getView(final int position, final View convertView, @NonNull final ViewGroup parent) {
            LayoutInflater layoutInflater = getLayoutInflater();
            @SuppressLint({"ViewHolder", "InflateParams"}) 
            final View view = layoutInflater.inflate(R.layout.listitem_layout, null, true);
            final RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.relativelayout_selected_item);

            // if the views that is currently being created is the same is the one clicked before notifying the adapter then change it's color
            if (selectedPosition == position) {
                relativeLayout.setBackgroundResource(R.color.tomatoLight);
            } else {
                relativeLayout.setBackgroundResource(R.color.orange);
            }

            return view;
        }
    };
    listView.setAdapter(arrayAdapter);
    listView.setOnItemclickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
                selectedPosition = position;
                arrayAdapter.notifyDataSetChanged();
        }
    });
}

【讨论】:

  • "v" 是 onClick(View v) 的视图,因此它不会在 onClick() 事件之外工作。再想想!
  • 是的,我更新了我的答案.. 应该在 onClick 之外使用 relativeLayout 视图
  • 是的,但是当单击项目时,我想将背景颜色更改为“tomatolight”并且当我单击另一个项目时,前一个项目的背景更改为“橙色”(它是原始颜色)。
  • 这个想法是让SELECTED_ID 包含当前选定的项目.. 当您使用notifyDataSetChanged 通知适配器时,只有单击的项目将是番茄光所有其他项目将是橙色的.. 你尝试过吗我在您的应用程序中提供的代码?
  • 它不会起作用,因为您在将 SELECTED_ID 与当前位置进行比较之前更改了它。
【解决方案3】:

@ColdFire 是正确的,因为您必须覆盖 ArrayAdapter 的getView 方法的默认行为。我相信只使用 ListView 的 getChild 方法只会返回一个具有不同引用的视图。这就是为什么当您尝试通过调用 setBackgroundColor 来修改它时没有任何反应的原因(您正在设置差异列表项视图的背景颜色。

如果您希望在单击另一个项目时改变其他项目的颜色,您需要记住的不仅仅是一个值。例如:除了selectedPosition,您可能还想使用另一个变量,比如previousPosition,并在发生更改时跟踪这些变量的值。这些只是两个变量,这意味着您一次只能记住两件事。也许使用List&lt;Integer&gt;int[] 数组,并在用户选择和取消选择列表中的项目时添加或删除值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多