单击新项目后,删除listview中先前突出显示的项目

时间:2017-08-09 02:31:13

标签: android listview

我有一个列表视图。在此列表视图中,我突出显示了listview项的文本。当我点击列表视图中的另一个项目时,我想删除上一个项目的文本颜色和文本样式。

这是listview适配器代码`

public class SubLessionAdapter extends ArrayAdapter<SubLessionDetail> {
Context context;
int resource, textViewResourceId;
List<SubLessionDetail> items;
List<SubLessionDetail> tempItems;
List<SubLessionDetail> suggestions;
private Dialog pinDialog;
private EditText popup_title, popup_review;
private TextView submit;
private RatingBar rating;
private String flag;
private VideoList videoList;
private CourseListMain myCourseListMain;
SubLessionAdapter.ViewHolder publicholder;
SharedPreferences pref;
SharedPreferences.Editor editor;
int listposition;

String topic_position,course_position,lession_position,item_id;

public SubLessionAdapter(Context dummyTopicList, ArrayList<SubLessionDetail> lessionLIstDetails, String flag,String course_position,String topic_position,String lession_position,String item_id) {
    super(dummyTopicList, 0, lessionLIstDetails);
    this.context = dummyTopicList;
    this.items = lessionLIstDetails;
    this.flag=flag;
    this.course_position=course_position;
    this.topic_position=topic_position;
    this.lession_position=lession_position;
    this.item_id=item_id;


    pref = getContext().getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
    editor = pref.edit();


    myCourseListMain = PrefUtils.getMyCourse(getContext());
    listposition = pref.getInt("sublistpostion",0);

}


private class ViewHolder {

    TextView title;
    ImageView img_lock,download_green,download_gray,itemcheckded;
    LinearLayout topic;


}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    SubLessionAdapter.ViewHolder holder = null;



    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_topics, parent, false);

        holder = new SubLessionAdapter.ViewHolder();
        holder.topic =(LinearLayout)convertView.findViewById(R.id.topic);
        holder.title = (TextView) convertView.findViewById(R.id.title);
        holder.img_lock = (ImageView) convertView.findViewById(R.id.img_lock);
        holder.download_gray=(ImageView)convertView.findViewById(R.id.downlaod_gray_sublist);
        holder.download_green=(ImageView)convertView.findViewById(R.id.download_green_sublist);
        holder.itemcheckded =(ImageView)convertView.findViewById(R.id.itemchecked);

        convertView.setTag(holder);

    }
    else
    {
        holder = (SubLessionAdapter.ViewHolder) convertView.getTag();
    }


    SubLessionDetail productItems = items.get(position);
    holder.title.setText( productItems.getTitle());
    if (productItems.getLocked().equals("1")) {holder.img_lock.setVisibility(View.GONE);} else {holder.img_lock.setVisibility(View.VISIBLE);}
    if (flag.equals("my_course")){holder.img_lock.setVisibility(View.GONE);}else {holder.img_lock.setVisibility(View.VISIBLE);}



    if(listposition==position){

        holder.title.setTypeface(null, Typeface.BOLD);
        holder.title.setTextColor(Color.parseColor("#4dd289"));
    }else{
        holder.title.setTypeface(null, Typeface.NORMAL);
        holder.title.setTextColor(Color.parseColor("#000000"));
    }


    final ViewHolder finalHolder = holder;
    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            finalHolder.title.setTypeface(null, Typeface.BOLD);
            finalHolder.title.setTextColor(Color.parseColor("#4dd289"));
           // finalHolder.itemcheckded.setVisibility(View.VISIBLE);

            switch (flag) {

                case "my_course":

                    Intent intent = new Intent(getContext(), ContentTabActivity.class);
                    intent.putExtra("titlevalue",myCourseListMain.getCourseArrayList().get(Integer.parseInt(course_position)).getTopicListMain().getTopicDetailsArrayList().get(Integer.parseInt(topic_position)).getLessionArrayList().get(Integer.parseInt(lession_position)).getLessionLIstDetailArrayList().get(position).getTitle());
                    intent.putExtra("lession_id", myCourseListMain.getCourseArrayList().get(Integer.parseInt(course_position)).getTopicListMain().getTopicDetailsArrayList().get(Integer.parseInt(topic_position)).getLessionArrayList().get(Integer.parseInt(lession_position)).getLessionLIstDetailArrayList().get(position).getLession_id());
                    intent.putExtra("id", item_id);
                    intent.putExtra("lession_position", lession_position);
                    intent.putExtra("postion_current_selected_item",position+"");
                    intent.putExtra("course_position", course_position);
                    intent.putExtra("topic_position", topic_position);
                    editor.putString("sublession_has_1_item","no");
                    editor.putInt("sublistpostion",position);
                    editor.commit();
                    context.startActivity(intent);


                    break;


                case "curriculam":
                    Toast.makeText(getContext(), "Content is Locked", Toast.LENGTH_SHORT).show();
                    break;

            }

        }
    });


    return convertView;

}

} `

现在发生的事情是,当我点击该项目时,它会更改textstyle和textcolor,但前一项仍然会突出显示。

我想一次只突出显示一个项目(清除上一项中的突出显示)。

下面是一些正在发生的事情的图片。

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

您的数据源中应该有一个字段来指示数据是否被选中,您只需更改onclick侦听器中的字段而不是突出显示该视图。更改视图的具体操作应该在getView()方法中进行,在此期间您应该设置或恢复文本颜色和字体取决于字段值。

你可以这样做:

public class SubLessionAdapter extends ArrayAdapter<SubLessionDetail> {
Context context;
int resource, textViewResourceId;
List<SubLessionDetail> items;
List<SubLessionDetail> tempItems;
List<SubLessionDetail> suggestions;
private Dialog pinDialog;
private EditText popup_title, popup_review;
private TextView submit;
private RatingBar rating;
private String flag;
private VideoList videoList;
private CourseListMain myCourseListMain;
SubLessionAdapter.ViewHolder publicholder;
SharedPreferences pref;
SharedPreferences.Editor editor;
private SubLessionDetail preSelectedItem;

String topic_position,course_position,lession_position,item_id;

public SubLessionAdapter(Context dummyTopicList, ArrayList<SubLessionDetail> lessionLIstDetails, String flag,String course_position,String topic_position,String lession_position,String item_id) {
    super(dummyTopicList, 0, lessionLIstDetails);
    this.context = dummyTopicList;
    this.items = lessionLIstDetails;
    this.flag=flag;
    this.course_position=course_position;
    this.topic_position=topic_position;
    this.lession_position=lession_position;
    this.item_id=item_id;


    pref = getContext().getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
    editor = pref.edit();


    myCourseListMain = PrefUtils.getMyCourse(getContext());
    listposition = pref.getInt("sublistpostion",0);

}


private class ViewHolder {

    TextView title;
    ImageView img_lock,download_green,download_gray,itemcheckded;
    LinearLayout topic;


}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    SubLessionAdapter.ViewHolder holder = null;



    if (convertView == null) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_topics, parent, false);

        holder = new SubLessionAdapter.ViewHolder();
        holder.topic =(LinearLayout)convertView.findViewById(R.id.topic);
        holder.title = (TextView) convertView.findViewById(R.id.title);
        holder.img_lock = (ImageView) convertView.findViewById(R.id.img_lock);
        holder.download_gray=(ImageView)convertView.findViewById(R.id.downlaod_gray_sublist);
        holder.download_green=(ImageView)convertView.findViewById(R.id.download_green_sublist);
        holder.itemcheckded =(ImageView)convertView.findViewById(R.id.itemchecked);

        convertView.setTag(holder);

    }
    else
    {
        holder = (SubLessionAdapter.ViewHolder) convertView.getTag();
    }


    final SubLessionDetail productItems = items.get(position);
    holder.title.setText( productItems.getTitle());
    if (productItems.getLocked().equals("1")) {holder.img_lock.setVisibility(View.GONE);} else {holder.img_lock.setVisibility(View.VISIBLE);}
    if (flag.equals("my_course")){holder.img_lock.setVisibility(View.GONE);}else {holder.img_lock.setVisibility(View.VISIBLE);}


    if (productItems.isSelected()) {
        holder.title.setTypeface(null, Typeface.BOLD);
        holder.title.setTextColor(Color.parseColor("#4dd289"));
    } else {
        holder.title.setTypeface(null, Typeface.NORMAL);
        holder.title.setTextColor(Color.parseColor("#000000"));
    }


    final ViewHolder finalHolder = holder;
    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            if (preSelectedItem != null) {
                preSelectedItem.setSelected(false);
            }
            productItems.setSelected(true);
            preSelectedItem = productItems;
            notifyDataSetChanged();

            switch (flag) {

                case "my_course":

                    Intent intent = new Intent(getContext(), ContentTabActivity.class);
                    intent.putExtra("titlevalue",myCourseListMain.getCourseArrayList().get(Integer.parseInt(course_position)).getTopicListMain().getTopicDetailsArrayList().get(Integer.parseInt(topic_position)).getLessionArrayList().get(Integer.parseInt(lession_position)).getLessionLIstDetailArrayList().get(position).getTitle());
                    intent.putExtra("lession_id", myCourseListMain.getCourseArrayList().get(Integer.parseInt(course_position)).getTopicListMain().getTopicDetailsArrayList().get(Integer.parseInt(topic_position)).getLessionArrayList().get(Integer.parseInt(lession_position)).getLessionLIstDetailArrayList().get(position).getLession_id());
                    intent.putExtra("id", item_id);
                    intent.putExtra("lession_position", lession_position);
                    intent.putExtra("postion_current_selected_item",position+"");
                    intent.putExtra("course_position", course_position);
                    intent.putExtra("topic_position", topic_position);
                    editor.putString("sublession_has_1_item","no");
                    editor.putInt("sublistpostion",position);
                    editor.commit();
                    context.startActivity(intent);


                    break;


                case "curriculam":
                    Toast.makeText(getContext(), "Content is Locked", Toast.LENGTH_SHORT).show();
                    break;

            }

        }
    });


    return convertView;

}

您没有显示SubLessionDetail类,并且还应该在其中添加名为selected的布尔字段。

答案 1 :(得分:0)

您需要做的是保存项目的点击位置,然后检查点击项目的文字颜色是否为青色。如果是,则执行您想要的操作,如果没有,则清除列表,调用通知,然后再次添加完整列表。通知添加后,在我们之前保存的位置设置文本颜色。

 List<Bean> list;
 List<Bean> tempList;
 tempList = list;
 int pos;

 //Clean List
 public void cleanList() {
    int size = this.list.size();
    this.list.clear();
    notifyItemRangeRemoved(0, size);
    list.addAll(tempList);
    notifyDataSetChanged();
}

现在是textView的点击监听器

 textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              pos =  = getAdapterPosition();
             if (textView.getTextColors().equals(<ColorYouDesire>)){
               Log.d(TAG,"Item Clicked Again");
               }else{
               cleanList();
               textView.setTextColor(<ColorYouDesire>);
               notifyItemChanged(pos);
               }
            }
        });

试试这个,让我知道发生了什么。