如何在更改适配器数据之前暂时停止单击效果?(How to temporarily stop clicking effect before changing adapter data?)

编程入门 行业动态 更新时间:2024-10-24 10:18:24
如何在更改适配器数据之前暂时停止单击效果?(How to temporarily stop clicking effect before changing adapter data?)

背景

从listView中选择项目时,我更改其数据并调用notifyDataSetChanged。

问题

由于它是相同的listView,当我单击该项时,效果将保留在notifyDataSetChanged之后将使用的视图。

这在Android Lollipop上尤为明显,在使用新数据刷新listView之后,纹波可以继续。

代码

以下是显示问题的示例代码:

public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView=(ListView)findViewById(R.id.listView); listView.setAdapter(new BaseAdapter() { int pressCount=0; @Override public int getCount() { return 100; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position,View convertView,ViewGroup parent) { View rootView=convertView; if(rootView==null) { rootView=LayoutInflater.from(MainActivity.this).inflate(android.R.layout.simple_list_item_1,parent,false); rootView.setBackgroundResource(getResIdFromAttribute(MainActivity.this,android.R.attr.selectableItemBackground)); rootView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pressCount++; notifyDataSetChanged(); } }); } TextView tv=(TextView)rootView; tv.setText("text:"+(pressCount+position)); return rootView; } }); } public static int getResIdFromAttribute(final Activity activity,final int attr) { if(attr==0) return 0; final TypedValue typedvalueattr=new TypedValue(); activity.getTheme().resolveAttribute(attr,typedvalueattr,true); return typedvalueattr.resourceId; } }

这个问题

如何暂时停止选择效果,直到下次在listView上单击任何内容时(但也会在下次用户单击某个项目时恢复允许它)?

background

When choosing an item from a listView, I change its data and call notifyDataSetChanged.

The problem

Since it's the same listView, when I click the item, the effect stays for the view that will be used after the notifyDataSetChanged.

This is especially noticeable on Android Lollipop, where the ripple can continue after the listView gets refreshed with new data.

The code

Here's a sample code showing the problem:

public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView listView=(ListView)findViewById(R.id.listView); listView.setAdapter(new BaseAdapter() { int pressCount=0; @Override public int getCount() { return 100; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position,View convertView,ViewGroup parent) { View rootView=convertView; if(rootView==null) { rootView=LayoutInflater.from(MainActivity.this).inflate(android.R.layout.simple_list_item_1,parent,false); rootView.setBackgroundResource(getResIdFromAttribute(MainActivity.this,android.R.attr.selectableItemBackground)); rootView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pressCount++; notifyDataSetChanged(); } }); } TextView tv=(TextView)rootView; tv.setText("text:"+(pressCount+position)); return rootView; } }); } public static int getResIdFromAttribute(final Activity activity,final int attr) { if(attr==0) return 0; final TypedValue typedvalueattr=new TypedValue(); activity.getTheme().resolveAttribute(attr,typedvalueattr,true); return typedvalueattr.resourceId; } }

The question

How can I temporarily stop the selection effect till the next time anything is clicked on the listView (but also resume allowing it for the next time the user clicks an item) ?

最满意答案

好的,我找到了答案。 这似乎是一个已知问题,解决方案非常简单(如下所示):

ViewCompat.jumpDrawablesToCurrentState(view);

奇怪的是,它只适用于我通过Handler.post(...)调用它。

不知道为什么(因为视图已经在动画期间),以及是否有更好的解决方案。

OK, I've found the answer. It seems it's a known issue, and the solution is quite simple (shown here) :

ViewCompat.jumpDrawablesToCurrentState(view);

Weird thing is, it works for me only when I call it via Handler.post(...) .

Wonder why (as the view is already during animation), and if there's a better solution.

更多推荐

本文发布于:2023-07-04 10:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023535.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:适配器   单击   效果   数据   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!