【问题标题】:Pagination in ListView's present in three different Fragments which is in a ViewPagerListView 中的分页存在于 ViewPager 中的三个不同片段中
【发布时间】:2014-12-08 00:50:00
【问题描述】:

我遇到了 Pagination 的问题,该 ListView 存在于 **Each Fragment** 中,该 ViewPager 内。我最初必须在第一个 Fragment 中显示 10 条记录,即 “All”,由此整理出 “Failed” 并在第二个 Fragment 和 “Passed”中显示它 在第三个片段中。我该如何实现分页?我应该有一个 "Load More" 按钮,每当用户点击它时,我应该得到接下来的 10 条记录。我试图搜索,但找不到任何符合我目的的东西。请帮我。谢谢。

【问题讨论】:

    标签: android android-fragments android-listview pagination android-viewpager


    【解决方案1】:

    我不太了解“每个片段中都存在列表视图”。你的意思是显示的记录数是一样的吗?

    那么如果第一个片段显示 10 条记录,其中 5 条失败,另外 5 条通过,那么第二个和第三个片段将只显示 5 条记录?

    你不需要相同的 listView,我将指导你完成构建这个东西的过程。走吧!

    我将假设获取记录不是问题,并且出于此答案的目的,我将假设记录是具有两个属性的对象:字符串内容和布尔值传递,可以由 isPassed() 和 getContent() 方法检索。

    首先让我们处理调用片段的活动并添加几个重要的方法:

    {...}
      private int listSize = 10;  
    
      public void addMoreToList(){
        listSize +=10;
      }
    
      public void getListSize(){
        return listSize;
      }
    
      //replace this method and all its implementations with your own way of getting the records
     public List<Record> getRecords(){
        return recordList;
       }
    
     
     {...}

    好的,现在我们的主要活动可以存储和检索记录的数量,让我们来看看更大的枪,我们的适配器!

    private class RecordAdapter extends BaseAdapter{
      
      public static final int MODE_ALL = 0;
      public static final int MODE_PASSED = 1;
      public static final int MODE_FAILED = 2;
      
      private int mode;
      private Context context;
      private List<Records> records;
      
      public RecordAdapter(Context context, int mode){
        this.context = context;
        this.mode = mode;
        }
      
      public void setData(List<Records> totalList, int size){
        //this will add from the records list the size we want
        //if that size is smaller than the total size of the list
        //or else we'd get an error :(
        for(int i = 0; i <  (totalList.size() < size ? size: totalList.size()); i++){
          switch(mode){
           case(MODE_ALL): 
            records.add(totalList.get(i))
            break;
           case(MODE_PASSED):
            if(totalLost.get(i).isPassed()){
              records.add(totalLost.get(i)
            }
            break;
           case(MODE_FAILED):
            if(!totalLost.get(i).isPassed()){
              records.add(totalLost.get(i));
            }
            break;
           }
         }
        notifyDatasetChanged(); //i almost forgot this one, heh, its very important.
       }
        
      @Override 
      public Object getItem(int position){
        return records.get(position)
      }
      
      @Override
      public int getCount(){
        return records.size();
      }
          
      @Override
      public int getItemId(int position){
        return position;
      }
      
      @Override
      public View getView(int position, View convertView, ViewGroup parent){
        View view;
        if(convertView == null){
          view =  LayoutInflater.from(context).inflate(R.layout.yourtextview, parent, false); //I'm assuming here you 
          //only want a textview to display the text
        }else{
          view = convertView;
        }
        
        TextView textView = (TextView) view.findViewById(R.id.yourtextview);
        Record record = records.get(position)
        textView.setText(record.getContent());
        
        return view;
     }
    }

    我们快到了!现在我们的片段。首先是xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
          <ListView
            android:id = "@+id/lv_records"
            android:layout_width = "match_parent"
            android:layout_height = "0 dp"           
            />
         
    </LinearLayout>          

    private RecordsAdapter adapter;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
      View view = inflater.inflate(R.layout.xmlabove, container, false);
    
      ListView listView = (ListView) view.findViewById(R.id.lv_records);
      
    
      //This is just an example. You can inflate a view from an xml if you want fancy format
      //and etc. You could also define an onClickListener directly in the button and add it
      //to the listView, but i think this design is more appropriate.
      Button button = new Button(getActivity());
      button.setText("Load more...");
      listView.addFooterView(button);
      listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
              if(position < ((RecordsAdapter) parent).getCount()){
                //code for regular record click here
              }else{
               //means the position is not in the adapter, it's our button
               ((YourActivityNameHere)getActivity()).addMoreToList();        
                adapter.setData(((YourActivityNameHere)getActivity()).getRecords(),    
                      ((YourActivityNameHere)getActivity()).getListSize());
              }
            }
    
      });
      adapter = new RecordsAdapter(context, RecordsAdapter.MODE_NORMAL); //replace each mode on each fragment
      
      adapter.setData(((YourActivityNameHere)getActivity()).getRecords(), ((YourActivityNameHere)getActivity()).getListSize());
    
      listView.setAdapter(adapter);
      
      return view; 
    
    @Override
    public void onResume(){
      super.onResume();
      //in case there was a change of listsize in another fragment, we want this adapter to update when we return
      //to this fragment.
      adapter.setData(((YourActivityNameHere)getActivity()).getRecords(), ((YourActivityNameHere)getActivity()).getListSize());
      }

    毕竟你应该有一个你想要的功能实现!

    另外,请记住,我只是在脑海中编写了所有这些代码,不知道,一些错误可能潜伏在代码中,因此请在使用之前阅读它。我现在真的无法重新阅读它。

    希望我能有所帮助!

    【讨论】:

    • 我必须动态地将加载更多按钮添加到列表视图作为页脚。不在 XML 文件中
    • 天哪,挑剔的挑剔。好的,那就开始吧,给我几分钟时间。
    • 已编辑更改,现在应该可以使用了。别忘了调试代码,如果有问题回来找我。
    • 您在活动类中提供的代码...我在 Fragments 中这样做以获取数据。我还能遵循这个程序吗?
    • OnItemClickListener 在我的情况下不起作用,因为我的适配器视图有一些图像和按钮,在适配器类中有一个动作。
    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    相关资源
    最近更新 更多