Recyclerview没有更新

时间:2019-01-17 06:11:47

标签: android android-recyclerview recycler-adapter

我正在尝试使用Class B更新recyclerview,但是recyclerview没有得到更新,我知道在堆栈溢出时会问很多类似的问题,我尝试了其中的大多数问题,但是似乎没有任何作用我的情况;

第一次将数据放入片段中时,我正在为适配器创建一个对象并将其设置为可以正常工作的recyclerview,但是下一次适配器已初始化时,我正试图通知给我一个适配器的适配器即使提供的列表不为空,也可以是空的recyclerview。

请提出建议。

RecyclerView代码

StartEventArgs

适配器代码:

Entered PageSwapSender
Entered Connect
Entered Handler

ViewHolder类

notifyDataSetChanged()

3 个答案:

答案 0 :(得分:1)

  

尝试一下:将您的数据更新方法名称更改为 updateDataSet 并尝试一下

RecyclerView代码:

        var width = 400;
        var height = 300; //this is the double because are showing just the half of the pie
        var radius = Math.min(width, height) / 2;

        var labelr = radius + 30; // radius for label anchor
        //array of colors for the pie (in the same order as the dataset)
        var color = d3.scale.ordinal()
        .range(['#2b5eac', '#0dadd3', '#ffea61', '#ff917e', '#ff3e41']);

        data = [{
        label: 'CDU',
        value: 10
        },
        {
        label: 'SPD',
        value: 15
        },
        {
        label: 'Die Grünen',
        value: 8
        },
        {
        label: 'Die Mitte',
        value: 1
        },
        {
        label: 'Frei Wähler',
        value: 3
        }
        ];

        var vis = d3.select("#chart")
        .append("svg") //create the SVG element inside the <body>
        .data([data]) //associate our data with the document
        .attr("width", width) //set the width and height of our visualization (these will be attributes of the <svg> tag
        .attr("height", height)
        .append("svg:g") //make a group to hold our pie chart
        .attr('transform', 'translate(' + (width / 2) + ',' + (height / 2) + ')'); //move the center of the pie chart from 0, 0 to radius, radius

        var arc = d3.svg.arc() //this will create <path> elements for us using arc data
        .innerRadius(79)
        //                                  .outerRadius(radius);
        .outerRadius(radius - 10) // full height semi pie
        //.innerRadius(0);


        var pie = d3.layout.pie() //this will create arc data for us given a list of values
        .startAngle(-90 * (Math.PI / 180))
        .endAngle(90 * (Math.PI / 180))
        .padAngle(.02) // some space between slices
        .sort(null) //No! we don't want to order it by size
        .value(function(d) {
        return d.value;
        }); //we must tell it out to access the value of each element in our data array

        var arcs = vis.selectAll("g.slice") //this selects all <g> elements with class slice (there aren't any yet)
        .data(pie) //associate the generated pie data (an array of arcs, each having startAngle, endAngle and value properties) 
        .enter() //this will create <g> elements for every "extra" data element that should be associated with a selection. The result is creating a <g> for every object in the data array
        .append("svg:g") //create a group to hold each slice (we will have a <path> and a <text> element associated with each slice)
        .attr("class", "slice"); //allow us to style things in the slices (like text)

        arcs.append("svg:path")
        .attr("fill", function(d, i) {
        return color(i);
        }) //set the color for each slice to be chosen from the color function defined above
        .attr("d", arc); //this creates the actual SVG path using the associated data (pie) with the arc drawing function

        const textEl = arcs.append("svg:text")
        .attr("class", "labels") //add a label to each slice
        .attr("fill", "grey")
        .attr("transform", function(d) {
        var c = arc.centroid(d),
        xp = c[0],
        yp = c[1],
        // pythagorean theorem for hypotenuse
        hp = Math.sqrt(xp * xp + yp * yp);
        return "translate(" + (xp / hp * labelr) + ',' +
        (yp / hp * labelr) + ")";
        })
        .attr("text-anchor", "middle"); //center the text on it's origin

        textEl.append('tspan')
        .text(function(d, i) {
        return data[i].label;
        });

        textEl.append('tspan')
        .text(function(d, i) {
        return data[i].value;
        })
        .attr('x', '0')
        .attr('dy', '1.2em');

        arcs.append("svg:text")      
        .attr("class", "labels")//add a label to each slice
        .attr("fill", "grey")
        .attr("transform", function(d) {
        var c = arc.centroid(d),
        xp = c[0],
        yp = c[1],
        // pythagorean theorem for hypotenuse
        hp = Math.sqrt(xp*xp + yp*yp);
        return "translate(" + (xp/hp * labelr) +  ',' +
        (yp/hp * labelr) +  ")"; 
        })
        .attr("text-anchor", "middle")    //center the text on it's origin
        .text(function(d, i) { return d.data.value; })
        .text(function(d, i) { return d.data.label; });  

        //tooltip
        arcs.on("mouseover", function(d) {
        d3.select("#tooltip")
        .style("left", `${d3.event.clientX}px`)
        .style("top", `${d3.event.clientY}px`)
        .classed("hidden", false);
        d3.select("#tooltip-data")
        .html(`Label: ${d.data.label}<br>Value: ${d.data.value}`);
        });

        arcs.on("mouseout", function(d) {
        d3.select("#tooltip")
        .classed("hidden", true);
        });

适配器代码:

private void showStateList(List<State> states) {
        if (states != null) {
            this.lstStates = states;
            if (stateAdapter == null) {
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                stateFragmentBinding.stateRecyclerView.setLayoutManager(linearLayoutManager);

                stateFragmentBinding.stateRecyclerView.addItemDecoration(new DividerItemDecoration(stateFragmentBinding.stateRecyclerView.getContext(),
                        linearLayoutManager.getOrientation()));
                stateFragmentBinding.stateRecyclerView.setHasFixedSize(true);

                stateAdapter = new StateAdapter(lstStates, this);
                stateFragmentBinding.stateRecyclerView.setAdapter(stateAdapter);
            } else {
                stateAdapter.updateDataSet(lstStates);
            }
        }
    }

查看持有人代码:

class StateAdapter extends RecyclerView.Adapter<StateViewHolder> {
    private LayoutInflater inflater;
    private StateClickListener stateClickListener;
    private List<State> lstStateItem = new ArrayList<>();;

    public StateAdapter(List<State> lstStateItem, StateClickListener stateClickListener) {
        this.lstStateItem.addAll(lstStateItem);
        this.stateClickListener = stateClickListener;
    }

    @NonNull
    @Override
    public StateViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        if (inflater == null)
            inflater = LayoutInflater.from(viewGroup.getContext());
        StateAdapterBinding stateAdapterBinding = StateAdapterBinding.inflate(inflater, viewGroup, false);
        return new StateViewHolder(stateAdapterBinding, stateClickListener);
    }

    @Override
    public void onBindViewHolder(@NonNull StateViewHolder stateViewHolder, int position) {
        State state = lstStateItem.get(position);
        stateViewHolder.bind(state, position);
    }

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

    public void updateDataSet(List<State> states) {
        this.lstStateItem.clear();
        this.lstStateItem.addAll(states);
        notifyDataSetChanged();
    }
}

答案 1 :(得分:0)

尝试使用:

 your_binding_instance.executePendingBindings()

您的viewholder类应如下所示:

    class ViewHolder(binding) extends RecyclerView.ViewHolder(binding.root){
    bind(item){
    bindnig.item = item
    binding.executePendingBindings()
    }
}

答案 2 :(得分:0)

如果有人有更好的解决方法,请通过以下代码更改showStateList方法:

   private void showStateList(List<State> states) {
    if (states != null) {
        this.lstStates = states;
        if(stateAdapter == null){
            stateAdapter = new StateAdapter(lstStates, this);
        }
        else {
            stateAdapter.notifyStateChanged(lstStates);
        }
        stateFragmentBinding.stateRecyclerView.setAdapter(stateAdapter);
    }

}
相关问题