2017-08-15 8 views
1

これは私の行レイアウト(rowlayout.xml)CheckedTextViewを含むカスタムリストビューにリスナーを追加し、ボタン

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/checkedTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="8dp" 
     android:layout_marginTop="8dp" 
     android:drawableLeft="?android:attr/listChoiceIndicatorMultiple" 

     android:gravity="center_vertical" 
     android:paddingRight="16dp" 
     android:text="CheckedTextView" /> 

    <Button 
     android:id="@+id/telque" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:layout_toRightOf="@id/checkedTextView" 
     android:text="telque" /> 


</RelativeLayout> 

であり、これは私の主なレイアウト(activity_work_condition.xml)

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.doctorbeingwell.doctorbeingwell.createfilepack.WorkConditions"> 


    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     android:layout_marginEnd="8dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginStart="8dp" 
     android:layout_marginTop="8dp" 
     android:layout_weight="1" 
     android:onClick="passconditions" 
     android:text="Button" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/linearLayout" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     tools:layout_editor_absoluteX="8dp" 
     tools:layout_editor_absoluteY="8dp" 
     android:id="@+id/linearLayout"> 

     <ListView 
      android:id="@+id/checkabalelist" 
      android:layout_width="match_parent" 
      android:layout_height="491dp" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintTop_toTopOf="parent" 
      android:layout_weight="0.2"/> 

    </LinearLayout> 

</android.support.constraint.ConstraintLayout> 

であり、これは私のクラス(workconditions.java):

public class WorkConditions extends Activity { 
    ArrayList<String> selectedItems = new ArrayList<>(); 
    ListView checkablelist; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_work_condition); 


     checkablelist = findViewById(R.id.checkabalelist); 
     checkablelist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
     String[] items = {"étudiant", 
       "chomeur", 
       "retraité", 
       "travail à domicile", 
       "travail au bureau"}; 

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.rowlayout, R.id.checkedTextView, items); 
     checkablelist.setAdapter(adapter); 
     CheckedTextView checkedTextView= findViewById(R.id.checkedTextView); 
     checkablelist.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 


       String selectedItem = ((CheckedTextView) view).getText().toString(); 
       if (selectedItems.contains(selectedItem)) { 
        selectedItems.remove(selectedItem); 
       } else selectedItems.add(selectedItem); 
      } 
     }); 

、これは結果であり、

result

しかしアイテムクリック可能ではなく、私は、このコードrowlayout.xmlで

String selectedItem = ((CheckedTextView) view).getText().toString(); 

答えて

0

とデータを盗んアンドロイド追加カント:(ルートノードにdescendantFocusability = "blocksDescendants"をあなたのケースではRelativeLayout)。 rowlayout.xmlで

CheckedTextViewviewボタンフォーカスを取得してからListView項目を阻止する、両方のフォーカス可能です。より多くの場合

、あなたの答えのためのandroid:descendantFocusability

+0

感謝を参照してください。私は私の結果 Log.d( "workconditions"、 "stringSelectedItems:" + stringSelectedItems)を参照するには、ログを追加しました。 結果を選択できますが、レイアウトで描画可能な部分はチェックされずに残ります –

+0

画面:https://ibb.co/ioE1Ba –

+0

@testerNew別の質問をしています。いずれにせよ、それは私を混乱させます。クリックしたい** CheckedTextView **、または** CheckedTextView **と** Button **を含むリストビュー項目? ** CheckedTextView **のclickイベントに応答するには、その上にクリックリスナーを指定する必要があります。しかし、私はあなたのコードでそのような論理を見つけられませんでした。 – cmoaciopm

関連する問題