Android - 自定义 CursorAdapter 在 ViewFlipper 翻转时不断调用 BindView

标签 android android-cursoradapter

我已经构建了一个自定义的 CursorAdapter,它需要用推文填充 ListView...将显示简单的日期和推文... 它工作得很好,唯一的事情是,如果我添加一个日志条目,我注意到 BindView 方法每 8 秒为每个项目连续调用一次......这与屏幕上其他地方的 ViewFlipper 的 flipDuration 相对应......(由验证更改持续时间并看到以新的间隔时间调用 BindView)

ViewFlipper 确实使用与 ViewList 相同的布局 xml 和相同的数据(尽管我对数据库进行了 2 次单独调用以加载它们)

这是正常行为吗?

呈现 List 和 Flipper 的方法:

public void loadTwitterFeed(){

    ListView twitterList = (ListView) findViewById(R.id.twitterList);

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    dbHelper dbh = new dbHelper(this);

    dbh.openRead();

    twitterDbAdapter adapt = new twitterDbAdapter(this, dbh.getTweets());

    twitterList.setAdapter(adapt);

    ViewFlipper flipper = (ViewFlipper) findViewById(R.id.twitterFlipper);

    ArrayList<twitterTweet> tweets = dbh.getTweetsAsList();

    for( twitterTweet obj : tweets){

        View tItem = (View) inflater.inflate(R.layout.twitter_item, flipper, false);

        TextView tv1 = (TextView) tItem.findViewById(R.id.textView1);
        TextView tv2 = (TextView) tItem.findViewById(R.id.textView2);

        tv1.setText(obj.getDate());
        tv2.setText(obj.getText());

        flipper.addView(tItem);

    }

    dbh.close();

    flipper.setFlipInterval(5000);
    flipper.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left));
    flipper.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right));
    flipper.startFlipping();

}

我的自定义适配器:

public class twitterDbAdapter extends CursorAdapter{
private LayoutInflater inflater;

public twitterDbAdapter(Context context, Cursor cursor){

    super(context, cursor);
    inflater = LayoutInflater.from(context);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    Log.i("extra","binding");
    TextView textView = (TextView) view.findViewById(R.id.textView1);
    TextView textView2 = (TextView) view.findViewById(R.id.textView2);

    textView.setText(cursor.getString(cursor.getColumnIndex("createdat")));
    textView2.setText(cursor.getString(cursor.getColumnIndex("tweet")));

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View view = inflater.inflate(R.layout.twitter_item, parent, false);
    return view;
}

}

最佳答案

It works great, the only thing is that if I add a Log entry I notice that the BindView method is getting called continuously every 8 seconds for every item

我怀疑是否为 ListView 中的所有项目调用了 bindView,很可能是为可见项目调用了该方法(加上一些额外的调用)。

ViewFillper 翻阅它的子项时,Android 需要重新布局当前屏幕内容,这可能是您的 bindView 方法在 处被调用的原因>ViewFlipper 的切换间隔。这对你来说应该无关紧要。

关于Android - 自定义 CursorAdapter 在 ViewFlipper 翻转时不断调用 BindView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11502558/

相关文章:

android - getFragmentManager().beginTransaction() 问题

android - 如何创建唯一的文件名?

Android Studio 和 Gradle : versionNameSuffix

android - 从 CursorAdapter 中的 ListView 中删除项目

android - Activity 生命周期 - startActivity() 和 extras

android - 看似不一致的 OutOfMemory 在不同的 Android 设备上崩溃

android - 从 ListView 获取选中的 View

android - 如何单击 GridView 中单个项目的特定部分?

android - CursorAdapter 并不总是在 AutoCompleteTextView 上工作

android - 如何将 CursorAdapter 绑定(bind)到 ExpandableListView?