Android列表视图在小屏幕设备上选择的第一个和最后一个项目

时间:2023-01-19 10:45:42

I have a problem with my ListView on devices with small screen, when we click on first item of the list and we scroll to the end, we see the last item selected with the color of the first item.

我的ListView在有小屏幕的设备上有问题,当我们点击列表的第一项并滚动到结尾时,我们看到最后一项选择了第一项的颜色。

Part of my adapter code :

部分适配器代码:

public static class ViewHolder {
    TextView mItemTitleView;
}

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

    viewHolder = new ViewHolder();
    if (convertView == null) {
        convertView = mInflater.inflate(this.mLayoutId, parent, false);

        viewHolder.mItemTitleView = (TextView) convertView.findViewById(R.id.itemtitle);

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    Typeface futura_font = Typeface.createFromAsset(getContext().getAssets(), "fonts/Futura.ttf");
    viewHolder.mRadioNameView.setTypeface(futura_font);

    viewHolder.mItemTitleView.setText("Something");

    return convertView;
}

Device for test : Samsung Galaxy S2 on Android 2.3.7.

测试设备:Android 2.3.7上的三星Galaxy S2。

Screenshots :

  • Top of the list and first item is selected : http://i.stack.imgur.com/ClDZe.jpg
  • 列表的顶部和第一项被选中:http://i.stack.imgur.com/ClDZe.jpg

  • After scroll, we see the last item is selected but we haven't clicked on it : http://i.stack.imgur.com/mU0ak.jpg
  • 滚动后,我们看到最后一项被选中,但我们没有点击它:http://i.stack.imgur.com/mU0ak.jpg

2 个解决方案

#1


I had this exact problem a week ago. the code you use to highlight your item has to be in your getView function and not outside of it, because getView is called everytime you scroll.

一周前我遇到了这个问题。用于突出显示项目的代码必须位于getView函数中,而不是在其外部,因为每次滚动时都会调用getView。

add it like so:

像这样添加它:

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

    viewHolder = new ViewHolder();
    if (convertView == null) {
        convertView = mInflater.inflate(this.mLayoutId, parent, false);

        viewHolder.mItemTitleView = (TextView) convertView.findViewById(R.id.itemtitle);

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    Typeface futura_font = Typeface.createFromAsset(getContext().getAssets(), "fonts/Futura.ttf");
    viewHolder.mRadioNameView.setTypeface(futura_font);

    viewHolder.mItemTitleView.setText("Something");
    if(getItemAtPosition(position).isHighlighted){
        //add hightlighting to item like you did outside of getView
        //in your app, highlighting an item makes its text red
        //so you should put the code that makes the text red here
     }
    return convertView;
}

this will work if for every item you saved a boolean higlighted variable.

如果您保存了布尔值更高的变量的每个项目,这将有效。

after you add this, getView will not mistakenly add highlighting to your last item.

添加后,getView不会错误地将突出显示添加到您的上一个项目。

#2


I had faced the same, you can create an anim folder in res folder, see the code below create a file name say , bg_press.xml

我遇到过同样的问题,你可以在res文件夹中创建一个anim文件夹,看下面的代码创建一个文件名说,bg_press.xml

 <?xml version="1.0" encoding="utf-8"?> 
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true"
 android:drawable="@drawable/bg_press_default" /> <!-- pressed -->
 <item android:state_focused="true"
 android:drawable="@drawable/bg_item_list" /> <!-- focused -->
 <item  android:drawable="@drawable/bg_item" /> <!-- default -->
</selector>

Note : you can also use colours in place of image as background in this file.

注意:您也可以使用颜色代替图像作为此文件中的背景。

in your row xml use this as,

在你的行xml中使用它,

 android:background="bg_press"

Hope this will help you

希望对你有帮助

#1


I had this exact problem a week ago. the code you use to highlight your item has to be in your getView function and not outside of it, because getView is called everytime you scroll.

一周前我遇到了这个问题。用于突出显示项目的代码必须位于getView函数中,而不是在其外部,因为每次滚动时都会调用getView。

add it like so:

像这样添加它:

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

    viewHolder = new ViewHolder();
    if (convertView == null) {
        convertView = mInflater.inflate(this.mLayoutId, parent, false);

        viewHolder.mItemTitleView = (TextView) convertView.findViewById(R.id.itemtitle);

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    Typeface futura_font = Typeface.createFromAsset(getContext().getAssets(), "fonts/Futura.ttf");
    viewHolder.mRadioNameView.setTypeface(futura_font);

    viewHolder.mItemTitleView.setText("Something");
    if(getItemAtPosition(position).isHighlighted){
        //add hightlighting to item like you did outside of getView
        //in your app, highlighting an item makes its text red
        //so you should put the code that makes the text red here
     }
    return convertView;
}

this will work if for every item you saved a boolean higlighted variable.

如果您保存了布尔值更高的变量的每个项目,这将有效。

after you add this, getView will not mistakenly add highlighting to your last item.

添加后,getView不会错误地将突出显示添加到您的上一个项目。

#2


I had faced the same, you can create an anim folder in res folder, see the code below create a file name say , bg_press.xml

我遇到过同样的问题,你可以在res文件夹中创建一个anim文件夹,看下面的代码创建一个文件名说,bg_press.xml

 <?xml version="1.0" encoding="utf-8"?> 
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true"
 android:drawable="@drawable/bg_press_default" /> <!-- pressed -->
 <item android:state_focused="true"
 android:drawable="@drawable/bg_item_list" /> <!-- focused -->
 <item  android:drawable="@drawable/bg_item" /> <!-- default -->
</selector>

Note : you can also use colours in place of image as background in this file.

注意:您也可以使用颜色代替图像作为此文件中的背景。

in your row xml use this as,

在你的行xml中使用它,

 android:background="bg_press"

Hope this will help you

希望对你有帮助