拉到刷新RecyclerVIew适配器无法正常工作

时间:2018-04-13 06:34:02

标签: java android android-recyclerview recycler-adapter swiperefreshlayout

我在google上搜索了很多次,如何在刷了swipeRefreslayout之后刷新回收器视图但是代码不起作用

这是我的代码

 swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        return r;
    }

    private void refresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, "refresh called", Toast.LENGTH_SHORT).show();
                fragment.RefreshList(String.valueOf(adpterposition));
                swipeRefreshLayout.setRefreshing(false);

            }
        }, 3000);
    }

这是片段

中的刷新列表功能
public void RefreshList(String catego) {
        parser = new ParseDataClass(getContext(), recyclerview, swipeRefreshLayout);
        parser.execute("http://192.168.100.12/fetchtext.php", catego);
    }

应用的另一种方法

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });

    }

    private void refresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
               // refreshEvents(OutletsData1);
                MyRecyclerAdapter.this.notifyDataSetChanged();
                swipeRefreshLayout.setRefreshing(false);

            }
        },3000);
    }

我是android的新手我没有在适配器中添加更多数据但我希望当我刷新回收站视图时如果互联网可用我的列表应该再次刷新如果互联网不可用我的回收站视图应该为空或由于刷新为空

请指导我该怎么办?

2 个答案:

答案 0 :(得分:0)

您可以使用滑动布局

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

      swipeRefreshLayout.setOnRefreshListener(this);
         swipeRefreshLayout.post(new Runnable() {
                    @Override
                    public void run() {
                       RefreshList();
                    }
                });

   }

 @Override
        public void onRefresh() {
            swipeRefreshLayout.setRefreshing(true);
              refresh();
        }



 private void refresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RefreshList();
               // refreshEvents(OutletsData1);

             swipeRefreshLayout.setRefreshing(false);
            }
        },3000);
}


 private void RefreshList() {
     //clear your list before any update
    //here fatch the data ....
    //on success or failture
MyRecyclerAdapter.this.notifyDataSetChanged();

    }

这是我的适配器代码

 public MyRecyclerAdapter(Context c, ArrayList<DataStored> 
      outletsData,SwipeRefreshLayout swipeRefreshLayout) {
        this.c = c;
        OutletsData = outletsData;
        OutletsData1 = outletsData;
        this.swipeRefreshLayout = swipeRefreshLayout;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.singleitem,null);
        MyViewHolder myViewHolder=new MyViewHolder(view);

        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.mBrandName.setText(OutletsData.get(position).getMbrand_name());
        holder.branchAddress.setText(OutletsData.get(position).getMbranch_address());
        holder.discountTxt.setText(OutletsData.get(position).getMdiscount_amount());
        holder.mBrandName.setText(OutletsData.get(position).getMbrand_name());

        holder.setItemClickListner(new ItemClickListner() {
            @Override
            public void onItemClick(View view, int position) {

                Toast.makeText(c,OutletsData.get(position).getMbrand_name(),Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(c,DetailOfListCard.class);
                intent.putExtra("brand_name",OutletsData.get(position).getMbrand_name());
                intent.putExtra("branch_logo",OutletsData.get(position).getMbranch_logo());
                intent.putExtra("brandlogo",OutletsData.get(position).getMbrand_logo());
                intent.putExtra("branch_address",OutletsData.get(position).getMbranch_address());
                intent.putExtra("position",MainActivity.adapterPosition);
                c.startActivity(intent);

            }
        });

    }

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

    @Override
    public Filter getFilter() {
        if(customFilter==null){
            customFilter=new CustomFilter(this,OutletsData1);
        }
        return customFilter;
    }

答案 1 :(得分:0)

  

我希望当我刷新回收站视图时,如果互联网可用,我的列表应该再次刷新,如果互联网不可用,我的回收站视图应为空或由于刷新而为空。

为此,我认为您应该在尝试刷新后清除回收站视图的数据,并仅在获得实际数据时添加新项目。这样,如果您没有互联网,那么您的回收站视图将为空,一旦您恢复连接,请刷新它以获取新数据