我的RecyclerView没有更新Notify Methods

时间:2015-04-12 23:33:18

标签: android multithreading android-recyclerview

请在将其标记为重复之前阅读! :)

我在我的应用中将listview转换为recyclerview。但问题是,当我调用任何通知方法时,它不会更新。我读到我应该在Ui Thread上调用这些方法。但即使这样也行不通。数据列表中添加了项目,但它没有显示在UI上。

这是代码。

适配器:

public class CustomViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

Activity activity;
ArrayList<Leg> data;

public CustomViewAdapter(Activity a, ArrayList<Leg> d) {
    super();
    activity = a;
    data = d;
}

@Override
public int getItemCount() {
    return data.size();
}

@Override
public int getItemViewType(int position) {
    Leg tempValues = data.get(position);
    return tempValues.getLegType();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    switch (viewType) {
        case 1:
            //...type 1
        case 2:
            //...type 2
        default:
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_leg, parent, false);
            return new LegViewHolder1(v);
    }
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    Leg tempValues = data.get(position);

    //.... Simple view logic that correctly displays
}

class LegViewHolder2 extends RecyclerView.ViewHolder {
    //... Holder of another type
}

class LegViewHolder1 extends RecyclerView.ViewHolder {

    @InjectView(R.id.leg_add_button)
    RelativeLayout legAddIcon;

    public AddLegViewHolder(View v) {
        super(v);
        ButterKnife.inject(this, v);

        legAddIcon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("Add Debug", "AddIcon clicked");

                Leg leg = new Leg();
                leg.setLegType(Constants.LEG);

                data.add((data.size() - 1), leg);

                refreshList("add", (data.size() - 1)); /// was using notify previously here
            }
        });

    }
}

public void refreshList(final String type, final int position) {
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (type.equals("update")) {
                notifyItemChanged(position);
            } else if (type.equals("remove")) {
                notifyItemRemoved(position);
            } else if (type.equals("add")) {
                notifyItemInserted(position);
            }
        }
    });
}

}

这是活动:

public class LegActivity extends BaseActivity {

CustomViewAdapter adapter;
LoadTask loadTask = null;
ArrayList<Leg> legsList;

@InjectView(R.id.leg_list)
RecyclerView legList;

@Override
public void onCreate(Bundle savedInstanceState) {
    //...boiler plate code

    legList.setHasFixedSize(true);
    legList.setLayoutManager(new LinearLayoutManager(this));

    loadTask = new LoadTask(this);
    loadTask.execute();
}

class LoadTask extends RoboAsyncTask<Boolean> {

    protected LoadTask(Context context) {
        super(context);
    }

    @Override
    protected void onPreExecute() {
        // show loader
    }

    @Override
    public Boolean call() throws Exception {
        //load some data the send boolean for success

    }

    @Override
    protected void onSuccess(Boolean success) {
    //checking for success and loading data to legslist.

    adapter = new CustomViewAdapter(LegActivity.this, legsList);
    legList.setAdapter(new AlphaInAnimationAdapter(adapter));
    legList.setItemAnimator(new SlideInLeftAnimator());

    }

    @Override
    protected void onException(Exception e) {
        // other code....
    }
}

}

问题是当我点击添加按钮时,对象被添加到实际的legList中。我验证了但是通知notifyItemInserted似乎没有用。我尝试在UI线程上运行它,但即便看起来也没有用。

有什么建议吗?我关注了android recyclerview示例。也许我错误地使用了RunOnUiThread?

此外,如果您发现由于名称更改而导致的任何语法错误,这不是问题,在Android Studio上代码没有错误。

1 个答案:

答案 0 :(得分:0)

由于这个自定义动画适配器,我会拍摄并说出来:

legList.setAdapter(new AlphaInAnimationAdapter(adapter));

以这种方式尝试:

adapter = new AlphaInAnimationAdapter(new CustomViewAdapter(LegActivity.this, legsList));
legList.setAdapter(adapter);