问题
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