带有ListAdapter的Android ListView,需要始终有一个选定的条目



我有一个ListView(lvProperties),它的适配器是自定义ListAdapter(而不是ArrayAdapter)。此适配器从自定义类型Section的Instance变量(包含一些Strings、Integers、ArrayLists和方法)获取is数据。

我需要这个列表来始终显示一个选择(必须始终有一个选定的元素,即使在活动启动时也是如此)。

在过去使用简单的ArrayAdapter时,这样的东西就足够了:

lvProperties.requestFocus();
lvProperties.setSelection(0);

然而,在这种情况下,它根本不起作用。我一直在寻找周围的SO和网络和使用:

lvProperties.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvProperties.setFocusable(true);            
lvProperties.setFocusableInTouchMode(true);
lvProperties.requestFocus();
lvProperties.setSelection(0);

但仍然一无所获。

我的列表适配器:

    lvProperties.setAdapter(new ListAdapter() {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v=convertView;
                if(v==null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v=vi.inflate(R.layout.detail_row1, null);
                }
                TextView name=(TextView)v.findViewById(R.id.propName);
                TextView value=(TextView)v.findViewById(R.id.propValue);
                if(name!=null) {
                    name.setText(section.propNames.get(position));
                }
                if(value!=null) {
                value.setText(section.propValues.get(position));
                }
                return v;
            }
        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {}
        @Override
        public void registerDataSetObserver(DataSetObserver observer) {}
        @Override
        public boolean isEmpty() {
            return false;
        }
        @Override
        public boolean hasStableIds() {
            return false;
        }
        @Override
        public int getViewTypeCount() {
            return 2;
        }
        @Override
        public int getItemViewType(int position) {
            return 0;
        }
        @Override
        public long getItemId(int position) {
            return 0;
        }
        @Override
        public Object getItem(int position) {
            return null;
        }
        @Override
        public int getCount() {
            return section.propNames.size();
        }
        @Override
        public boolean isEnabled(int position) {
            return true;
        }
        @Override
        public boolean areAllItemsEnabled() {
            return true;
        }
    });

我未实现的OnItemSelectedListener:

    lvProperties.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });

我的detail_row1.xml(我接下来将实现行的布局,但现在就是这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:orientation="horizontal" 
  android:layout_height="wrap_content"
  android:padding="6dip"
  >
  <TextView
    android:id="@+id/propName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"   
    android:layout_weight="1"    
    android:textSize="16sp"
    android:textStyle="bold" 
    android:textColor="#000000"
    android:gravity="center_vertical|left"
    android:paddingTop="10sp"
    android:paddingBottom="10sp"
    android:paddingLeft="4sp"
     />
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:id="@+id/propValue"       
    android:textColor="#000000"
    android:textSize="16sp"
    android:textStyle="bold"
    android:paddingTop="10sp"
    android:paddingBottom="10sp"
    android:paddingRight="4sp"
    android:gravity="center_vertical|right"
    />  
</LinearLayout><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:orientation="horizontal" 
  android:layout_height="wrap_content"
  android:padding="6dip"
  >
  <TextView
    android:id="@+id/propName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"   
    android:layout_weight="1"    
    android:textSize="16sp"
    android:textStyle="bold" 
    android:textColor="#000000"
    android:gravity="center_vertical|left"
    android:paddingTop="10sp"
    android:paddingBottom="10sp"
    android:paddingLeft="4sp"
    />
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:id="@+id/propValue"       
    android:textColor="#000000"
    android:textSize="16sp"
    android:textStyle="bold"
    android:paddingTop="10sp"
    android:paddingBottom="10sp"
    android:paddingRight="4sp"
    android:gravity="center_vertical|right"
    />  
</LinearLayout>

关于如何做到这一点,有什么帮助/指南吗?我不想使用drawable,只是我正在使用的主题的默认选择器。

同样,列表中必须始终有一个选定的条目。

我意识到,从设计上讲,这不是列表的预期行为,但这是一个大小不适合所有的情况之一

对于所有感兴趣的人,我只是复制了我所需要的,需要处理ChoiceMode、requestFocus、setSelection等,在API 8以上的中运行良好

当应用程序启动时,我希望第一行已经突出显示:

selectedItem.setValue(0)//我有一个内部类,也可以是一个简单的整数

我的内部类数组Dapter(我需要它一直看到父级selectedItem):

注意:接下来我将更改硬编码的颜色,我刚刚解决了我的问题:

public class DetailsAdapter extends ArrayAdapter<Section> {
    public Section section;
    public View v;
    public Context c;
    public DetailsAdapter(Context context, int textViewResourceId,
            Section s) {
        super(context, textViewResourceId);
        this.section=s;
        this.c=context;
    }
    @Override
    public int getCount() {
        return section.propNames.size();
    }
    @Override
    public View getView(int pos, View convertView, ViewGroup parent){
        this.v = convertView;
        LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(v==null) {     
            v=vi.inflate(R.layout.detail_row1, null); 
        }
        **LinearLayout llc=(LinearLayout) v.findViewById(R.id.detail_container);**
        TextView name=(TextView)v.findViewById(R.id.propName);
        TextView value=(TextView)v.findViewById(R.id.propValue);
        if(pos==selectedItem.value) {
           llc.setBackgroundColor(Color.LTGRAY);
        } else
        { llc.setBackgroundColor(Color.WHITE);}
        Log.e("Pos",""+pos);
        if(llc!=null) {
        }
        if(name!=null) {
            name.setText(section.namesAsArray()[pos]);
        }
        if(value!=null) {
            value.setText(section.valuesAsArray()[pos]);
        }

        return v;
    }
}

在我的XML文件中,我可以避免android:state_xxxxxxx的技巧是用嵌套的LinearLayout来包装我的自定义行,填充外部行。

行的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal" 
android:layout_height="wrap_content"
android:id="@+id/detail_linearLayout"
>
<LinearLayout 
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_weight="0"
   android:id="@+id/detail_container"
     android:padding="6dip"
   >
<TextView 
android:id="@+id/propName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"   
android:layout_weight="1"    
android:textSize="16sp"
android:textStyle="bold" 
android:textColor="#000000"
android:gravity="center_vertical|left"
android:paddingTop="10sp"
android:paddingBottom="10sp"
android:paddingLeft="4sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:id="@+id/propValue"       
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"
android:paddingTop="10sp"
android:paddingBottom="10sp"
android:paddingRight="4sp"
android:gravity="center_vertical|right"
/>  
</LinearLayout>
</LinearLayout>

最后,在上,我设置了OnItemClickListener:

    lvProperties.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
                **selectedItem.value=arg2;
                lvProperties.invalidateViews();**
                        .
                                    .
                                    .
                     }});

完成了,完成了!

相关内容

最新更新