Android版的Java:无法选择通过突出监听onScroll在ListView行 [英] Android Java: Unable to selectively highlight rows in a ListView via onScroll listener

查看:138
本文介绍了Android版的Java:无法选择通过突出监听onScroll在ListView行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还有一个拦截器,因为我学习Android的发展。
这一次,我的问题是,当我想选择性强调从适配器通过数据填充一个ListView一行。

这ListView控件实际上是一个对话框中,并且目的是显示的朋友,在这里用户可以选择多个,并强调其作为他选择的列表。
顺便把选定的值,被存储在pviously选择那些$ P $一个ArrayListarr_FriendsShare,使下一次他打开列表视图,行会高亮显示(通过onScrollListener)。

什么是目前发生的事情,只有最近或最后一个点击行/项被高亮显示;而似乎是清除所有的previously突出显示的行。

我不明白为什么它表现的方式,因为行的值被成功存储/从arr_FriendsShare ArrayList中删除,因为我一下就可以了。

下面是我的听众codeS,并在此先感谢平时的帮助:

  //项目点击监听器选择朋友的ListView
    listview_SelectFriends.setOnItemClickListener(新OnItemClickListener()
    {
       @覆盖
       公共无效onItemClick(适配器视图<>适配器,视图V,INT位置,
             长ARG3)
       {
             字符串friends_ListItemSelected =(字符串)adapter.getItemAtPosition(位置);             如果(!arr_FriendsShare.contains(friends_ListItemSelected)){
                 arr_FriendsShare.add(friends_ListItemSelected);
             }
             其他{
                 removeItemFromArrayListString(Main.this,arr_FriendsShare,friends_ListItemSelected);
             }
       }
    });    listview_SelectFriends.setOnScrollListener(新OnScrollListener(){
        @覆盖
        公共无效onScrollStateChanged(AbsListView观点,诠释scrollState){        }        @覆盖
        公共无效onScroll(AbsListView观点,诠释firstVisibleItem,诠释visibleItemCount,诠释totalItemCount){
            对于(INT I = firstVisibleItem;我≤(visibleItemCount + firstVisibleItem);我++){
                串listViewItemText = view.getItemAtPosition(ⅰ)的ToString();
                如果(arr_FriendsShare.contains(listViewItemText)){
                    ColorDrawable CD =新ColorDrawable(getResources()的getColor(R.color.red_light));
                    view.setSelector(CD);
                }
                否则如果(arr_FriendsShare.contains(listViewItemText)){
                    ColorDrawable CD =新ColorDrawable(Color.TRANSPARENT);
                    view.setSelector(CD);
                }
            }
        }
    });

其他code座:

 的ArrayList<串GT;字符串数组=新的ArrayList<串GT;();
    字符串jsonURL =<有的URL HERE取代;
    字符串数组= Global.getStringArrayFromJSON(Main.this,jsonURL,朋友,FriendUsername);    LayoutInflater吹气= getLayoutInflater();
    查看convertView =(查看)inflater.inflate(R.layout.friends_list_layout,NULL);    ListView控件listview_SelectFriends =(ListView控件)convertView.findViewById(R.id.layout_Friends);
    ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,字符串数组);
    listview_SelectFriends.setAdapter(适配器);


解决方案

修改

  ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,字符串数组);

  //在类级别定义这个 - >私人FriendsAdapter适配器= NULL;
适配器=新FriendsAdapter(Main.this,字符串数组);

在您的活动添加此方法

 私人无效setResetSelection(INT指数,布尔setSelection){
    视图V = listview_SelectFriends.getChildAt(指数);
    如果(V!= NULL){
        TextView的名字=(TextView中)v.findViewById(R.id.name);
        如果(setSelection)
            name.setBackgroundResource(R.color.red);
        其他
            name.setBackgroundResource(R.color.transparent);
    }
}

和创建一个新的类

 公共类FriendsAdapter延伸BaseAdapter {
私人LayoutInflater mInflater;
私人的ArrayList<串GT; mFriends;
私人的ArrayList<串GT; mSelectedFriends =新的ArrayList<串GT;();公共GoodPeopleAdapter(上下文的背景下,ArrayList的<串GT;朋友){
    mInflater = LayoutInflater.from(上下文);
    mFriends =的朋友;
}公共无效setSelectedFriends(ArrayList的<串GT; selectedFriends){
    mSelectedFriends = selectedFriends;
}@覆盖
公众诠释的getCount(){
    返回mFriends.size();
}@覆盖
公共对象的getItem(INT位置){
    返回mFriends.get(位置);
}@覆盖
众长getItemId(INT位置){
    返回的位置;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    查看视图。
    ViewHolder持有人;
    如果(convertView == NULL){
        鉴于= mInflater.inflate(R.layout.row_layout,父母,假);
        持有人=新ViewHolder();
        holder.name =(TextView中)view.findViewById(R.id.name);
        view.setTag(保持器);
    }其他{
        鉴于= convertView;
        支架=(ViewHolder)view.getTag();
    }    字符串名称= mFriends.get(位置);
    holder.name.setText(名);    如果(mSelectedFriends.contains(名))
        holder.name.setBackgroundResource(R.color.red)//红色是由默认的颜色XML,变化根据您的选择    返回视图。
}私有类ViewHolder {
    公共TextView的名称;
}
}

在方法的末尾添加以下行 onItemClick

  adapter.setSelectedFriends(arr_FriendsShare);

onItemClick

的一部分,如果该添加

  setResetSelection(位置,真正的);

而这部分其他

  setResetSelection(位置,FALSE);

另外创建一个名称的新的XML布局 row_layout 与标识一个TextView 名称

I have another blocker as I study Android Development. This time my problem is when I wanted to "selectively" highlight a row in a ListView populated by data from an adapter.

This ListView is actually within a dialog, and purpose is to show a list of friends, where user can multi-select and highlight it as he selects. The selected values by the way, is stored in an ArrayList "arr_FriendsShare" so that the next time he opens the listview, rows will be highlighted (via onScrollListener) for those previously selected.

What is currently happening, only the "recently" or "last" clicked row/item is highlighted; and seems to be clearing all the previously highlighted rows.

I cannot understand why it is behaving that way, as row's value is successfully stored to/removed from arr_FriendsShare ArrayList, as I click on it.

Below is my listener codes, and thanks in advance for the usual help:

    //Item click listener for Select Friends ListView
    listview_SelectFriends.setOnItemClickListener(new OnItemClickListener()
    {
       @Override
       public void onItemClick(AdapterView<?> adapter, View v, int position,
             long arg3) 
       {
             String friends_ListItemSelected = (String)adapter.getItemAtPosition(position); 

             if(!arr_FriendsShare.contains(friends_ListItemSelected)){
                 arr_FriendsShare.add(friends_ListItemSelected);
             }
             else{
                 removeItemFromArrayListString(Main.this, arr_FriendsShare, friends_ListItemSelected);
             }
       }
    });

    listview_SelectFriends.setOnScrollListener(new OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
            for (int i = firstVisibleItem; i < (visibleItemCount + firstVisibleItem); i++) {
                String listViewItemText = view.getItemAtPosition(i).toString();
                if(arr_FriendsShare.contains(listViewItemText)){
                    ColorDrawable cd = new ColorDrawable(getResources().getColor(R.color.red_light));
                    view.setSelector(cd);
                }
                else if(arr_FriendsShare.contains(listViewItemText)){
                    ColorDrawable cd = new ColorDrawable(Color.TRANSPARENT);
                    view.setSelector(cd);
                }
            }
        }
    });

Additional Code Block:

    ArrayList<String> stringArray = new ArrayList<String>();
    String jsonURL = <SOME URL HERE>;
    stringArray = Global.getStringArrayFromJSON(Main.this, jsonURL, "friends", "FriendUsername");

    LayoutInflater inflater = getLayoutInflater();
    View convertView = (View) inflater.inflate(R.layout.friends_list_layout, null);

    ListView listview_SelectFriends = (ListView) convertView.findViewById(R.id.layout_Friends);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, stringArray);
    listview_SelectFriends.setAdapter(adapter);

解决方案

Change

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, stringArray);

to

// Define this at class level as --> private FriendsAdapter adapter = null;
adapter = new FriendsAdapter(Main.this, stringArray);

add this method in your activity

private void setResetSelection(int index, boolean setSelection){
    View v = listview_SelectFriends.getChildAt(index);
    if(v != null){
        TextView name = (TextView) v.findViewById(R.id.name);
        if(setSelection)
            name.setBackgroundResource(R.color.red);
        else
            name.setBackgroundResource(R.color.transparent);
    }
}

and create a new class as

public class FriendsAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<String> mFriends;
private ArrayList<String> mSelectedFriends = new ArrayList<String>();

public GoodPeopleAdapter(Context context, ArrayList<String> friends) {
    mInflater = LayoutInflater.from(context);
    mFriends= friends;
}

public void setSelectedFriends(ArrayList<String> selectedFriends){
    mSelectedFriends = selectedFriends;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view;
    ViewHolder holder;
    if(convertView == null) {
        view = mInflater.inflate(R.layout.row_layout, parent, false);
        holder = new ViewHolder();
        holder.name = (TextView)view.findViewById(R.id.name);
        view.setTag(holder);
    } else {
        view = convertView;
        holder = (ViewHolder)view.getTag();
    }

    String name = mFriends.get(position);
    holder.name.setText(name);

    if(mSelectedFriends.contains(name))
        holder.name.setBackgroundResource(R.color.red) // red is in color xml by default, change according to your choice

    return view;
}

private class ViewHolder {
    public TextView name;
}
}

Add following line at the end of method onItemClick

adapter.setSelectedFriends(arr_FriendsShare);

Add this in the if part of onItemClick

setResetSelection(position, true);

and this in else part

setResetSelection(position, false);

Also create a new xml layout with name row_layout with a textview with id name.

这篇关于Android版的Java:无法选择通过突出监听onScroll在ListView行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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