不断更新Android Listview但保留onClick功能

时间:2015-11-10 17:13:10

标签: android listview android-listview

我在每个Listview视图中创建一个计时器,但是当我不断调用notifyDataSetChange()时,我无法单击我的按钮。

有没有办法在不断更新时在listview视图中保留按钮和editText功能。还有其他选择吗?

谢谢!

更新:这是我正在运行以不断更新的代码

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    handler = new Handler();
    handler.post(runnable);

}
Runnable runnable = new Runnable(){
    @Override
    public void run() {
        adapterWhiteMales.notifyDataSetChanged();
        handler.postDelayed(runnable, 500);
    }
};

更新:每个列表视图中的我的按钮 没有把我的所有代码都放在getChildView和按钮上点击,但这是主要设置。

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View view, ViewGroup parentView)
{
   if (view == null) {

        viewHolderItem = new ViewHolderItem();
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.row_explistview_child_meet_athletes, parentView, false);
        viewHolderItem.btnStartLap = (Button) view.findViewById(R.id.btnStartLap);

        view.setTag(viewHolderItem);
    } else {
        // Use already initialized variables
        viewHolderItem = (ViewHolderItem) view.getTag();
    }
   viewHolderItem.btnStartLap.setOnClickListener(new View.OnClickListener()      
   {
            @Override
            public void onClick(View v) {
                Log.e("", "Start Lap Button Clicked!");
            }
   }

}

2 个答案:

答案 0 :(得分:0)

您是否尝试在UI线程中调用该代码?任务可能是暂停UI线程。尝试使用以下代码

runOnUiThread(new Runnable(){
    public void run() {
        notifyDataSetChange();
    }
});

答案 1 :(得分:0)

我的一个应用程序中存在一个问题,如果我尝试多次设置onClick侦听器,则会破坏侦听器并导致奇怪的行为。

尝试更改你的getChildView方法

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View view, ViewGroup parentView)
{
    if (view == null) {

        viewHolderItem = new ViewHolderItem();
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.row_explistview_child_meet_athletes, parentView, false);
        viewHolderItem.btnStartLap = (Button) view.findViewById(R.id.btnStartLap);
        viewHolderItem.btnStartLap.setOnClickListener(new View.OnClickListener()      
        {
            @Override
            public void onClick(View v) {
                Log.e("", "Start Lap Button Clicked!");
            }
        }
        view.setTag(viewHolderItem);
    } else {
        // Use already initialized variables
        viewHolderItem = (ViewHolderItem) view.getTag();
    }
}