Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用数组适配器更改列表视图项布局的背景色?_Android_Listview_Android Arrayadapter - Fatal编程技术网

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

Android 如何使用数组适配器更改列表视图项布局的背景色?,android,listview,android-arrayadapter,Android,Listview,Android Arrayadapter,我有Listview、ArrayAdapter和ArrayAdapter的布局作为ItemView 现在,我已成功更改选定/单击项目布局的背景色 但是,当选择另一个项目时,如何将背景颜色更改为原始颜色 代码示例: Listview listview; int PREVIOUSLY_SELECTED_ID = -1; if (arrayList != null) { Collections.sort(arrayList); 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);
 }
Listview-Listview;
int先前选择的\u ID=-1;
if(arrayList!=null){
Collections.sort(arrayList);
arrayAdapter=新的arrayAdapter(getContext(),android.R.layout.simple\u list\u item\u 1,arrayList){
@非空
@覆盖公共视图getView(最终int位置,最终视图convertView,@NonNull最终视图组父级){
LayoutInflater LayoutInflater=getLayoutInflater();
//这是我正在扩展到arrayadapter的布局文件“listitem_Layout.xml”。
@SuppressLint({“ViewHolder”,“InflateParams”})最终视图=LayoutFlater.inflate(R.layout.listitem_layout,null,true);
//这是“listitem_layout.xml”中的相对值。
最终RelativeLayout RelativeLayout=(RelativeLayout)视图.findViewById(R.id.RelativeLayout\u选定项);
//这是“relativelayout”的onClick事件。
relativeLayout.setOnClickListener(新视图.OnClickListener(){
@覆盖公共void onClick(视图v){
如果(以前选择的位置)
{
//在这里,我在单击项目时更改relativelayout的背景色。
v、 挫折背景资源(R.color.tomatoLight);
如果(以前选择的\u ID!=-1)
{
//在这里,我想将以前选择的项目的背景颜色更改为其原始颜色(即“橙色”)。
getAdapter().getView(位置、转换视图、父级).setBackgroundResource(R.color.orange);
}
以前选择的\u ID=位置;
}
其他的
{
v、 setBackgroundResource(R.color.橙色);
}
});
返回视图;
}
};
setAdapter(arrayAdapter);
}

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

更新 使用
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();
        }
    });
}
Listview-Listview;
int selectedPosition=-1;
if(arrayList!=null){
Collections.sort(arrayList);
arrayAdapter=新的arrayAdapter(getContext(),android.R.layout.simple\u list\u item\u 1,arrayList){
@非空
@凌驾
公共视图getView(最终int位置,最终视图convertView,@NonNull最终视图组父级){
LayoutInflater LayoutInflater=getLayoutInflater();
@SuppressLint({“视图持有者”、“充气参数”})
最终视图=LayoutFlater.inflate(R.layout.listitem_布局,null,true);
最终RelativeLayout RelativeLayout=(RelativeLayout)视图.findViewById(R.id.RelativeLayout\u选定项);
//如果当前创建的视图与通知适配器之前单击的视图相同,请更改其颜色
如果(selectedPosition==位置){
相对论挫折背景资源(R.color.tomatoLight);
}否则{
相对生长.挫折背景资源(R.颜色.橙色);
}
返回视图;
}
};
setAdapter(arrayAdapter);
setOnItemclickListener(新的OnItemClickListener(){
@凌驾
公共链接(适配器视图适配器,视图arg1,int位置,长id){
selectedPosition=位置;
arrayAdapter.notifyDataSetChanged();
}
});
}
您可以使用Listview方法获取视图,然后更改颜色

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

                        }

希望这能有所帮助。

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


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

是否添加了任何用于检测所选项目的标志?是“已选择的ID”是用来记住上次选定id的int标志。arrayList上有多少数据?您需要设置一个标志来检测数据列表上的选定项。单个“选定id”是不够的,应该有这些id的数组,在您的数组适配器中,如果(选定id[当前]==选定),则颜色为选定,否则颜色为未选定“v”是onClick视图(视图v)所以它不会在onClick()事件之外工作。再想一想!是的,我更新了我的答案。
relativeLayout
视图应该在
onClick
Yes之外使用,但是当单击项目时,我想将背景颜色更改为“tomotlight”,当我单击另一个项目时,上一个项目的背景更改为“橙色”(这是它的起源