2014-11-05 85 views
0

鉴于下面的ListView,我想根据用户选择文本(创建新活动)还是单击相关复选框(将其添加到最爱列表)。这是可能的这种设置,或者我将不得不使用自定义适配器,甚至不同的布局?ListView - 区分复选框选择和文本选择

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 
</LinearLayout> 


setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, teams)); 
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
       String team_name = adapterView.getItemAtPosition(i).toString().trim(); 
       Intent intent = new Intent("blah.blah.blah"); 
       intent.putExtra("team", team_name); 
       startActivity(intent); 
      } 
}); 

一个

回答

0

如果我正确理解你,你的ListView应该包含TextBox和CheckBox,而TextBox和CheckBox是可点击的,而不是ListView本身。

为此,您必须制作自定义适配器,您将在其中为TextBox和CheckBox创建侦听器。

public class CustomAdapter extends BaseAdapter { 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     // your_costum_view should contain textbox and checkboc 
     view = inflater.inflate(R.layout.your_costum_view, null); 

     // Get your checkbox and textbox and add listeners 
     TextView textView = (TextView) view.findById(R.id.textView); 
     textView.setOnClickListener... 

     Checkbox checkbox=(CheckBox)view.findById(R.id.checkBox); 
     checkBox.setOnClickListener... 


     return view; 
    } 

} 

your_costum_view布局例如

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

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <CheckBox 
     android:id="@+id/checkBox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 
0

使用自定义适配器,必将使您的生活更轻松。在适配器中,您可以引用复选框和textView,并为每个添加一个onClick监听器 - 您可以从中添加代码来处理每个事件。我也建议使用recyclerView而不是ListView。这是Android 5.0中的新事物,然后使用常规ListView确实更容易。希望这有助于: RecyclerView Help

0

在您的XML布局中将CheckBox设置为focusable =“false”。

android:focusable="false" 

如果不运行去to this link看到的例子,因为你需要一个在列表视图中创建自定义行并设置:

Checkbox checkbox=(CheckBox)view.findById(R.id.checkboxID); 
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      //do stuff 

     } 
    });