package bw.com.day11;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//控件
private PullToRefreshListView mPtf;
//数据源
private List<Bean.DataBean> totalList = new ArrayList<>();
//适配器
private MyAdapter adapter;
private String path = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=";
private int page = 1;//页码
private int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPtf = (PullToRefreshListView) findViewById(R.id.ptf_id);
adapter = new MyAdapter(this,totalList);
mPtf.setAdapter(adapter);
//加载数据, 刷新适配器
initData();
ListView lv = mPtf.getRefreshableView();
registerForContextMenu(lv);
mPtf.setMode(PullToRefreshBase.Mode.BOTH);
mPtf.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
page=1;
initData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
page++;
initData();
}
});
mPtf.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent();
intent.setClass(MainActivity.this,InfoActivity.class);
intent.putExtra("image",totalList.get(position).getPic());
intent.putExtra("title",totalList.get(position).getTitle());
startActivity(intent);
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.main,menu);
position = ((AdapterView.AdapterContextMenuInfo)menuInfo).position;
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.action_01:
Toast.makeText(this, "11111", Toast.LENGTH_SHORT).show();
break;
case R.id.action_02:
Toast.makeText(this, "222222", Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
//自定义加载数据的方法
public void initData()
{
RequestParams params = new RequestParams(path+page);
x.http().get(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String s) {
Bean bean = new Gson().fromJson(s,Bean.class);
List<Bean.DataBean> list = bean.getData();
if(list!=null && list.size()>0)
{
totalList.addAll(list);//把当前加载的第N页的数据, 填充到整个数据源中
adapter.notifyDataSetChanged();
}
//取消加载的进度
mPtf.onRefreshComplete();
}
@Override
public void onError(Throwable throwable, boolean b) {
}
@Override
public void onCancelled(CancelledException e) {
}
@Override
public void onFinished() {
}
});
}
}
package bw.com.day11;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import org.xutils.image.ImageOptions;
import org.xutils.x;
import java.util.List;
/**
* Created by Administrator on 2017/10/23.
*/
public class MyAdapter extends BaseAdapter{
private Context context;
private List<Bean.DataBean> data;
public MyAdapter(Context context,List<Bean.DataBean> data)
{
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView==null)
{
convertView = LayoutInflater.from(context).inflate(R.layout.item_lv,parent,false);
viewHolder = new ViewHolder();
viewHolder.tv = (TextView) convertView.findViewById(R.id.tv);
viewHolder.iv = (ImageView) convertView.findViewById(R.id.iv);
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tv.setText(data.get(position).getTitle());
//加载图片圆角处理
ImageOptions options = new ImageOptions.Builder()
.setCircular(true)
.build();
x.image().bind(viewHolder.iv,data.get(position).getPic(),options);
//动画
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f,0.0f);
alphaAnimation.setDuration(5000);
viewHolder.iv.startAnimation(alphaAnimation);
return convertView;
}
class ViewHolder
{
TextView tv;
ImageView iv;
}
}
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
x.Ext.setDebug(true);
//初始化Fresco
Fresco.initialize(this);
}
}
Bean.java 实体类
InfoActivity.java
package bw.com.day11;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
public class InfoActivity extends AppCompatActivity {
private SimpleDraweeView iv;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
initView();
Intent intent = getIntent();
String image = intent.getStringExtra("image");
String title = intent.getStringExtra("title");
tv.setText(title);
Uri uri = Uri.parse(image);
iv.setImageURI(uri);
}
private void initView() {
iv = (SimpleDraweeView) findViewById(R.id.iv);
tv = (TextView) findViewById(R.id.tv);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
>
<!--com.handmark.pulltorefresh.library.PullToRefreshGridView-->
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ptf_id"/>
</LinearLayout>
item+lv.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:text="糗百的内容"
android:layout_margin="10dp"
android:textSize="18sp"
/>
</LinearLayout>
info.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/iv"
fresco:placeholderImage="@mipmap/ic_launcher"
fresco:roundedCornerRadius="20dp"
fresco:roundTopLeft="true"
fresco:roundTopRight="true"
fresco:roundBottomLeft="true"
fresco:roundBottomRight="true"
fresco:roundingBorderWidth="5dp"
fresco:roundingBorderColor="@color/colorPrimary"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:text="糗百的内容"
android:layout_margin="10dp"
android:textSize="18sp"
/>
</LinearLayout>
菜单
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/action_01"
android:title="不好"
/>
<item
android:id="@+id/action_02"
android:title="好"
/>
</menu>