Listview位置无法正常工作

时间:2016-12-09 12:24:11

标签: android listview onclicklistener

我有列表视图。我想改变点击行的颜色。但是只有0,1和2个索引才能正确着色,否则其他人也无法正常工作如果我点击第4行第5行颜色会发生变化,有时如果我点击第7行则没有任何行被着色。请帮忙

 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,final int position, long id) {

            for (int i = 0; i < lv.getChildCount(); i++) {
                if(position == i ){
                    lv.getChildAt(i).setBackgroundColor(Color.BLUE);
                }else{
                    lv.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
                }
            }



  public abstract class CustomAdapter extends BaseAdapter implements  SeekBar.OnSeekBarChangeListener {
    Context context;      
    ArrayList<HashMap<String, String>> countryList;        
    ArrayList<HashMap<String, String>> mStringFilterList;
    LayoutInflater inflter;
    public ImageView img2,img3;
    Handler mHandler = new Handler();
    SeekBar songProgressBar;
    SelfUpdatingSeekbar self;
    public boolean isStarted = true;
    public static final int UPDATE_FREQUENCY = 500;
    public static final int STEP_VALUE = 4000;
    public final Handler handler = new Handler();


    public final Runnable updatePositionRunnable = new Runnable() {
        public void run() {
            updatePosition();
        }
    };

    public CustomAdapter(Context applicationContext, ArrayList<HashMap<String, String>> countryList) {
        this.context = applicationContext;
        this.countryList = countryList;
        mStringFilterList =  countryList;

        inflter = (LayoutInflater.from(applicationContext));
    }

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

    public void updateData(ArrayList<HashMap<String, String>> countryList) {
        this.countryList = countryList;
        notifyDataSetChanged();
    }

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

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

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        view = inflter.inflate(R.layout.list_itemss, null);

        view.setTag(position);

        String hello = String.valueOf(countryList.get(position));
        String s = hello;
        int s1 = s.lastIndexOf("=");
        int s2 = s.lastIndexOf("}");

         strSub = s.substring(s1+1,s2/*s.lastIndexOf("=")*/);

        Log.d("Hello",hello);
        String henno1 = String.valueOf(hello.length());
        Log.d("hellya",strSub);

        TextView country = (TextView) view.findViewById(R.id.textView);
        country.setText(strSub);

         uniqueItemIdCount = countryList.size();

        Log.d("PrintIdss", String.valueOf(uniqueItemIdCount));

        ImageView twitt = (ImageView)view.findViewById(R.id.button5);

        twitt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TweetComposer.Builder builder = new TweetComposer.Builder(context)
                        .text(strSub);

                builder.show();
            }
        });

        ImageView fb = (ImageView)view.findViewById(R.id.button6);

        fb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                ShareLinkContent linkContent = new ShareLinkContent.Builder()
                        .setContentTitle(strSub)
                        .setContentDescription(
                                "Top Hollywood Songs")
                        .setContentUrl(Uri.parse("http://www.moremovies.com/"))
                        .build();

                shareDialog.show(linkContent);
            }
        });




        songProgressBar = (SeekBar) view.findViewById(R.id.songProgressBar);
        songProgressBar.setOnSeekBarChangeListener(this);

        songCurrentDurationLabel = (TextView)view.findViewById(R.id.songCurrentDurationLabel);
        songTotalDurationLabel = (TextView)view.findViewById(R.id.songTotalDurationLabel);


        img2 = (ImageView)view.findViewById(R.id.button3);


        img2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                int songIndex = position;



                String hellos = String.valueOf(countryList.get(songIndex));
                int s1 = hellos.lastIndexOf("=");
                int s2 = hellos.lastIndexOf("}");

              String  strSubs = hellos.substring(s1+1,s2/*s.lastIndexOf("=")*/);
                selelctedFile.setText(strSubs);

                currentSongIndex=songIndex;

                playSong(currentSongIndex);


            }
        });
    }

4 个答案:

答案 0 :(得分:0)

您必须管理自己的位置和背景,因为在重新使用单元格时,在适配器中。请检查您的适配器,你会得到它。

并实现你想要的。您必须更新适配器并在适配器视图中实现单击侦听器。

答案 1 :(得分:0)

尝试减去列表视图的位置:

for (int i = 0; i < lv.getChildCount(); i++) {
    if(position - lv.getFirstVisiblePosition() == i ){ //<-Here
        lv.getChildAt(i).setBackgroundColor(Color.BLUE);
    }else{
        lv.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
    }
}

答案 2 :(得分:0)

如果您想从播放按钮开始,点击更改以暂停单击的所有其他人以及其他所有其他人重新播放,然后在适配器内部初始化播放/暂停按钮:

playPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {               
             lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                 public void onItemClick(AdapterView<?> parent, View v,final int position, long id) {
                     for (int i = 0; i < lv.getChildCount(); i++) {
                          Button PlayPause = v.findViewById(R.id.play_pause_button)
                          ButtonPlayPause.setImageResource("play resource")
                     }
                     playPause.setImageResource("pause resource")
                  }          
             }
        }
    });

代码可能有一些语法错误但你明白了。

答案 3 :(得分:0)

您需要实现两个后台选项,因为适配器将重用您的布局。

因此,您需要跟踪所选项目(而不是布局),因为我看到您正在使用HashMap来填充您的适配器,因此,创建一个包含所选位置的List,并在您填充适配器时执行以下操作:

 //Globals Variables
 List<int> SelectedList = new ArrayList<>();

 //On select event
 SelectedList.add(position);

 //On deselect event
 SelectedList.remove(position);


 //On get view
if(SelectedList.contains(position){

     // Background selected
}else{
   // Explicit set the background to the default
}