防止在ListView中选择双重

时间:2015-06-08 13:42:35

标签: android listview android-listview

Hay我试图选择ListView并将所选位置的视图颜色背景更改为透明红色,但每次点击或选择其他项目颜色背景的项目也都会更改。如何只选择和更改我选择的项目的颜色背景?

Screenshot. I am sorry the app name and logo i deleted in this picture

这是我的ListView

<?xml version="1.0" encoding="utf-8"?>

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listFragmentSettingWaktu" />

这是我的代码

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by Asus on 6/4/2015.
 */
public class SettingWaktuFragment extends Fragment {

    private static final String TAG_PAGE = "tag_page";
    private static final String TAG_TITLE = "tag_title";
    private int page;
    private String title;
    private ArrayList<JamSibuk> jamList;

    private ListView list;
    private MyBaseAdapter jamAdapter;

    public SettingWaktuFragment newInstance(int page, String title){
        SettingWaktuFragment waktuFragment = new SettingWaktuFragment();
        Bundle b = new Bundle();
        b.putInt(TAG_PAGE, page);
        b.putString(TAG_TITLE, title);
        waktuFragment.setArguments(b);
        return waktuFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        this.page = getArguments().getInt(TAG_PAGE);
        this.title = getArguments().getString(TAG_TITLE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.fragment_setting_waktu_list_jam, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        list = (ListView) getView().findViewById(R.id.listFragmentSettingWaktu);

        jamList = new ArrayList<JamSibuk>();
        String jam = null;
        for (int i=0; i<24; i++) {
            if (i < 10)
                jam = "0" + i + ":00";
            else
                jam = i + ":00";
            jamList.add(new JamSibuk(jam, false));
        }
        jamAdapter = new MyBaseAdapter();
        list.setAdapter(jamAdapter);
    }

    private class MyBaseAdapter extends BaseAdapter{

        class ViewHolder{
            TextView textView;
        }

        @Override
        public int getCount() {
            return jamList.size();
        }

        @Override
        public Object getItem(int position) {
            return jamList.get(position);
        }

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

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if(convertView == null)
                convertView = LayoutInflater.from(getActivity()).inflate(android.R.layout.simple_list_item_1, null);

            final ViewHolder holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(android.R.id.text1);
            holder.textView.setText(jamList.get(position).getJam());
            if(jamList.get(position).isSelected())
                convertView.setBackgroundColor(convertView.getContext().getResources().getColor(R.color.transparent_red));

            holder.textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   if(!jamList.get(position).isSelected())
                       jamList.get(position).setSelected(true);
                    else
                       jamList.get(position).setSelected(false);
                    jamAdapter.notifyDataSetChanged();
                }
            });

            return convertView;
        }
    }
}

提前致谢

4 个答案:

答案 0 :(得分:1)

将此添加到您的列表视图android:choiceMode="singleChoice"

答案 1 :(得分:1)

改为使用onItemClickListener()

ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            view.setBackgroundColor(convertView.getContext().getResources().getColor(R.color.transparent_red));
        }

答案 2 :(得分:1)

如果未选择它,则必须设置透明/默认颜色,否则在将一个项目设为红色后会使项目变为红色。

if(jamList.get(position).isSelected()) {
    convertView.setBackgroundColor(convertView.getContext().getResources().getColor(R.color.transparent_red));
}
else{
    convertView.setBackgroundColor(convertView.getContext().getResources().getColor(android.R.color.transparent));
}

答案 3 :(得分:0)

位置不起作用,你应该在界面onitemclicklistener外面记录选择位置,然后更新适配器中的选择位置