简体   繁体   English

滚动时,Android ListView会丢失背景颜色

[英]Android ListView losing background color when scrolled

I am changing the background color of the clicked item 我正在更改所点击项目的背景颜色

public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.layout_create_msg, container, false);

     final EditText tbsendto=(EditText) view.findViewById(R.id.tbsendto);

     System.out.println("Aqui");

    SharedPreferences userDetails = getActivity().getBaseContext().getSharedPreferences("Login", Context.MODE_PRIVATE);
    username=userDetails.getString("Unm","");


    GetUsersMsg getMsg=new GetUsersMsg(getActivity());
    getMsg.execute("new",username);
    final ListView list=(ListView)view.findViewById(R.id.lv_users_new_msg);

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View itemview, int i, long l) {
         View v=itemview;
            v=inflater.inflate(R.layout.layout_create_msg,null);

            final String item = ((TextView) itemview).getText().toString();

            System.out.println(item);
            String textsendto;
            System.out.println(tbsendto.getText().toString());
            if (tbsendto.getText().toString().contains(item)){
                textsendto=tbsendto.getText().toString().replace( "  " + item,"");
                itemview.setBackgroundColor(Color.parseColor("#19394a"));
                ((TextView) itemview).setTextColor(Color.WHITE);

            }else {
                textsendto=tbsendto.getText().toString() + "  " +  item;
                itemview.setBackgroundColor(Color.WHITE);
                ((TextView) itemview).setTextColor(Color.parseColor("#19394a"));
            }


            tbsendto.setText(textsendto);
            tbsendto.setSelection(tbsendto.getText().length());

        }
    });

    return view;
}

But when i scroll the listview and the item leaves the screen is loosing the given color. 但是当我滚动列表视图并且项目离开时,屏幕会丢失给定的颜色。 Selecting items in the listivew 选择listivew中的项目

Sctrolling Sctrolling

What am i doing wrong and why is it loosing the given color? 我做错了什么,为什么它会失去给定的颜色? Thanks! 谢谢!

Because when scrolling the views are recreated. 因为滚动视图时会重新创建。 You have to set the color in your adapter. 您必须在适配器中设置颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM