向下滚动后,Android ListView不设置第一项的背景颜色

时间:2016-01-19 07:05:38

标签: android android-listview

我有一个列表视图和一个自定义适配器,它可以容纳10个项目并一次显示6个项目,直到用户向下滚动显示最后几个项目。由于我的代码正在选择项目,我将一个接一个地更改每个特定项目的背景颜色,并且它工作正常,直到我点击最后一项并使用:

mailList.smoothScrollToPosition(0);

向上滚动到列表顶部。当发生这种情况时,项目0的背景颜色不会改变,然后一旦我选择了下一个项目,背景颜色会发生变化并继续完美地工作。

要更改我使用此方法的每个项目的背景颜色:

public void selectRow(int position){

    // Get the row
    View row = getViewByPosition(position, mailList);
    // Highlight background colour
    row.setBackgroundColor(ContextCompat.getColor(activity, R.color.Pallette_Peach_CC));
    // For every other row apart from 'position'
    // Check if read/unread and set background colour
    View v;
    for(int i = 0; i < mailList.getCount(); i++){
        if(i != position){
            v = getViewByPosition(i, mailList);
            if(Consts.mailBox.get(i).isRead()){
                v.setBackgroundColor(ContextCompat.getColor(activity, R.color.Ivory_Transparent_55));
            }else{
                v.setBackgroundColor(ContextCompat.getColor(activity, R.color.Ivory_Transparent_77));
            }
        }
    }

}

使用方法getViewByPosition()返回视图,以便我可以设置它的颜色:

// returns a specific row
private View getViewByPosition(int pos, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (pos < firstListItemPosition || pos > lastListItemPosition ) {
        return listView.getAdapter().getView(pos, null, listView);
    } else {
        final int childIndex = pos - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}

在第一次迭代时,项目0会改变颜色,但是一旦我到达列表的末尾(其中列表位置9(列表的末尾)最多可见3)并且smoothScroll到第一个(0)位置,项目0的颜色不会改变。要调用selectRow方法,我使用:

if(position == 0) {
    mailList.smoothScrollToPosition(0);
    selectRow(0);
}

因此它应该向上滚动到列表的顶部,调用selectRow(0)并像第一次迭代那样工作,但它似乎并没有这样做。谁能帮我吗?感谢。

编辑:

这是我的适配器类的getView方法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if(view == null){
        view = inflater.inflate(R.layout.fragment_mail_row, null);
    }

    LinearLayout rowContainer = (LinearLayout) view.findViewById(R.id.mailRowContainer);
    ImageView mailIcon = (ImageView) view.findViewById(R.id.mailRowImage);
    TextView mailTitle = (TextView) view.findViewById(R.id.mailRowTitleText);
    TextView mailDate = (TextView) view.findViewById(R.id.mailRowTimeText);

    MailItem mMailItem = mail.get(position);
    String title = mMailItem.getTitle();

    String timestamp;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    Date today = new Date();
    Date messageDate = mMailItem.getDate();
    if(sdf.format(today).equals(sdf.format(messageDate))){
        // If date of message is today then display the time
        SimpleDateFormat tf = new SimpleDateFormat("kk:mm");
        timestamp = tf.format(messageDate);
    }else{
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yy");
        timestamp = df.format(messageDate);
    }

    // If mail is already opened, re-set the background colour
    if(mMailItem.isRead()){
        rowContainer.setBackgroundColor(ContextCompat.getColor(activity, R.color.Ivory_Transparent_55));
    }else{
        rowContainer.setBackgroundColor(ContextCompat.getColor(activity, R.color.Ivory_Transparent_77));
    }

    // Set the title and time
    mailTitle.setText(title);
    mailDate.setText(timestamp);
    if(mMailItem.getType().equals(Consts.MAIL_TYPE_AUDIO)){
        if(mMailItem.isRead()){
            mailIcon.setImageResource(R.mipmap.ic_mail_audio_open);
        }else{
            mailIcon.setImageResource(R.mipmap.ic_mail_audio_closed);
        }
    }else{
        if(mMailItem.isRead()){
            mailIcon.setImageResource(R.mipmap.ic_mail_opened);
        }else{
            mailIcon.setImageResource(R.mipmap.ic_mail_closed);
        }
    }

    return view;
}

编辑2:

为了进一步说明,selectRow方法是从一个名为MailSelector的单独类中调用的,该类完整地在这里:

public class MailSelector {

    double noOfItems;
    double noOfItemsPerPage;
    int noOfPages;
    int noOfItemsOnLastPage;
    private ListView mailList;
    private Activity activity;

    public MailSelector(Activity a, ListView list){
        this.activity = a;
        this.mailList = list;
        noOfItemsPerPage = mailList.getLastVisiblePosition() - mailList.getFirstVisiblePosition();
    }

    public int nextRow(int position){
        // Calculate item/page info
        noOfItems = mailList.getCount();
        if(noOfItems <= noOfItemsPerPage ){
            // Only one page so iterate and return to start position
            // If only one item or if after last item, select position 0
            if(mailList.getChildCount() == 1 || position >= mailList.getCount()){
                selectRow(0);
                return 0;
            }
        }else{
            // More than one page so handle scrolling
            noOfPages = (int) Math.ceil(noOfItems / noOfItemsPerPage);
            // Calculate current page. Round down and + 1, as page 1 is actually 0
            int currentPage = getCurrentPage(position);
            int curPageStartPos = (int) ((int) (noOfItemsPerPage * currentPage) - noOfItemsPerPage);
            int curPageEndPos = curPageStartPos + ((int) noOfItemsPerPage - 1);
            noOfItemsOnLastPage = (int)(noOfItems % noOfItemsPerPage);

            // Scrolling if on last item of page
            if(position == curPageEndPos){
                // Check if last page
                if(currentPage == noOfPages){
                    // Scroll back to position 0
                    mailList.smoothScrollToPosition(0);
                    selectRow(0);
                    return 0;
                }else if(currentPage == noOfPages - 1){
                    // If page before last
                    // Get last position of last page
                    int lastPosOfNextPage = mailList.getCount() - 1;
                    mailList.smoothScrollToPosition(lastPosOfNextPage);
                    position = (int) ((position + 1) % noOfItems);
                    selectRow(position);
                }else{
                    int lastPosOfNextPage = (int) (position + noOfItemsPerPage);
                    mailList.smoothScrollToPosition(lastPosOfNextPage);
                    position = (int) ((position + 1) % noOfItems);
                    selectRow(position);
                }
            }else{
                position = (int) ((position + 1) % noOfItems);
                selectRow(position);
            }

        }
        if(position == 0) {
            mailList.smoothScrollToPosition(0);
            selectRow(0);
        }
        return position;
    }

    public void selectRow(int position){

        // Get the row
        View row = getViewByPosition(position, mailList);
        // Highlight background colour
        row.setBackgroundColor(ContextCompat.getColor(activity, R.color.Pallette_Peach_CC));
        // For every other row apart from 'position'
        // Check if read/unread and set background colour
        View v;
        for(int i = 0; i < mailList.getCount(); i++){
            if(i != position){
                v = getViewByPosition(i, mailList);
                if(Consts.mailBox.get(i).isRead()){
                    v.setBackgroundColor(ContextCompat.getColor(activity, R.color.Ivory_Transparent_55));
                }else{
                    v.setBackgroundColor(ContextCompat.getColor(activity, R.color.Ivory_Transparent_77));
                }
            }
        }

    }

    // returns a specific row
    private View getViewByPosition(int pos, ListView listView) {
        final int firstListItemPosition = listView.getFirstVisiblePosition();
        final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

        if (pos < firstListItemPosition || pos > lastListItemPosition ) {
            return listView.getAdapter().getView(pos, null, listView);
        } else {
            final int childIndex = pos - firstListItemPosition;
            return listView.getChildAt(childIndex);
        }
    }

    private int getCurrentPage(int position){
        return ((int) Math.floor(position / (double) noOfItemsPerPage)) + 1;
    }


}

0 个答案:

没有答案