拉动刷新 RecyclerVIew 适配器不工作
Pull to Refresh RecyclerVIew Adapter not working
我在 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 我没有在适配器中添加更多数据,但我想在刷新回收站视图时如果互联网可用我的列表应该再次刷新如果互联网不可用我的回收站视图应该是由于刷新,为 null 或为空。
请指导我该怎么做?
您可以使用滑动布局
@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;
}
I want when I refresh recycler view if internet is available my list should be refresh again if internet is not available my recycler view should be null or empty due to refresh.
为此,我认为您应该在尝试刷新回收站视图时立即清除数据,只有在获得实际数据时才添加新项目。这样,如果您没有互联网,那么您的回收站视图将是空的,一旦您恢复连接,就会刷新它以获取新数据
我在 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 我没有在适配器中添加更多数据,但我想在刷新回收站视图时如果互联网可用我的列表应该再次刷新如果互联网不可用我的回收站视图应该是由于刷新,为 null 或为空。
请指导我该怎么做?
您可以使用滑动布局
@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;
}
I want when I refresh recycler view if internet is available my list should be refresh again if internet is not available my recycler view should be null or empty due to refresh.
为此,我认为您应该在尝试刷新回收站视图时立即清除数据,只有在获得实际数据时才添加新项目。这样,如果您没有互联网,那么您的回收站视图将是空的,一旦您恢复连接,就会刷新它以获取新数据