import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.lfg.Utils.MyVolley;
import org.lfg.Utils.StringRequestUTF;
import org.lfg.cityselect_activity.CityHomeActivity;
import org.lfg.widgets.ListViewForScrollView;
import BaseAdapter.Contains;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.nuomi.AllActivity;
import com.example.nuomi.R;
import com.handmark.pulltorefresh.library.MyScrollView;
import com.handmark.pulltorefresh.library.MyScrollView.OnScrollListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.nostra13.universalp_w_picpathloader.core.DisplayImageOptions;
import com.nostra13.universalp_w_picpathloader.core.ImageLoader;
public class todayFragment extends Fragment
{
private ListViewForScrollView mlistmnue;
private PullToRefreshScrollView mScrollview;
protected ImageLoader p_w_picpathLoader = ImageLoader.getInstance();
private ArrayList<MenuList> context = new ArrayList<todayFragment.MenuList>();
private String strUrl = Contains.strUrl;
private DisplayImageOptions options;
private mlistmnueAdapter mAdapter;
private TextView mTvall;
private ImageButton mBtnBackTop;
private Button mLocationcity;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View layout = inflater.inflate(R.layout.home, null);
InitUI(layout);
Options();
InitScroll(layout);
HttpConn();
return layout;
}
/*
* 却接受CityHomeActivity的消息
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(data != null)
{
if(resultCode == 1)
{
String cityName = data.getStringExtra("city");
int end = cityName.indexOf("市");
mLocationcity.setText(cityName.substring(0, end));
}
}else
{
Toast.makeText(getActivity(), "亲!定位失败哦~~~", Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
private void InitUI(View layout) {
mBtnBackTop = (ImageButton) layout.findViewById(R.id.img_backtop);
mBtnBackTop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ScrollView scrollView = (ScrollView) mScrollview.findViewById(R.id.scrollview);
scrollView.smoothScrollTo(0, 0);
}
});
// 跳转全部团购页面
TextView find_more = (TextView) layout.findViewById(R.id.find_more);
find_more.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),AllActivity.class);
startActivity(intent );
}
});
// 定位城市事件
mLocationcity = (Button) layout.findViewById(R.id.btn_locationcity);
mLocationcity.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),CityHomeActivity.class);
startActivityForResult(intent, 0);
}
});
}
@SuppressLint("NewApi")
private void HttpConn() {
Listener<String> listener = new Listener<String>()
{
@Override
public void onResponse(String response)
{
try {
JSONObject jsonObj = new JSONObject(response);
JSONArray jsonarr = jsonObj.getJSONArray("deals");
for (int i = 0; i < jsonarr.length(); i++)
{
JSONObject jsonObject = jsonarr.getJSONObject(i);
String name = jsonObject.getString("name");
String thumbnail = jsonObject.getString("thumbnail");
String solds = jsonObject.getString("solds");
String price = jsonObject.getString("price");
String value = jsonObject.getString("value");
String title = jsonObject.getString("title");
MenuList menuList = new MenuList(name,thumbnail,solds,price,value,title);
context.add(menuList);
}
} catch (JSONException e) {
e.printStackTrace();
}
mAdapter.notifyDataSetChanged();
}
};
ErrorListener errorListener = new ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
// Toast.makeText(getActivity(), "网络连接有问题"+error, Toast.LENGTH_SHORT).show();
}
};
volleyGet(strUrl, listener, errorListener);
}
// 下载文件(注意结合ListView 数据设置始终是去设置ListView)
private void volleyGet(String url, Listener<String> listener,
ErrorListener errorListener)
{
RequestQueue queue = MyVolley.getRequestQueue();
StringRequest request =
new StringRequestUTF(Method.GET,url,listener,errorListener);
queue.add(request);
}
// 图片下载前、显示
private void Options()
{
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.about_logo)
.showImageForEmptyUri(R.drawable.about_logo)
.showImageOnFail(R.drawable.about_logo)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
// .displayer(new RoundedBitmapDisplayer(8))
.build();
}
// 解决ScrollView滑动的问题
private void InitScroll(View layout) {
mlistmnue = (ListViewForScrollView) layout.findViewById(R.id.listViewForScrollView1);
mAdapter = new mlistmnueAdapter();
mlistmnue.setAdapter(mAdapter);
// 使用ScrollView滑动到最顶部
mScrollview = (PullToRefreshScrollView) layout.findViewById(R.id.scrollView1);
MyScrollView scrollView = (MyScrollView) mScrollview.findViewById(R.id.scrollview);
scrollView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScroll(int scrollY) {
if (scrollY == 0)
{
mBtnBackTop.setVisibility(View.INVISIBLE);
}
else
{
mBtnBackTop.setVisibility(View.VISIBLE);
}
}
});
scrollView.smoothScrollTo(0, 0);
mScrollview.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(PullToRefreshBase refreshView) {
new Thread()
{
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
getActivity().runOnUiThread( new Runnable() {
public void run() {
mScrollview.onRefreshComplete();
}
});
}
}.start();
}
});
}
private ImageView mbackTop;
// 设置适配器
class mlistmnueAdapter extends BaseAdapter
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ListInfo listInfo = null;
View inflate = null;
if(convertView == null)
{
listInfo = new ListInfo();
inflate = getActivity().getLayoutInflater().inflate(R.layout.munulist, null);
listInfo.LTiltle = (TextView) inflate.findViewById(R.id.Title);
listInfo.LStore = (TextView) inflate.findViewById(R.id.Store);
listInfo.LDesContext = (TextView) inflate.findViewById(R.id.DesContext);
listInfo.LCurrprice = (TextView) inflate.findViewById(R.id.Currprice);
listInfo.LOldprice = (TextView) inflate.findViewById(R.id.Oldprice);
listInfo.LCount = (TextView) inflate.findViewById(R.id.Count);
listInfo.mImageView = (ImageView) inflate.findViewById(R.id.p_w_picpath_advertes);
// 解决如何去实现点击跳到顶部
// mbackTop = (ImageView) inflate.findViewById(R.id.img_backtop);
inflate.setTag(listInfo);
}
else
{
inflate = convertView;
listInfo = (ListInfo) inflate.getTag();
}
// scrollView.smoothScrollTo(0, 0);
MenuList menuList = context.get(position);
listInfo.LTiltle.setText(menuList.name);
String img = menuList.thumbnail;
p_w_picpathLoader .displayImage(
img,
listInfo.mImageView,
options,
null);
return inflate;
}
@Override
public int getCount()
{
return context.size();
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position)
{
return 0;
}
}
class ListInfo
{
TextView LTiltle ;
TextView LStore;
TextView LDesContext;
TextView LCurrprice;
TextView LOldprice;
TextView LCount;
ImageView mImageView;
}
class MenuList
{
private String name;
private String thumbnail;
private String solds;
private String price;
private String value;
private String address;
public MenuList(String name, String thumbnail, String solds,
String price, String value, String address) {
this.name = name;
this.thumbnail = thumbnail;
this.solds = solds;
this.price = price;
this.value = value;
this.address = address;
}
@Override
public String toString() {
return "MenuList [name=" + name + ", img=" + thumbnail + ", solds="
+ solds + ", price=" + price + ", value=" + value
+ ", address=" + address + "]";
}
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。