Search code examples
androidlistviewconvertview

Android convertView


i have used horizontal listview like this with Baseadapter.

private class ListViewItemClickListener implements
            AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            if (imageview != null) {
                previousImage = (ImageView) imageview
                        .findViewById(R.id.imageView);
                previousImage.setSelected(false);
                TextView previousTitle = (TextView) textview
                        .findViewById(R.id.title);
                previousTitle.setTextColor(Color.parseColor("#33D6AD"));
                previousTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
            }

            selected_image = (ImageView) view.findViewById(R.id.imageView);
            selected_image.setSelected(true);

            selected_text = (TextView) view.findViewById(R.id.title);
            selected_text.setTextColor(Color.parseColor("#368AFF"));
            selected_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);

            select_position = position;

            imageview = selected_image;
            textview = selected_text;

        }
    }

private static String[] dataObjects = new String[] { "그랜드 캐니언",
            "그레이트 베리어 리프", "디즈니 월드", "뉴질랜드 남섬", "케이프 타운", "암리차르의 황금사원",
            "라스베가스", "시드니", "뉴욕", "타지마할" };

private BaseAdapter mAdapter = new BaseAdapter() {

        @Override
        public int getCount() {
            return dataObjects.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(
                        R.layout.listitem, null);
            }
            TextView title = (TextView) convertView.findViewById(R.id.title);
            title.setText(dataObjects[position]);

            return convertView;
        }
    };

and i want to change textcolor if one item is clicked. but whenever i click one item in the listview, two item's color are changing right now. and distance between those two item is always 5. I am using convertView on the getView code. and textcolor change code is in clicklistener method. can i solve this with setTag, getTag. and if it is, how can i solve this?


Solution

  • Put this code in your adapter class :

    private int selectedPos = -1;
    
    
    public void setSelectedPosition(int pos){
        selectedPos = pos;
        notifyDataSetChanged();
    }
    
    
    
     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
          if (convertView == null) {
               convertView = LayoutInflater.from(parent.getContext()).inflate(
                            R.layout.listitem, null);
           }
           TextView title = (TextView) convertView.findViewById(R.id.title);
           title.setText(dataObjects[position]);
    
            if (selectedPos == position) {
                title.setTextColor(Color.parseColor("#368AFF"));
                title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            } else {
               title.setTextColor(Color.parseColor("#000000"));
            }
            return convertView;
        }
    

    And on item click, call method to set current position:

    public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                    adapter.setSelectedPosition(pos);
            }