java Android:删除行后如何刷新表格布局?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5680559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How to refresh a tablelayout after removing a row?
提问by Danny
I have a tablelayout that retrieves data from a *.txt file. For every line of data in the txt file, there will be one row of data.
我有一个从 *.txt 文件中检索数据的表格布局。对于txt文件中的每一行数据,都会有一行数据。
Let's say I have two rows of data in the txt file right now, it makes sense that two tablerows will be generated.
假设我现在在 txt 文件中有两行数据,生成两个表格行是有道理的。
Then, I added a OnLongPressListener which, when called, will delete one row of data from the txt file.
然后,我添加了一个 OnLongPressListener,它在调用时将从 txt 文件中删除一行数据。
Now's the first question: After deleting data in the txt file, how do I refresh my tablelayout to reflect that change?
现在是第一个问题:删除 txt 文件中的数据后,如何刷新我的 tablelayout 以反映该更改?
Second question is: After I get my first question solved, is it possible to have some kind of animation where one row will fade out or slide out instead of just disappearing outright?
第二个问题是:在我解决了我的第一个问题之后,是否有可能有某种动画,其中一行会淡出或滑出而不是完全消失?
Thanks!
谢谢!
回答by Jermin Bazazian
Not sure if you are still looking for answer. But I came across this post facing kinda the same problem. Plus the fact that I was forced to use TableLayout
(the amount of code written using TableLayout
is huge and it wasn't my call to switch to ListView
).
What I ended up doing was to remove all the views from TableLayout
using:
不确定您是否仍在寻找答案。但我遇到了这篇文章,面临着同样的问题。加上我被迫使用的事实TableLayout
(使用编写的代码量TableLayout
很大,我不希望切换到ListView
)。我最终做的是从TableLayout
使用中删除所有视图:
`tableLayout.removeAllViews();`
But that's not gonna work if the number of rows after removal changes drastically. I needed to invalidate my view too, using a handler. Here is the rest of my code.
但是如果删除后的行数发生了巨大的变化,这将不起作用。我也需要使用处理程序使我的视图无效。这是我的其余代码。
protected static final int REFRESH = 0;
private Handler _hRedraw;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tree_view_activity);
_hRedraw=new Handler(){
@Override
public void handleMessage(Message msg)
{
switch (msg.what) {
case REFRESH:
redrawEverything();
break;
}
}
};
...
}
private void redrawEverything()
{
tableLayout.invalidate();
tableLayout.refreshDrawableState();
}
There is only one part left and that's the part where you send a message to your handler to refresh your view. Here is the code for it:
只剩下一个部分,这是您向处理程序发送消息以刷新视图的部分。这是它的代码:
_hRedraw.sendEmptyMessage(REFRESH);
回答by Sagar Hatekar
Why not use a ListView
instead? It gives you better control when using an MVC model where there's a dataset tied to a View
. So you could have a class that extends BaseAdapter
. This class binds your data to the View
and this class also has a method: notifyDataSetChanged()
which is what should solve your problem.
为什么不使用 aListView
呢?在使用数据集绑定到View
. 所以你可以有一个扩展的类BaseAdapter
。此类将您的数据绑定到View
并且此类还有一个方法:notifyDataSetChanged()
这应该可以解决您的问题。
You may find notifyDataSetChanged exampleand customListViewhelpful.
您可能会发现notifyDataSetChanged 示例和customListView很有帮助。
Let me know if you need any help further.
如果您需要进一步的帮助,请告诉我。
回答by gvk51
Generally in onCreate()we do all the stuff that shows the ui with text, try to put the code that makes up UI in a private method say setUpTabView()and try to call this from onCreate and even try calling this setUpTabView()when ever the text changed. This kind of approach i did in grid view. worked very well...!
一般的onCreate() ,我们做的所有的东西,显示文本的用户界面,尽量把代码,构成了UI中的私有方法发言权setUpTabView()并尝试从的onCreate调用这个甚至尝试调用此setUpTabView()时文字改变了。我在网格视图中采用的这种方法。工作得很好......!