0

アダプターがカスタム ListAdapter (ArrayAdapter ではない) である ListView (lvProperties) があります。このアダプターは、カスタム タイプ セクション (いくつかの文字列、整数、ArrayList、およびメソッドを含む) のインスタンス変数からデータを取得します。

常に選択を表示するには、このリストが必要です (アクティビティの起動時でも、選択された要素が常に存在する必要があります)。

過去に単純な ArrayAdapter を使用する場合、次のようなもので十分でした:

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

ただし、この場合はまったく機能しません。私はSOとWebを見回して使用しました:

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>

これを達成する方法についてのガイドへのヘルプ/ポインタはありますか? ドローアブルを使用したくありません。使用しているテーマのデフォルトのセレクターだけです。

繰り返しますが、リストには常に 1 つのエントリが選択されている必要があります。

設計上、これはリストの意図した動作ではないことを認識していますが、これは 1 つのサイズがすべてに適合しないケースの 1 つです。

4

1 に答える 1

1

興味のある他のすべての人のために、ChoiceMode、requestFocus、setSelectionなどをいじる必要があるものを複製しただけで、API8以降で正常に動作します

アプリが起動したときに、最初の行がすでに強調表示されているようにします。

selectedItem.setValue(0)//私が持っている内部クラスは、単純な整数でもかまいません

私の内部クラスArrayDapter(親の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();**
                        .
                                    .
                                    .
                     }});

やった、やった!

于 2012-08-23T23:15:43.667 に答える