lAndroid ListView OnListItemClick API 8高亮显示行移动到行 [英] lAndroid ListView OnListItemClick API 8 Highlight row Moving to a Row

查看:99
本文介绍了lAndroid ListView OnListItemClick API 8高亮显示行移动到行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在一个需要与API 8一起使用的项目中工作时,我发现了很多关于如何突出显示行的示例,因为它们需要至少10个API以上的版本.

As I am working on a project where it needs to work with API 8 I found a lot of examples on how to highlight a row would not work as they need minimum API 10 upwards.

所以我环顾四周,并通过OnListItemClick使荧光笔工作,但是我现在的问题是,当列表中有多个项目时,它允许选择多个项目,这不是我想要的

So I have looked around and got the highlighter to work via OnListItemClick however my problem now is , when there is more than one item in the list, it allows for multiple items to be selected which is not what I want

我使用以下内容突出显示和取消突出显示该行:

I used the following to highlight and un highlight the row:

if (selectedrow == 0) {
   ((TextView)v).setBackgroundColor(Color.rgb(0, 153, 204)); 
   selectedrow = 1;
   oki.setEnabled(true);
   currpos = position;
   File fileselected = new File(pathi.get(currpos));

 } else {
  ((TextView)v).setBackgroundColor(Color.TRANSPARENT); 
  selectedrow = 0;
  oki.setEnabled(false);
   currpos = -1;

}

当我发现它突出显示了多行时,我在上面的代码之前尝试了以下内容:

When I noticed it was highlighting more than one row I then tried the following before the above code:

if (currpos != -1 ) {
   ((ListView)l).smoothScrollToPosition(currpos);
   ((TextView)v).setBackgroundColor(Color.TRANSPARENT); 
   ((ListView)l).smoothScrollToPosition(position);
   selectedrow = 0;
 }

我也尝试了以下方法:

if (currpos != -1 ) {
  ((ListView)l).setSelection(currpos);
  ((TextView)v).setBackgroundColor(Color.TRANSPARENT); 
  ((ListView)l).setSelection(position);
  selectedrow = 0;
 }

编辑*

自定义适配器现在可以正常工作,如下所示,但是荧光笔仍然是一个问题,我希望发布代码可以对此有所启发:

The custom adapter is now working and is as follows but the highlighter is still an issue I hope posting the code will shed some light on this:

public class FileListAdapter extends ArrayAdapter<String>  {

     private List<String> itemsll; 

        public FileListAdapter(Context context, int row,
                List<String> iteml) {
            super(context,row,iteml);
            // TODO Auto-generated constructor stub
            this.itemsll = iteml;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View viewnull = convertView;
            if (viewnull == null) {
                LayoutInflater vrow;
                vrow = LayoutInflater.from(getContext());

                viewnull = vrow.inflate(R.layout.row, null);
            }
            String currow = itemsll.get(position);
            TextView rowtext = (TextView)viewnull.findViewById(R.id.rowtext);
            rowtext.setText(currow);

            if (currpos == position) {
                rowtext.setBackgroundColor(Color.BLUE);
            }

            return viewnull;
        }


        public void setSelectedPosition( int pos )
        {
            currpos = pos; // selectedPos is global variable to handle clicked position
            // inform the view of this change
            notifyDataSetChanged();
        }
     }

然后添加如下的onItemClick:

and then I added the onItemClick as follows:

public void onItemClick( AdapterView<?> arg0,View arg1, int arg2, Long arg3) {

         int pos = arg2;
         fl.setSelectedPosition(pos);
     }


已解决.
很快注册,因为我不能使用onListItemClick和onItemClick,而且由于该类是ListActivity,所以我必须使用Listview lw = getListView()才能使用onItemClick方法


Solved.
Soon registered that I could not use onListItemClick and onItemClick and also as the class is a ListActivity I had to use Listview lw = getListView() to then use the onItemClick method

推荐答案

如果您使用的是自定义列表适配器,则可以做一件事:

if you are using custom list adaptor than you can do one thing:

在适配器类中创建一个公共方法,如:

create one public method in your adaptor class like :

public void setSelectedPosition( int pos )
    {
        selectedPos = pos; // selectedPos is global variable to handle clicked position
        // inform the view of this change
        notifyDataSetChanged();
    }

在getView方法中,更改背景色:

In getView method change your backgroundcolor:

 if ( selectedPos == position )
    {
            ((TextView)v).setBackgroundColor(Color.rgb(0, 153, 204)); 
    }

onItemClick方法调用适配器方法:

onItemClick method call adaptor method:

@Override
    public void onItemClick( AdapterView<?> arg0, View arg1, int arg2, long arg3 )
    {
        int position = arg2;

        youradapter.setSelectedPosition( position );

    }

这篇关于lAndroid ListView OnListItemClick API 8高亮显示行移动到行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆