Android拉动刷新:如何刷新

时间:2016-09-01 00:16:34

标签: android

如何使用滑动视图使用onRefresh刷新我的列表视图。这是我的代码谢谢。我不能让事情发挥作用。

public class rentList extends Fragment implements AsyncResponse, AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {
final String TAG = this.getClass().getName();
private ArrayList<Cars> carList;
private ListView lvCars;
private FunDapter<Cars> adapter;
SharedPreferences pref;
SharedPreferences.Editor editor;
SwipeRefreshLayout swipeCar;

public rentList() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v =  inflater.inflate(R.layout.fragment_rent_list, container, false);

    ImageLoader.getInstance().init(UILConfig.config(getActivity()));
    PostResponseAsyncTask taskRead = new PostResponseAsyncTask(getActivity(), this);

    taskRead.execute("http://carkila.esy.es/product.php");

    lvCars = (ListView) v.findViewById(R.id.lvCars);
    registerForContextMenu(lvCars);

    pref = getActivity().getSharedPreferences("Login.conf", Context.MODE_PRIVATE);

    Log.d(TAG, pref.getString("username",""));
    Log.d(TAG, pref.getString("password",""));

    swipeCar = (SwipeRefreshLayout) v.findViewById(R.id.swipeCar);
    swipeCar.setOnRefreshListener(this);

    return v;
}

@Override
public void processFinish(String s) {

    carList = new JsonConverter<Cars>().toArrayList(s, Cars.class);

    BindDictionary<Cars> dict = new BindDictionary<Cars>();

    dict.addDynamicImageField(R.id.ivImg, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return product.Image;
        }
    }, new DynamicImageLoader() {
        @Override
        public void loadImage(String url, ImageView imageView) {

            ImageLoader.getInstance().displayImage(url, imageView); // Default options will be used

        }
    });
    dict.addStringField(R.id.tvCarModel, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Car Model: " + product.Car_Model;
        }
    });
    dict.addStringField(R.id.tvCarType, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Car Type: " + product.Car_Type;
        }
    });

    dict.addStringField(R.id.tvCapacity, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Capacity: " + product.Capacity;
        }
    });
    dict.addStringField(R.id.tvFuelType, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Fuel Type: " + product.FuelType;
        }
    });
    dict.addStringField(R.id.tvPlateNumber, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Plate Number: " + product.PlateNumber;
        }
    });
    dict.addStringField(R.id.tvPoster, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Posted by: " + product.owner;
        }
    });
    dict.addStringField(R.id.tvPrice, new StringExtractor<Cars>() {
        @Override
        public String getStringValue(Cars product, int position) {
            return "Price: " + product.carPrice;
        }
    });

    adapter = new FunDapter<>(
            getActivity(), carList, R.layout.layout_list, dict);
    if(swipeCar.isRefreshing()){
        swipeCar.setRefreshing(false);
    }

    lvCars.setAdapter(adapter);
    lvCars.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    Cars selectedCars = carList.get(position);
    Intent in = new Intent(getActivity(), DetailActivity.class);
    in.putExtra("cars", selectedCars);
    startActivity(in);
}

@Override
public void onRefresh() {
    Toast.makeText(getActivity(), "refreshhh!!!", Toast.LENGTH_SHORT).show();
}
}

.XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.kun.carkila.ListActivity"
tools:showIn="@layout/activity_list">

<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/swipeCar">
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lvCars"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:outlineProvider="none"/>

</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

你需要打电话

PostResponseAsyncTask taskRead = new PostResponseAsyncTask(getActivity(), this); 
taskRead.execute("carkila.esy.es/product.php");

在onRefresh方法中。隐藏圈子由&#34; processFinish方法&#34;并且在&#34; onCreate&#34;中创建适配器实例方法和调用&#34; notifyDataSetChange&#34;方法