简体   繁体   English

我的RecyclerView没有在通知方法上更新

[英]My RecyclerView doesnt update on Notify Methods

please read before marking it as a duplicate! 在将其标记为重复项之前,请先阅读! :) :)

I converted a listview to recyclerview in my app. 我在应用程序中将listview转换为recyclerview。 But the problem is that it just doesnt update when I call any notify methods. 但是问题是,当我调用任何notify方法时,它只是不会更新。 I read that i should call these methods on the Ui Thread. 我读到我应该在Ui Thread上调用这些方法。 but even that isn't working. 但即使那样也不起作用。 The data list has the item added to it but it doesnt get shown on UI. 数据列表中添加了该项目,但未在UI上显示。

Here's the code. 这是代码。

Adapter: 适配器:

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);
            }
        }
    });
}

} }

Here's the Activity: 这是活动:

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....
    }
}

} }

The problem is that the object is added to the actual legList when i click on the add button. 问题是当我单击添加按钮时,该对象已添加到实际的legList中。 I verified that but the notify notifyItemInserted doesnt seem to work. 我验证了,但是notify notifyItemInserted似乎不起作用。 I tried running that on the UI thread but even that didnt seem to work. 我试着在UI线程上运行它,但即使这样似乎也没有用。

Any suggestions? 有什么建议么? I followed the android recyclerview sample. 我遵循了android recyclerview示例。 Maybe i have used the RunOnUiThread incorrectly? 也许我没有正确使用RunOnUiThread?

Also if you spot any syntax errors due to name changes, that isnt the problem, on Android Studio the code is error free. 另外,如果您发现由于名称更改而引起的任何语法错误(不是问题),则在Android Studio上,代码是无错误的。

I will take a shot and say it's because of this custom animating adapter: 我会拍照,说这是因为有了这个自定义动画适配器:

legList.setAdapter(new AlphaInAnimationAdapter(adapter));

Try it this way: 尝试这种方式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM