背景
从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.
更多推荐
发布评论