• <tfoot id='HG2w0'></tfoot>
        <bdo id='HG2w0'></bdo><ul id='HG2w0'></ul>
    1. <legend id='HG2w0'><style id='HG2w0'><dir id='HG2w0'><q id='HG2w0'></q></dir></style></legend>

      <small id='HG2w0'></small><noframes id='HG2w0'>

    2. <i id='HG2w0'><tr id='HG2w0'><dt id='HG2w0'><q id='HG2w0'><span id='HG2w0'><b id='HG2w0'><form id='HG2w0'><ins id='HG2w0'></ins><ul id='HG2w0'></ul><sub id='HG2w0'></sub></form><legend id='HG2w0'></legend><bdo id='HG2w0'><pre id='HG2w0'><center id='HG2w0'></center></pre></bdo></b><th id='HG2w0'></th></span></q></dt></tr></i><div id='HG2w0'><tfoot id='HG2w0'></tfoot><dl id='HG2w0'><fieldset id='HG2w0'></fieldset></dl></div>

        如何从 viewpager 和 pageradapter 中删除项目

        时间:2023-10-04

        <i id='dFVKi'><tr id='dFVKi'><dt id='dFVKi'><q id='dFVKi'><span id='dFVKi'><b id='dFVKi'><form id='dFVKi'><ins id='dFVKi'></ins><ul id='dFVKi'></ul><sub id='dFVKi'></sub></form><legend id='dFVKi'></legend><bdo id='dFVKi'><pre id='dFVKi'><center id='dFVKi'></center></pre></bdo></b><th id='dFVKi'></th></span></q></dt></tr></i><div id='dFVKi'><tfoot id='dFVKi'></tfoot><dl id='dFVKi'><fieldset id='dFVKi'></fieldset></dl></div>

        <small id='dFVKi'></small><noframes id='dFVKi'>

        <legend id='dFVKi'><style id='dFVKi'><dir id='dFVKi'><q id='dFVKi'></q></dir></style></legend>
          <tbody id='dFVKi'></tbody>
        • <tfoot id='dFVKi'></tfoot>

              • <bdo id='dFVKi'></bdo><ul id='dFVKi'></ul>
                1. 本文介绍了如何从 viewpager 和 pageradapter 中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 pageradapter 和 viewpager 来显示 imageview 数组、textview 数组.但是如何在按钮单击时删除寻呼机.下面代码中的整个代码,但是我在 xml 中添加了按钮 onclick 应该删除页面.

                  I am using pageradapter and viewpager to display imageview array,textview array .But how do I delete pager on button click .Entire code in the code below ,but I have added button in xml onclick should delete the page.

                  https://www.swipetips.com/android-viewpager-gallery-images-and-texts-tutorial/

                   package com.androidbegin.viewpagertutorial;
                  
                      import android.content.Context;
                      import android.support.v4.view.PagerAdapter;
                      import android.support.v4.view.ViewPager;
                      import android.view.LayoutInflater;
                      import android.view.View;
                      import android.view.View.OnClickListener;
                      import android.view.ViewGroup;
                      import android.widget.Button;
                      import android.widget.ImageView;
                      import android.widget.RelativeLayout;
                      import android.widget.TextView;
                  
                      public class ViewPagerAdapter extends PagerAdapter {
                          // Declare Variables
                          Context context;
                          String[] rank;
                          String[] country;
                          String[] population;
                          int[] flag;
                          LayoutInflater inflater;
                  
                          public ViewPagerAdapter(Context context, String[] rank, String[] country,
                                  String[] population, int[] flag) {
                              this.context = context;
                              this.rank = rank;
                              this.country = country;
                              this.population = population;
                              this.flag = flag;
                          }
                  
                          @Override
                          public int getCount() {
                              return rank.length;
                          }
                  
                          @Override
                          public boolean isViewFromObject(View view, Object object) {
                              return view == ((RelativeLayout) object);
                          }
                  
                          @Override
                          public Object instantiateItem(final ViewGroup container, final int position) {
                  
                              // Declare Variables
                              TextView txtrank;
                              TextView txtcountry;
                              TextView txtpopulation;
                              ImageView imgflag;
                  
                              Button b;
                  
                              inflater = (LayoutInflater) context
                                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                              View itemView = inflater.inflate(R.layout.viewpager_item, container,
                                      false);
                  
                              // Locate the TextViews in viewpager_item.xml
                              txtrank = (TextView) itemView.findViewById(R.id.rank);
                              txtcountry = (TextView) itemView.findViewById(R.id.country);
                              txtpopulation = (TextView) itemView.findViewById(R.id.population);
                              b=(Button)itemView.findViewById(R.id.button1);
                  
                  
                              b.setOnClickListener(new OnClickListener() {
                  
                                  @Override
                                  public void onClick(View v) {
                  
                  
                  
                                  }
                              });
                  
                              // Capture position and set to the TextViews
                              txtrank.setText(rank[position]);
                              txtcountry.setText(country[position]);
                              txtpopulation.setText(population[position]);
                  
                              // Locate the ImageView in viewpager_item.xml
                              imgflag = (ImageView) itemView.findViewById(R.id.flag);
                              // Capture position and set to the ImageView
                              imgflag.setImageResource(flag[position]);
                  
                              // Add viewpager_item.xml to ViewPager
                              ((ViewPager) container).addView(itemView);
                  
                              return itemView;
                          }
                  
                          @Override
                          public void destroyItem(ViewGroup container, int position, Object object) {
                              // Remove viewpager_item.xml from ViewPager
                              ((ViewPager) container).removeView((RelativeLayout) object);
                  
                          }
                      }
                  

                  //////////////////

                  ////////////////////

                  // Locate the ViewPager in viewpager_main.xml
                          viewPager = (ViewPager) findViewById(R.id.pager);
                          // Pass results to ViewPagerAdapter Class
                          adapter = new ViewPagerAdapter(MainActivity.this, rank, country, population, flag);
                          // Binds the Adapter to the ViewPager
                          viewPager.setAdapter(adapter);
                  

                  推荐答案

                  首先需要使用List,因为动态删除对象.第二个你需要

                  first you need to use List because of dynamically deleting objects. second you need

                      @Override
                      public int getItemPosition(Object object){
                          return PagerAdapter.POSITION_NONE;
                      }
                  

                  强制适配器每次创建新页面,而不是在内存页面中使用.

                  to force the adapter to create new page each time and not using in memory pages.

                   public class ViewPagerAdapter extends PagerAdapter {
                      // Declare Variables
                      Context context;
                      List<String> rank;
                      List<String> country;
                      List<String> population;
                      List<Integer> flag = new ArrayList<Integer>();
                      LayoutInflater inflater;
                  
                      public ViewPagerAdapter(Context context, String[] rank, String[] country,
                              String[] population, int[] flag) {
                          this.context = context;
                          this.rank = new ArrayList<String>(Arrays.asList(rank));
                          this.country = new ArrayList<String>(Arrays.asList(country));
                          this.population = new ArrayList<String>(Arrays.asList(population));
                          for (int i : flag){ 
                              this.flag.add(i);
                          }
                  
                      }
                  
                      @Override
                      public int getCount() {
                          return rank.size();
                      }
                  
                      @Override
                      public boolean isViewFromObject(View view, Object object) {
                          return view == ((RelativeLayout) object);
                      }
                  
                      @Override
                      public Object instantiateItem(ViewGroup container, int position) {
                  
                          // Declare Variables
                          TextView txtrank;
                          TextView txtcountry;
                          TextView txtpopulation;
                          ImageView imgflag;
                  
                          inflater = (LayoutInflater) context
                                  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                          View itemView = inflater.inflate(R.layout.viewpager_item, container,
                                  false);
                  
                          // Locate the TextViews in viewpager_item.xml
                          txtrank = (TextView) itemView.findViewById(R.id.rank);
                          txtcountry = (TextView) itemView.findViewById(R.id.country);
                          txtpopulation = (TextView) itemView.findViewById(R.id.population);
                           Button b=(Button)itemView.findViewById(R.id.button1);
                  
                           final int delPosition = position;
                  
                           b.setOnClickListener(new OnClickListener() {
                  
                               @Override
                               public void onClick(View v) {
                                   rank.remove(delPosition);
                                   country.remove(delPosition);
                                   population.remove(delPosition);
                                   flag.remove(delPosition);
                                   notifyDataSetChanged();
                               }
                           });
                          // Capture position and set to the TextViews
                          txtrank.setText(rank.get(position));
                          txtcountry.setText(country.get(position));
                          txtpopulation.setText(population.get(position));
                  
                          // Locate the ImageView in viewpager_item.xml
                          imgflag = (ImageView) itemView.findViewById(R.id.flag);
                          // Capture position and set to the ImageView
                          imgflag.setImageResource(flag.get(position));
                  
                          // Add viewpager_item.xml to ViewPager
                          ((ViewPager) container).addView(itemView);
                  
                          return itemView;
                      }
                  
                      @Override
                      public void destroyItem(ViewGroup container, int position, Object object) {
                          // Remove viewpager_item.xml from ViewPager
                          ((ViewPager) container).removeView((RelativeLayout) object);
                  
                      }
                      @Override
                      public int getItemPosition(Object object){
                          return PagerAdapter.POSITION_NONE;
                      }
                  }
                  

                  这篇关于如何从 viewpager 和 pageradapter 中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Android oncreateview 调用了两次 下一篇:带有 ViewPager 的 CollapsingToolbarLayout

                  相关文章

                  <tfoot id='U31UN'></tfoot>
                    <legend id='U31UN'><style id='U31UN'><dir id='U31UN'><q id='U31UN'></q></dir></style></legend>
                      <bdo id='U31UN'></bdo><ul id='U31UN'></ul>

                      <i id='U31UN'><tr id='U31UN'><dt id='U31UN'><q id='U31UN'><span id='U31UN'><b id='U31UN'><form id='U31UN'><ins id='U31UN'></ins><ul id='U31UN'></ul><sub id='U31UN'></sub></form><legend id='U31UN'></legend><bdo id='U31UN'><pre id='U31UN'><center id='U31UN'></center></pre></bdo></b><th id='U31UN'></th></span></q></dt></tr></i><div id='U31UN'><tfoot id='U31UN'></tfoot><dl id='U31UN'><fieldset id='U31UN'></fieldset></dl></div>

                      <small id='U31UN'></small><noframes id='U31UN'>