采用Android,如何从一个ListView,其中包含Button控件选择行控件、其中包含、Android、ListView

2023-09-12 09:21:09 作者:心瘾成病

我有以下的,非常简单的测试程序,使用一个ListView。我创建了一个ListView并将其设置为内容视图。我设置了ListAdapter它提供行。有30行,每一行包含一个的LinearLayout的ViewGroup的。成ViewGroup中,我把一个TextView和一个按钮。当我运行程序时,我发现我不能选择行之列。我可以,但是,滚动列表,然后单击按钮。

如果我删除了的LinearLayout按钮(以便它仅包含TextView的),那么我可以选择行之列。我会 希望能够对我个人的看法排按键,并且仍然能够选择行之列。在另一个论坛上,有人说,这是可能的,但我很茫然,如何完成它。

谁能给我一个线索?

感谢。

 公共类ListViewTest扩展活动实现ListAdapter
{
 INT m_count;
 DataSetObserver m_observer;

 公共ListViewTest()
 {
  m_count = 30;
  m_observer = NULL;
 }

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        ListView的LV =新的ListView(本);
        lv.setAdapter(本);
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        的setContentView(LV);
   }

 @覆盖
 公共布尔areAllItemsEnabled(){
  返回true;
 }

 @覆盖
 公共布尔的IsEnabled(INT位置){
  返回true;
 }

 @覆盖
 公众诠释getCount将()
 {
  返回m_count;
 }

 @覆盖
 公共对象的getItem(INT位置){
  返回null;
 }

 @覆盖
 众长getItemId(INT位置){
  返回0;
 }

 @覆盖
 公众诠释getItemViewType(INT位置){
  返回0;
 }

 @覆盖
 公共查看getView(INT位置,查看convertView,父母的ViewGroup)
 {
  LinearLayout中VG =新的LinearLayout(本);

  TextView的电视=新的TextView(本);
  tv.setText(列表项);

  按钮BV =新的按钮(这一点);
  bv.setText(按钮);

  vg.addView(电视);
  vg.addView(BV);

  返回(VG);
 }

 @覆盖
 公众诠释getViewTypeCount(){
  返回1;
 }

 @覆盖
 公共布尔hasStableIds(){
  返回false;
 }

 @覆盖
 公共布尔的isEmpty(){
  返回false;
 }

 @覆盖
 公共无效registerDataSetObserver(DataSetObserver观察者)
 {
  m_observer =观测;
 }

 @覆盖
 公共无效unregisterDataSetObserver(DataSetObserver观察者){

 }
}
 

解决方案

至于其他的答案指出,不管是不是可以选择的ListView行作为自己的整个项目取决于这些行是否不包含可获得焦点项目。然而,该解决方案通常是不设置可聚焦=假上的按钮或类似物。这将prevent您的应用程序被通航了D-垫,轨迹球,箭头键,或你有什么。

Android学习八 ListView的高级使用

您希望您的列表中的项目要能够控制自己的焦点属性。你想setItemsCanFocus.这将禁用特殊聚焦/选择处理该ListView的通常使用来治疗列表项作为一个单一的单元。

现在你可以设置你在你行的顶级元素使用的布局侦听器,在其上设置有状态的背景绘制显示焦点/ preSS状态,以及focusLeft /右键属性来控制聚焦项目本身内移动。

I have the following, very simple test program for using a ListView. I create a ListView and set it as the content view. I set a ListAdapter which supplies the rows. There are 30 rows, and each row consists of a LinearLayout ViewGroup. Into that ViewGroup, I place a TextView and a Button. When I run the program, I find that I cannot select rows of the list. I can, however, scroll the list and click the button.

If I remove the button from the LinearLayout (so that it contains only the TextView), then I am able to select rows of the list. I would like to be able to have buttons on my individual row views, and still be able to select rows of the list. On another forum, someone said that this was possible, but I am at a loss as to how to accomplish it.

Can anyone give me a clue?

Thanks.

public class ListViewTest extends Activity implements ListAdapter 
{
 int    m_count;
 DataSetObserver m_observer;

 public ListViewTest()
 {
  m_count  = 30;
  m_observer = null;
 }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        ListView lv = new ListView(this);
        lv.setAdapter(this);
        lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        setContentView(lv);
   }

 @Override
 public boolean areAllItemsEnabled() {
  return true;
 }

 @Override
 public boolean isEnabled(int position) {
  return true;
 }

 @Override
 public int getCount() 
 {
  return m_count;
 }

 @Override
 public Object getItem(int position) {
  return null;
 }

 @Override
 public long getItemId(int position) {
  return 0;
 }

 @Override
 public int getItemViewType(int position) {
  return 0;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) 
 {
  LinearLayout vg = new LinearLayout(this);

  TextView tv = new TextView(this);
  tv.setText("ListItem");

  Button bv = new Button(this);
  bv.setText("Button");

  vg.addView(tv);
  vg.addView(bv);

  return(vg);
 }

 @Override
 public int getViewTypeCount() {
  return 1;
 }

 @Override
 public boolean hasStableIds() {
  return false;
 }

 @Override
 public boolean isEmpty() {
  return false;
 }

 @Override
 public void registerDataSetObserver(DataSetObserver observer) 
 {
  m_observer = observer;
 }

 @Override
 public void unregisterDataSetObserver(DataSetObserver observer) {

 }
}

解决方案

As the other answers point out, whether or not you can select ListView rows as full items on their own depends on whether or not those rows contain focusable items. However, the solution is usually not setting focusable=false on your buttons or the like. That will prevent your app from being navigable with a d-pad, trackball, arrow keys, or what have you.

You want your list items to be able to control their own focus properties. You want setItemsCanFocus. This will disable the special focus/selection handling that ListView normally uses to treat list items as a single unit.

Now you can set a listener on the layout you use as the top-level element in your rows, set a stateful background drawable on it to display focus/press state, as well as focusLeft/Right properties to control focus shifting within the item itself.