我遇到ListView的分页问题,该视图存在于ViewPager
内部的**Each Fragment**
中。最初,我必须在第一个片段中显示10条记录,即"全部",从中排序出《失败》并在第二个片段和第三个片段中分别显示。如何在此实现分页?我应该有一个"Load More"
按钮,每当用户点击它时,我都会得到接下来的10条记录。我试过搜索,但找不到任何符合我目的的东西。请帮帮我。谢谢。
我真的不明白"listview存在于每个片段中"。你的意思是显示的记录数量相同吗?
那么,如果第一个片段显示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());
}
毕竟,你应该有一个你想要的功能实现!
此外,请记住,我只是一时兴起就对所有这些进行了编码,没有ide,代码中可能潜伏着一些错误,所以在使用之前先阅读一下。我现在真的无法重读。
希望我能帮上忙!