简体   繁体   中英

My RecyclerView doesnt update on Notify Methods

please read before marking it as a duplicate! :)

I converted a listview to recyclerview in my app. But the problem is that it just doesnt update when I call any notify methods. I read that i should call these methods on the Ui Thread. but even that isn't working. The data list has the item added to it but it doesnt get shown on 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. I verified that but the notify notifyItemInserted doesnt seem to work. I tried running that on the UI thread but even that didnt seem to work.

Any suggestions? I followed the android recyclerview sample. Maybe i have used the RunOnUiThread incorrectly?

Also if you spot any syntax errors due to name changes, that isnt the problem, on Android Studio the code is error free.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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