Updating Android Listview Constantly but Retain onClick functionality

試著忘記壹切 提交于 2019-12-12 01:57:16

问题


I am creating a timer in each Listview view but when I call notifyDataSetChange() constantly, I am unable to click my buttons.

Is there a way to retain button and editText functionality inside a listview view when updated constantly. Is there any alternatives?

Thanks!

UPDATE: This is the code I am running to update continously

@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);
    }
};

UPDATE: My buttons in each listview view Didn't put all my code for the getChildView and button on click, but this is the main setup.

@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!");
            }
   }

}

回答1:


Did you try calling that code in UI thread? Task could be pausing UI thread. Try using code below

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



回答2:


I had an issue in one of my apps where if I tried to set the onClick listener multiple times it would break the listener and cause weird behavior.

Try changing your getChildView method like so

@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();
    }
}


来源:https://stackoverflow.com/questions/33635722/updating-android-listview-constantly-but-retain-onclick-functionality

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!