滚动

时间:2017-01-07 21:47:57

标签: java android listview colors background

我正在更改所点击项目的背景颜色

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

但是当我滚动列表视图并且项目离开时,屏幕会丢失给定的颜色。 Selecting items in the listivew

Sctrolling

我做错了什么,为什么它会失去给定的颜色? 谢谢!

1 个答案:

答案 0 :(得分:0)

因为滚动视图时会重新创建。您必须在适配器中设置颜色。