如何正确使用TextSwitcher ListView中?如何正确、TextSwitcher、ListView

2023-09-06 04:09:09 作者:精牌大丈夫

我的 TextSwitcher 的ListView每个记录应显示第一个值(文本1 的)然后另一个值(文本2 的),那么第一个值再等等。它应该发生仅当文本2 的不是空的。否则的文本1 的应始终显示(没有任何变化和动画)。

我创建的Runnable(),它改变布尔变量(时间2 的),以然后调用 items.notifyDataSetChanged ()。它的工作原理如预期中的结果 setViewValue()我的的ListView 之称。

下面是code:

  items.setViewBinder(新SimpleCursorAdapter.ViewBinder(){

    @覆盖
    公共布尔setViewValue(查看视图,光标指针,整数参数:columnIndex){
      INT的ViewID = view.getId();
      开关(的ViewID){
      案例R.id.timetext:
          TextSwitcher ITEMTIME =(TextSwitcher)视图;
          如果(itemTime.getChildCount()!= 2){
              itemTime.removeAllViews();
              itemTime.setFactory(新ViewSwitcher.ViewFactory(){
                  @覆盖
                  公共查看makeView(){
                      TextView的T =新的TextView(MyActivity.this);
                      t.se​​tTextSize(18);
                      t.se​​tTypeface(NULL,Typeface.BOLD);
                      t.se​​tTextColor(Color.WHITE);
                      返回吨;
                    }
                  });
              itemTime.setAnimateFirstView(真正的);
              itemTime.setInAnimation(AnimationUtils.loadAnimation(MyActivity.this,
                      R.anim.push_up_in));
              itemTime.setOutAnimation(AnimationUtils.loadAnimation(MyActivity.this,
                      R.anim.push_up_out));
          }

          如果(!text2.equals()){
              如果(!时间2){
                  itemTime.setText(文本);
              } 其他 {
                  itemTime.setText(文本2);
              }
          } 其他 {
                      itemTime.setCurrentText(文本);
              }
          返回true;
      }
      返回false;
    }
  });
 

它的工作原理几乎一样的预期。只有一个小项目 - (!从另一个记录)时的文本2 的应显示,它的变化显示的数值为其他值,然后再播放动画。变化的文本2 的到的文本1 的情况正常。

我的理解是,原因如下 - 显示之前的文本2 的,所有视图 ITEMTIME 被删除,因此它被重建,这是为什么一些其它的值显示一秒钟。但为什么它的其他纪录显示值?​​

事实上的文本2 和文本1 的是从数据库中值,为前。 文本2 = cursor.getString(cursor.getColumnIndexOrThrow(DbAdapter.KEY_TIME_2)),也许,什么是错在这里和 setViewValue 所谓有错误的参数?

UPD。 文本1 和文本2 的从数据库中读取 setViewValue 。下面是例子充分code:

itemTime.setText(cursor.getString(cursor.getColumnIndexOrThrow(DbAdapter.KEY_CLOSE_TIME_1)) + - + cursor.getString(cursor.getColumnIndexOrThrow(DbAdapter.KEY_OPEN_TIME_1))); c 中如何修改listview中的值,或者在text中修改,然后点击修改按钮及成功

解决方案

我想我明白是怎么回事,这是因为这样的ListView作品。

ListView中回收其所有的内部意见,这样你只需要创建为可以在屏幕上显示尽可能多的观点。然而,这也意味着,当你绑定在你的setViewValue方法值一看,你是不是经常因为是在列表前的同一位置的看法。

假设你有三个列表项:意达,itemB,itemC的顺序。分别为每个包含文本1,文本2,和文本3在第一。

当你调用 items.notifyDataSetChanged(),ListView中回收所有这些列表项但感觉,所以你可能会得到itemC,意达,itemB新秩序;而文本将改为文本3,文本1,文本2。

因此​​,当你改变的第一个列表项为文本2的文字,你实际上将看到text1中的文本3更改为文本2,而不是过渡到文本2像你期待的。

My TextSwitcher for each record in ListView should display first value (text1) and then another value (text2), then first value again and so on. It should happen only if text2 not empty. Otherwise text1 should be always shown (without any changes and animation).

I've created Runnable(), which changes boolean variable (time2) to then call items.notifyDataSetChanged(). It works as expected and in result setViewValue() for my ListView is called.

Here is the code:

items.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
      int viewId = view.getId();
      switch(viewId) {
      case R.id.timetext:
          TextSwitcher itemTime = (TextSwitcher) view;
          if (itemTime.getChildCount() != 2) {
              itemTime.removeAllViews();
              itemTime.setFactory(new ViewSwitcher.ViewFactory() {
                  @Override
                  public View makeView() {
                      TextView t = new TextView(MyActivity.this);
                      t.setTextSize(18);
                      t.setTypeface(null, Typeface.BOLD);
                      t.setTextColor(Color.WHITE);
                      return t;
                    }
                  });
              itemTime.setAnimateFirstView(true);
              itemTime.setInAnimation(AnimationUtils.loadAnimation(MyActivity.this,
                      R.anim.push_up_in));
              itemTime.setOutAnimation(AnimationUtils.loadAnimation(MyActivity.this,
                      R.anim.push_up_out));
          }

          if (!text2.equals("")) {
              if (!time2) {
                  itemTime.setText(text1);
              } else {
                  itemTime.setText(text2);
              }
          } else {
                      itemTime.setCurrentText(text1);
              }
          return true;
      } 
      return false;
    }
  } );

It works almost as expected. With one minor item - when text2 should be shown, it changes displayed value to some other value first (from another record!) and then animation is played. Change of text2 to text1 happens correctly.

My understanding that the reason is the following - before displaying text2, all views of itemTime are removed and hence it is recreated and that is why some other value is shown for a second. But why does it show value from some other record?

Actually text2 and text1 are values from the database, for ex. text2 = cursor.getString(cursor.getColumnIndexOrThrow(DbAdapter.KEY_TIME_2)), probably, something is wrong here and setViewValue called with wrong parameters?

Upd. text1 and text2 are read from the database at setViewValue. Here is example of the full code:

itemTime.setText(cursor.getString(cursor.getColumnIndexOrThrow(DbAdapter.KEY_CLOSE_TIME_1)) + " - " + cursor.getString(cursor.getColumnIndexOrThrow(DbAdapter.KEY_OPEN_TIME_1)));

解决方案

I think I see what's going on here, and it's because of the way ListView works.

ListView recycles all of its views internally so that you only have as many views created as can be displayed on the screen. However, this also means that when you bind values to a view in your setViewValue method, you are not always given the view that was in the same position in the list before.

Say you have three list items: itemA, itemB, itemC in that order. Each contains text1, text2, and text3 respectively at first.

When you call items.notifyDataSetChanged(), ListView recycles all those list items however it feels like, so you may get a new order of itemC, itemA, itemB; and the text would then read text3, text1, text2.

As a result, when you change the text of the first list item to "text2", you will in fact see "text3" change to "text2" instead of a transition from "text1" to "text2" like you are expecting.