2017-07-23 46 views
0

現在我的代碼使用R.佈局。 simple_list_item_multiple_choice,它的工作完美的罰款。因爲我還需要第二TextView的,我試圖創建具有2的TextView和1個複選框我自己的XML。Android的列表視圖多列有一個複選框

我需要在我的列表視圖2動作:OnClickListener將打開一個新的活動,OnLongClickLisener將檢查/取消選中。

這是問題所在,如果我添加一個複選框,我失去了點擊我的ListView的能力,我只能點擊複選框。

這裏是我的代碼一個簡單的版本:(現在我還沒有改變的Checkmark代碼,因爲我不能讓它與我的新3列版本工作)

Main2Acitvity:

if (POSITION == 1){ 

     setContentView(R.layout.activity_main2); 
     Log.d(TAG, "onCreate: Started."); 
     mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(mActionBarToolbar); 
     getSupportActionBar().setTitle("Stigmate"); 

     ImageView = (ImageView)findViewById(R.id.imageView2); 
     ImageView.setImageResource(R.drawable.r_stigmate); 
     listView = (ListView)findViewById(R.id.listView); 

     Person john = new Person("Première cicatrice","V.0"); 
     Person john1 = new Person("Première cicatrice","V.0"); 
     Person john2= new Person("Première cicatrice","V.0"); 
     Person john3 = new Person("Première cicatrice","V.0"); 

     ArrayList<Person> peopleList = new ArrayList<>(); 
     peopleList.add(john); 
     peopleList.add(john1); 
     peopleList.add(john2); 
     peopleList.add(john3); 

     PersonListAdapter adapter = new PersonListAdapter(this, R.layout.adapter_view_layout, peopleList); 


     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      } 
     }); 
     listView.setAdapter(adapter); 

     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int i, long id) { 
       if (Var[i] == 0){ 
        listView.setItemChecked(i,false); 
       } 
       else if (Var[i] == 1){ 
        listView.setItemChecked(i,true); 
       } 
       Intent intent = new Intent(Main2Activity.this, Main3Activity.class); 
       intent.putExtra("CountryName",listView.getItemAtPosition(i).toString()); 
       startActivity(intent); 

       int n = 0; 
       for (n= 0; n < 20 ; n++) { 
        if(i==n){ 
         Main3Activity.POSITION3 = n; 
        }} 
      } 

     }); 
     listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
      @Override 

      public boolean onItemLongClick(AdapterView<?> arg0, View arg1, 
              int pos, long id) { 
       if (Var[pos] == 1){ 
        listView.setItemChecked(pos,false); 
        Var[pos] = 0; 
       } 
       else if (Var[pos] == 0){ 
        listView.setItemChecked(pos,true); 
        Var[pos] = 1; 
       } 
       else { 
        listView.setItemChecked(pos,true); 
       Var[pos] = 0; 
       } 
       Var0 = Var[0]; 
       Var1 = Var[1]; 
       Var2 = Var[2]; 
       Var3 = Var[3]; 
       Var4 = Var[4]; 

       SharedPreferences saveinfo = getSharedPreferences(SAVE, 0); 
       SharedPreferences.Editor editor = saveinfo.edit(); 
       editor.putInt("SavedStigmate1",Var0).putInt("SavedStigmate2",Var1).putInt("SavedStigmate3",Var2).putInt("SavedStigmate4",Var3).putInt("SavedStigmate5",Var4); 
       editor.apply(); 
       return true; 
      } 
     }); 




     // here you should set checked items 

     SharedPreferences loadinfo = getSharedPreferences(SAVE,0); 
     Var0 = loadinfo.getInt("SavedStigmate1",0); 
     Var1 = loadinfo.getInt("SavedStigmate2",0); 
     Var2 = loadinfo.getInt("SavedStigmate3",0); 
     Var3 = loadinfo.getInt("SavedStigmate4",0); 
     Var4 = loadinfo.getInt("SavedStigmate5",0); 
     Var[0] = Var0; 
     Var[1] = Var1; 
     Var[2] = Var2; 
     Var[3] = Var3; 
     Var[4] = Var4; 

     int iii; 
     for (iii=0; iii<10; iii++){ 
      if (Var[iii]== 1) 
       listView.setItemChecked(iii,true); 
      else if (Var[iii]== 0) 
       listView.setItemChecked(iii,false); 
      else 
       listView.setItemChecked(iii,false); 

     } 

    } 

人代碼:

package com.carte.alex.test3; 


/** 
* Created by User on 3/14/2017. 
*/ 

public class Person { 
    private String name; 
    private String birthday; 


    public Person(String name, String birthday) { 
     this.birthday = birthday; 
     this.name = name; 

    } 

    public String getBirthday() { 
     return birthday; 
    } 

    public void setBirthday(String birthday) { 
     this.birthday = birthday; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

} 

PersonListAdapter:

package com.carte.alex.test3; 

import android.content.Context; 
import android.support.annotation.NonNull; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Created by User on 3/14/2017. 
*/ 

public class PersonListAdapter extends ArrayAdapter<Person> { 

    private static final String TAG = "PersonListAdapter"; 

    private Context mContext; 
    private int mResource; 
    private int lastPosition = -1; 

    /** 
    * Holds variables in a View 
    */ 
    private static class ViewHolder { 
     TextView name; 
     TextView birthday; 

    } 

    /** 
    * Default constructor for the PersonListAdapter 
    * @param context 
    * @param resource 
    * @param objects 
    */ 
    public PersonListAdapter(Context context, int resource, ArrayList<Person> objects) { 
     super(context, resource, objects); 
     mContext = context; 
     mResource = resource; 
    } 

    @NonNull 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     //get the persons information 
     String name = getItem(position).getName(); 
     String birthday = getItem(position).getBirthday(); 


     //Create the person object with the information 
     Person person = new Person(name,birthday); 

     //create the view result for showing the animation 
     final View result; 

     //ViewHolder object 
     ViewHolder holder; 


     if(convertView == null){ 
      LayoutInflater inflater = LayoutInflater.from(mContext); 
      convertView = inflater.inflate(mResource, parent, false); 
      holder= new ViewHolder(); 
      holder.name = (TextView) convertView.findViewById(R.id.textView1); 
      holder.birthday = (TextView) convertView.findViewById(R.id.textView2); 


      result = convertView; 

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




     holder.name.setText(person.getName()); 
     holder.birthday.setText(person.getBirthday()); 


     return convertView; 



    } 
} 

Adapter_view_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="100"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="90" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="match_parent" 
      android:layout_height="50dp" 
      android:gravity="center_vertical|left" 
      android:paddingBottom="8dp" 
      android:paddingLeft="15dp" 
      android:paddingTop="8dp" 
      android:text="TextView1" 
      android:textAppearance="@style/TextAppearance.AppCompat.Body1" 
      android:textSize="16sp" /> 


    </LinearLayout> 


    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="12.36"> 

     <TextView 

      android:id="@+id/textView2" 
      android:layout_width="60dp" 
      android:layout_height="50dp" 
      android:gravity="center_vertical|left" 
      android:paddingBottom="8dp" 
      android:paddingLeft="12dp" 
      android:paddingTop="8dp" 
      android:text="TextView2" /> 


    </LinearLayout> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <CheckBox 
      android:id="@+id/checkBox1" 
      android:layout_width="wrap_content" 
      android:layout_height="50dp" 
      android:gravity="center" 
      android:paddingLeft="20dp" 
      android:paddingTop="8dp" 
      android:paddingBottom="8dp" 
      android:theme="@style/WhiteCheck"/> 


    </LinearLayout> 

</LinearLayout> 

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?> 



<RelativeLayout 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:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.carte.alex.test3.MainActivity"> 

    <android.support.v7.widget.Toolbar 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/toolbar" 

     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:fitsSystemWindows="true" 
     android:minHeight="?attr/actionBarSize" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     android:background="?attr/colorPrimary"> 

    </android.support.v7.widget.Toolbar> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"> 

    </android.support.v4.view.ViewPager> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="232dp" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentStart="true" 
     android:layout_below="@+id/toolbar" 
     android:scaleType="fitXY" 
     /> 

    <ListView 
     android:id="@+id/listView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_below="@+id/imageView2" 
     android:longClickable="true" 
     android:checkMark="@color/red" 
    /> 


</RelativeLayout> 

Here is the result on an emulator

回答

0

這裏的問題是,如果我添加一個複選框,我失去了點擊 對我的能力ListView和我只可以點擊複選框。

  1. 添加android:descendantFocusability="blocksDescendants"到RelativeLayout的你activity_main2.xml的

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
         ..... 
        android:descendantFocusability="blocksDescendants 
        tools:context="com.carte.alex.test3.MainActivity"> 
    
  2. 在複選框元素添加android:focusable="false"