0

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

如果单击一项,我想更改文本颜色。但是每当我单击列表视图中的一项时,两项的颜色现在正在改变。这两项之间的距离始终为 5。我在 getView 代码上使用 convertView。并且 textcolor 更改代码在 clicklistener 方法中。我可以用 setTag、getTag 解决这个问题吗?如果是,我该如何解决?

4

3 回答 3

3

将此代码放在您的适配器类中:

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

在项目单击时,调用方法来设置当前位置:

public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
                adapter.setSelectedPosition(pos);
        }
于 2013-11-25T04:50:16.883 回答
0

尝试这个

 convertView.setOnClickListener

代替

 onItemClick
于 2013-11-25T05:58:30.707 回答
0

尝试如下:

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


        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;

        view.setTag((Integer) position);
    }
}
于 2013-11-25T04:33:06.137 回答