如何获得ListView的焦点儿童?

时间:2021-08-03 07:33:33

I have a ListView in which i add row items dynamically. Each row has a no of childs. Now i am setting on click listeners on these childs, and in them it asks me to keep these child items final, it works fine with them and it is ok to do this as i am creating a class in each on click listener.

我有一个ListView,我在其中动态添加行项目。每行都没有孩子。现在我在这些孩子上设置点击监听器,并在其中要求我保持这些子项最终,它可以正常使用它们可以这样做,因为我在每个点击监听器上创建一个类。

Here isFree is a final checkBox.

这里是免费的最终复选框。

isFree.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
        // TODO Auto-generated method stub
        if (isFree.isChecked()){...}
    }
});

But i dont feel it as a good programming practice, i want to use some function which should detect which child was clicked and then i should be able to access its row using getParent and then its sibling items using getChildAt(index) So, i used this:

但我不认为这是一个很好的编程实践,我想使用一些功能,应该检测哪个孩子被点击,然后我应该能够使用getParent访问其行,然后使用getChildAt(索引)访问其兄弟项目所以,我用过这个:

isFree.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
        // TODO Auto-generated method stub
        CheckBox tv=(CheckBox) getCurrentFocus();
        TableRow row = (TableRow)tv.getParent();
        TextView tv1=(TextView) row.getChildAt(5);
        if (tv.isChecked()){...}
    }
});

But when i use it, on focussed item actualy returns the listview, but i want focused item in the row.

但是当我使用它时,在聚焦项目上实际返回列表视图,但我想要在行中聚焦项目。

2 个解决方案

#1


0  

you can get which child of listview was clicked with onItemClickListener, but if you want to do some action on a row which has some checkbox or some other clickable view and you want to perform some action on those click then i think you have to use click listeners on those views itself.

您可以使用onItemClickListener来查看listview的哪个子项,但是如果您想对具有某个复选框或其他可点击视图的行执行某些操作,并且您希望对这些点击执行某些操作,那么我认为您必须使用click这些观点本身的听众。

but if you want to get listitem row at click of those individual views, you can use setTag method of view class to store the index and then getting the row item from that index on your click listeners.

但是如果要在单击这些单独视图时获取listitem行,则可以使用viewTag方法来存储索引,然后从单击侦听器中获取该索引中的行项。

public view getView(int position, View convertView, ..) {
  ....
  ....
  ....
  // your code
  convertView.setTag(position);
  return convertView;
}

isFree.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
        // TODO Auto-generated method stub
        if (arg0.isChecked()){
            int index = arg0.getTag();
            View v = listview.getItemAtPosition(index);
            // or  get the adapter get data list from it and then index it.

        }
    }
});

NOTE that: I havent used final free inside oncheckedchange method.

注意:我没有在oncheckedchange方法中使用final final。

#2


0  

On the setOnCheckedChangeListener method, the first parameter is a CompoundButton that is a super-class of CheckBox. So, actually, arg0 is your checkbox. Use it to retrieve the parent list item and make your stuff.

在setOnCheckedChangeListener方法中,第一个参数是CompoundButton,它是CheckBox的超类。所以,实际上,arg0是你的复选框。用它来检索父列表项并制作你的东西。

isFree.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
        // TODO Auto-generated method stub
        TableRow row = (TableRow)arg0.getParent();
        TextView tv1=(TextView) row.getChildAt(5);
        if (arg0.isChecked()){...}
    }
});

#1


0  

you can get which child of listview was clicked with onItemClickListener, but if you want to do some action on a row which has some checkbox or some other clickable view and you want to perform some action on those click then i think you have to use click listeners on those views itself.

您可以使用onItemClickListener来查看listview的哪个子项,但是如果您想对具有某个复选框或其他可点击视图的行执行某些操作,并且您希望对这些点击执行某些操作,那么我认为您必须使用click这些观点本身的听众。

but if you want to get listitem row at click of those individual views, you can use setTag method of view class to store the index and then getting the row item from that index on your click listeners.

但是如果要在单击这些单独视图时获取listitem行,则可以使用viewTag方法来存储索引,然后从单击侦听器中获取该索引中的行项。

public view getView(int position, View convertView, ..) {
  ....
  ....
  ....
  // your code
  convertView.setTag(position);
  return convertView;
}

isFree.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
        // TODO Auto-generated method stub
        if (arg0.isChecked()){
            int index = arg0.getTag();
            View v = listview.getItemAtPosition(index);
            // or  get the adapter get data list from it and then index it.

        }
    }
});

NOTE that: I havent used final free inside oncheckedchange method.

注意:我没有在oncheckedchange方法中使用final final。

#2


0  

On the setOnCheckedChangeListener method, the first parameter is a CompoundButton that is a super-class of CheckBox. So, actually, arg0 is your checkbox. Use it to retrieve the parent list item and make your stuff.

在setOnCheckedChangeListener方法中,第一个参数是CompoundButton,它是CheckBox的超类。所以,实际上,arg0是你的复选框。用它来检索父列表项并制作你的东西。

isFree.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
        // TODO Auto-generated method stub
        TableRow row = (TableRow)arg0.getParent();
        TextView tv1=(TextView) row.getChildAt(5);
        if (arg0.isChecked()){...}
    }
});