fragment嵌套recyclerview,recyclerview的item重复加载怎么解决
2个回答
2017-07-07
展开全部
Fragment
public class HomePageFragment extends Fragment implements View.OnClickListener,PopupMenu.OnMenuItemClickListener {
private List<Home> list = new ArrayList<>();
ImageView shouye_ADD;
Intent intent;
private RecyclerView recyclerView;
private PullToLoadRecyclerView rcv_shouye;
private Handler handler;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view_shouye = inflater.inflate(R.layout.home_page, container, false);
Home_adapter adapter = new Home_adapter(list);
handler = new Handler();
rcv_shouye = (PullToLoadRecyclerView)view_shouye.findViewById(R.id.rcv_shouye);
rcv_shouye.setLayoutManager(new GridLayoutManager(getContext(),3));
init();
rcv_shouye.setAdapter(adapter);
//设置刷新监听
rcv_shouye.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onStartRefreshing() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
rcv_shouye.completeRefresh();
}
}, 1000);
}
});
// 设置加载监听
rcv_shouye.setOnLoadListener(new OnLoadListener() {
@Override
public void onStartLoading(int skip) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
rcv_shouye.completeLoad();
}
}, 1000);
}
});
shouye_ADD = (ImageView) view_shouye.findViewById(R.id.shouye_ADD);
shouye_ADD.setOnClickListener(this);
return view_shouye;
}
private void init() {
Area.queryAreasByUsername((result, error)->{
if(error!=null){
LogUtils.i(error);
}else {
List<Map<String,Object>> list1 =(List<Map<String,Object>>)result;
for (int i=0;i<list1.size();i++){
Home home=new Home(String.valueOf(list1.get(i).get("name")),String.valueOf(list1.get(i).get("num")));
list.add(home);
}
}
});
}
@Override
public void onClick(View vieww) {
PopupMenu popup = new PopupMenu(getContext(), vieww);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.add_wind, popup.getMenu());
popup.setOnMenuItemClickListener(this);
popup.show();
}
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.wind_add_shebei:
intent = new Intent(getContext(), Add_shebei.class);
startActivity(intent);
break;
case R.id.wind_add_quyu:
intent = new Intent(getContext(), Add_quyu.class);
startActivity(intent);
break;
default:
break;
}
return false;
}
}
adapter.java
public class Home_adapter extends RecyclerView.Adapter<Home_adapter.ViewHolder> {
private List<Home> list;
public Home home;
private PullToLoadRecyclerView rcv_shouye;
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView img_shouye_item;
TextView tv_shouye_item_neirong, tv_shouye_item_shuzi;
LinearLayout Rlayout;
public ViewHolder(View View) {
super(View);
//实例化控件
img_shouye_item = (ImageView) View.findViewById(R.id.img_shouye_item);
tv_shouye_item_neirong = (TextView) View.findViewById(R.id.tv_shouye_item_neirong);
tv_shouye_item_shuzi = (TextView) View.findViewById(R.id.tv_shouye_item_shuzi);
Rlayout = (LinearLayout) View.findViewById(R.id.Rlayout12);
rcv_shouye = (PullToLoadRecyclerView) View.findViewById(R.id.rcv_shouye);
}
}
//布局管理器
public Home_adapter(List<Home> homelist) {
list = homelist;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shouye_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
home = list.get(position);
holder.tv_shouye_item_shuzi.setText(home.getNum());
holder.tv_shouye_item_neirong.setText(home.getName());
holder.Rlayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (position) {
case 0:
Intent intent = new Intent(view.getContext(), Add_ShouYe_WoShi.class);
view.getContext().startActivity(intent);
Java code
?
1
[code=java]
[/code]
break;
case 1:
Intent intent2 = new Intent(view.getContext(), My_Humidifier.class);
view.getContext().startActivity(intent2);
break;
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
}
public class HomePageFragment extends Fragment implements View.OnClickListener,PopupMenu.OnMenuItemClickListener {
private List<Home> list = new ArrayList<>();
ImageView shouye_ADD;
Intent intent;
private RecyclerView recyclerView;
private PullToLoadRecyclerView rcv_shouye;
private Handler handler;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view_shouye = inflater.inflate(R.layout.home_page, container, false);
Home_adapter adapter = new Home_adapter(list);
handler = new Handler();
rcv_shouye = (PullToLoadRecyclerView)view_shouye.findViewById(R.id.rcv_shouye);
rcv_shouye.setLayoutManager(new GridLayoutManager(getContext(),3));
init();
rcv_shouye.setAdapter(adapter);
//设置刷新监听
rcv_shouye.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onStartRefreshing() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
rcv_shouye.completeRefresh();
}
}, 1000);
}
});
// 设置加载监听
rcv_shouye.setOnLoadListener(new OnLoadListener() {
@Override
public void onStartLoading(int skip) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
rcv_shouye.completeLoad();
}
}, 1000);
}
});
shouye_ADD = (ImageView) view_shouye.findViewById(R.id.shouye_ADD);
shouye_ADD.setOnClickListener(this);
return view_shouye;
}
private void init() {
Area.queryAreasByUsername((result, error)->{
if(error!=null){
LogUtils.i(error);
}else {
List<Map<String,Object>> list1 =(List<Map<String,Object>>)result;
for (int i=0;i<list1.size();i++){
Home home=new Home(String.valueOf(list1.get(i).get("name")),String.valueOf(list1.get(i).get("num")));
list.add(home);
}
}
});
}
@Override
public void onClick(View vieww) {
PopupMenu popup = new PopupMenu(getContext(), vieww);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.add_wind, popup.getMenu());
popup.setOnMenuItemClickListener(this);
popup.show();
}
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.wind_add_shebei:
intent = new Intent(getContext(), Add_shebei.class);
startActivity(intent);
break;
case R.id.wind_add_quyu:
intent = new Intent(getContext(), Add_quyu.class);
startActivity(intent);
break;
default:
break;
}
return false;
}
}
adapter.java
public class Home_adapter extends RecyclerView.Adapter<Home_adapter.ViewHolder> {
private List<Home> list;
public Home home;
private PullToLoadRecyclerView rcv_shouye;
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView img_shouye_item;
TextView tv_shouye_item_neirong, tv_shouye_item_shuzi;
LinearLayout Rlayout;
public ViewHolder(View View) {
super(View);
//实例化控件
img_shouye_item = (ImageView) View.findViewById(R.id.img_shouye_item);
tv_shouye_item_neirong = (TextView) View.findViewById(R.id.tv_shouye_item_neirong);
tv_shouye_item_shuzi = (TextView) View.findViewById(R.id.tv_shouye_item_shuzi);
Rlayout = (LinearLayout) View.findViewById(R.id.Rlayout12);
rcv_shouye = (PullToLoadRecyclerView) View.findViewById(R.id.rcv_shouye);
}
}
//布局管理器
public Home_adapter(List<Home> homelist) {
list = homelist;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shouye_item, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
home = list.get(position);
holder.tv_shouye_item_shuzi.setText(home.getNum());
holder.tv_shouye_item_neirong.setText(home.getName());
holder.Rlayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (position) {
case 0:
Intent intent = new Intent(view.getContext(), Add_ShouYe_WoShi.class);
view.getContext().startActivity(intent);
Java code
?
1
[code=java]
[/code]
break;
case 1:
Intent intent2 = new Intent(view.getContext(), My_Humidifier.class);
view.getContext().startActivity(intent2);
break;
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
}
展开全部
fragmentManager默认缓存三个页面,即当前和相邻的页面,切到3或4就会导致重新走oncreateview(),数据没清空就重载咯。在@Override
public boolean getUserVisibleHint() {//适用FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();transaction.hide(from).show(to).commitAllowingStateLoss();
return super.getUserVisibleHint();
}
@Override
public void onHiddenChanged(boolean hidden) {//适用于viewpager
super.onHiddenChanged(hidden);
}
进行数据管理
public boolean getUserVisibleHint() {//适用FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();transaction.hide(from).show(to).commitAllowingStateLoss();
return super.getUserVisibleHint();
}
@Override
public void onHiddenChanged(boolean hidden) {//适用于viewpager
super.onHiddenChanged(hidden);
}
进行数据管理
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询