fragment嵌套recyclerview,recyclerview的item重复加载怎么解决

 我来答
地瓜说机
2017-09-25 · TA获得超过2.9万个赞
知道大有可为答主
回答量:2.3万
采纳率:91%
帮助的人:1.2亿
展开全部
主页面
public class My_Main_Activity extends FragmentActivity implements View.OnClickListener {
LinearLayout shouye_layout,changjing_layout,anfang_layout,gengduo_layout;
ViewPager viewPager;
ImageButton yuyin_ivbt;
TextView shouye_tv,changjing_tv,anfang_tv,gengduo_tv;
//存放fragment集合
List<Fragment> list;
//4个Fragment
HomePageFragment shouye;
SceneFragment changjing;
SecurityFragment anfang;
MoreFragment gengduo;
int white,gray;
//适配器
MyAdapter myAdapter;
FragmentManager fragmentManager;
//资源类res
Resources resources;
// viewpage监听类
MyPageChangeListener myPageChangeListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_main_activity);
//通过getSupportFragmentManager()获取fragmentManager实例
fragmentManager= getSupportFragmentManager();
resources=getResources();
white=resources.getColor(R.color.colorbaizi);
gray=resources.getColor(R.color.colorhuizi);
initK();
initPager();
firstinto();
}
/**
* 初始化控件
* */
public void initK(){
myPageChangeListener=new MyPageChangeListener();
viewPager= (ViewPager) findViewById(R.id.view_pager);
viewPager.setOnPageChangeListener(myPageChangeListener);
shouye_layout= (LinearLayout) findViewById(R.id.shouye_layout);
changjing_layout= (LinearLayout) findViewById(R.id.changjing_layout);
anfang_layout= (LinearLayout) findViewById(R.id.anfang_layout);
gengduo_layout= (LinearLayout) findViewById(R.id.gengduo_layout);
yuyin_ivbt= (ImageButton) findViewById(R.id.yuyin_ivbt);
shouye_tv= (TextView) findViewById(R.id.shouye_tv);
changjing_tv= (TextView) findViewById(R.id.changjing_tv);
anfang_tv= (TextView) findViewById(R.id.anfang_tv);
gengduo_tv= (TextView) findViewById(R.id.gengduo_tv);
shouye_layout.setOnClickListener(this);
changjing_layout.setOnClickListener(this);
anfang_layout.setOnClickListener(this);
gengduo_layout.setOnClickListener(this);
View v =findViewById(R.id.yuyin_ivbt);//找到你要设透明背景的layout 的id
v.getBackground().setAlpha(0);//0~255透明度值
yuyin_ivbt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"语音按钮",Toast.LENGTH_LONG).show();
}
});
}
//初始化页面
private void initPager(){
list=new ArrayList<Fragment>();
shouye=new HomePageFragment();
changjing=new SceneFragment();
anfang=new SecurityFragment();
gengduo=new MoreFragment();
list.add(shouye);
list.add(changjing);
list.add(anfang);
list.add(gengduo);
myAdapter=new MyAdapter(fragmentManager,list);
viewPager.setAdapter(myAdapter);
}
//第一次进入页面的默认页面
private void firstinto(){
clearTuBiao();
shouye_tv.setTextColor(white);
}
@Override
public void onClick(View view) {
ChangeZiTi(view.getId());
}
//继承了ViewPage滑动监听事件
private class MyPageChangeListener implements ViewPager.OnPageChangeListener{
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
//通过onPageSelected回调方法传过来的参数可以得当前页面的位置编码
@Override
public void onPageSelected(int position) {
//调用下面的改变图标方法改变图标
ChangeZiTi(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
//改变图标的方法
private void ChangeZiTi(int i){
//清空选中项
clearTuBiao();
switch (i){
case R.id.shouye_layout : case 0:
//通过 viewPager.setCurrentItem();可以命令viewPager滑动到指定页面
//参数为int类型
viewPager.setCurrentItem(0);
shouye_tv.setTextColor(white);
break;
case R.id.changjing_layout:case 1:
viewPager.setCurrentItem(1);
changjing_tv.setTextColor(white);
break;
case R.id.anfang_layout:case 2:
viewPager.setCurrentItem(2);
anfang_tv.setTextColor(white);
break;
case R.id.gengduo_layout:case 3:
viewPager.setCurrentItem(3);
gengduo_tv.setTextColor(white);
break;
}
}
//清空字体的方法
private void clearTuBiao(){
shouye_tv.setTextColor(gray);
changjing_tv.setTextColor(gray);
anfang_tv.setTextColor(gray);
gengduo_tv.setTextColor(gray);
}
}
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);
break;
case 1:
Intent intent2 = new Intent(view.getContext(), My_Humidifier.class);
view.getContext().startActivity(intent2);
break;
}
}
});
}
@Override
public int getItemCount() {
return list.size();
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式