2016-09-01 205 views
-2

如何使用滑動視圖使用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> 
+0

問題仍然不清楚。你能解釋一下你想要達到的目標嗎? – dex

+0

@dex - 我不能讓我的'swiperefreshlayout'工作。 :c –

+0

當你拉動刷新你有沒有得到某種形式的圓?你有沒有在onRefresh方法中得到Toast消息? – dex

回答

0

需要調用

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

onRefresh方法內。隱藏的循環由「processFinish方法」處理,並且在「onCreate」方法中創建適配器實例並調用「notifyDataSetChange」方法