我正在尝试对SwipeListView
进行Pull-to-Refresh来实现ListView
。它成功集成,但是在列表之后添加了一个项目。 ListView 不刷新。并且没有项目添加到列表中。
我已经实现了here中的SwipeListView
和,并从here中实现了Refresh 的实现。
我如何在 ListView 中添加多个项目并在添加项目后刷新列表。
JAVA代码
package com.eample.swipelistviewexample;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.AsyncTask;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.fortysevendeg.swipelistview.BaseSwipeListViewListener;
import com.fortysevendeg.swipelistview.PullToRefreshBase;
import com.fortysevendeg.swipelistview.PullToRefreshBase.OnRefreshListener;
import com.fortysevendeg.swipelistview.PullToRefreshBase.OnRefreshListener2;
import com.fortysevendeg.swipelistview.PullToRefreshSwipeListView;
import com.fortysevendeg.swipelistview.SwipeListView;
public class Contact extends Activity {
public PullToRefreshSwipeListView ptorefreshList;
public SwipeListView swipeListView;
ProgressDialog dialog;
private ArrayList<String> mItems;
CategoryAdapter adapter;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mItems = new ArrayList<String>();
for(int i=0;i<10;i++)
mItems.add("Musethe place "+i);
ptorefreshList=(PullToRefreshSwipeListView) findViewById(R.id.example_list);
swipeListView = ptorefreshList.getRefreshableView();
adapter = new CategoryAdapter(Contact.this,
mItems);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
setListview();
ptorefreshList.setOnRefreshListener(new OnRefreshListener<SwipeListView>() {
@Override
public void onRefresh(PullToRefreshBase<SwipeListView> refreshView) {
// TODO Auto-generated method stub
new NewDataTask().execute();
}
});
}
private void setListview() {
swipeListView.setSwipeListViewListener(new BaseSwipeListViewListener() {
//position di -1 karena sejak gabung library swipelistview + pulltorefresh, position slalu kelebihan 1 & menyebabkan OutOfBound error.
@Override
public void onClickFrontView(final int position) {
//do something here
}
@Override
public void onOpened(int position, boolean toRight) {
// TODO Auto-generated method stub
super.onOpened(position-1, toRight);
}
@Override
public void onMove(int position, float x) {
// TODO Auto-generated method stub
super.onMove(position-1, x);
}
@Override
public int onChangeSwipeMode(int position) {
// TODO Auto-generated method stub
return SwipeListView.SWIPE_MODE_DEFAULT;
}
@Override
public void onStartOpen(int position, int action, boolean right) {
// TODO Auto-generated method stub
super.onStartOpen(position-1, action, right);
}
});
ptorefreshList.setAdapter(adapter);
ptorefreshList.setLongClickable(true);
swipeListView.setSwipeOpenOnLongPress(false);
}
public class CategoryAdapter extends BaseAdapter {
LayoutInflater inflator;
ArrayList<String> category;
public CategoryAdapter(Context context,ArrayList<String> category)
{
inflator=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.category=category;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return category.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final int p=position;
View vi=convertView;
if(convertView==null)
vi=inflator.inflate(R.layout.custom_row,null);
TextView tv=(TextView) vi.findViewById(R.id.example_itemname);
tv.setText(category.get(position));
return vi;
}
}
private class NewDataTask extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
if (android.os.Build.VERSION.SDK_INT >= 11) {
dialog = new ProgressDialog(Contact.this,AlertDialog.THEME_HOLO_LIGHT);
} else {
dialog = new ProgressDialog(Contact.this);
}
dialog.setMessage("Loading...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
@Override
protected String doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "WWW.MUSETHEPLACE.COM";
}
@Override
protected void onPostExecute(String result) {
// This should be called after refreshing finished
mItems.add(0, result);
adapter = new CategoryAdapter(Contact.this,
mItems);
ptorefreshList.setAdapter(adapter);
adapter.notifyDataSetChanged();
dialog.dismiss();
super.onPostExecute(result);
}
}
public int convertDpToPixel(float dp) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return (int) px;
}
}
最佳答案
这不是直接的解决方案,但是我必须做类似的事情,最终我要做的是使用android原生的“PullToRefresh”,其名称为 SwipeRefreshLayout ,位于支持库https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html中。然后,我使用了来自https://github.com/47deg/android-swipelistview的 SwipeListView ,最终一起玩得很好。