如何使用数组适配器更改列表视图项布局的背景颜色?

时间:2018-03-19 08:53:32

标签: android listview android-arrayadapter

我将ListView,ArrayAdapter和ArrayAdapter的布局作为ItemView。

现在,我已设法更改所选/点击项目布局的背景颜色。

但是,当选择其他项目时,如何将背景颜色更改为原始颜色?

代码示例:

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);
 }

3 个答案:

答案 0 :(得分:1)

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

<div class="bar">
  <div class="foo"></div>
  <p>Lorem ipsum</p>
</div>

希望这有帮助。

答案 1 :(得分:0)

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

<强>更新 使用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();
        }
    });
}

答案 2 :(得分:0)

@ColdFire是正确的,因为您必须覆盖 ArrayAdapter的someKey1=1234,someKey2="a string" 方法的默认行为。我相信仅使用ListView的getView方法就可以返回带有差异引用的视图。这就是为什么当您尝试通过调用getChild来修改它时什么都没发生的原因(您正在设置差异列表项目视图的背景色。

如果您希望在单击其他项目时更改其他项目的颜色,则需要记住多个值。例如:也许您想使用setBackgroundColor以外的其他变量,例如selectedPosition,并在发生更改时跟踪这些变量的值。这些只是两个变量,这意味着您一次只能记住两件事。也许使用previousPositionList<Integer>数组并在用户选择和取消选择列表中的项目时添加或删除值。