XML布局
<ListView
android:layout_weight="1"
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<LinearLayout
android:id="@+id/line"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="200dp">
</LinearLayout>
item布局
<ImageView
android:id="@+id/image_item"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:id="@+id/text_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="是是是"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="40dp"
/>
fragment布局
<TextView
android:id="@+id/fragment_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"
android:textSize="25dp"
android:gravity="center"
/>
HttpUtils
public class HttpUtils {
public static String getJson(String path) {
InputStream is = null;
ByteArrayOutputStream baos = null;
HttpURLConnection connection = null;
try {
URL url = new URL(path);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.connect();
if (connection.getResponseCode()==200){
is = connection.getInputStream();
baos = new ByteArrayOutputStream();
int len = 0;
byte b[] = new byte[1024];
while ((len=is.read(b))!=-1){
baos.write(b,0,len);
baos.flush();
}
return baos.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
JavaBean
public class JavaBean {
private int ret;
private List<DataBean> data;
public int getRet() {
return ret;
}
public void setRet(int ret) {
this.ret = ret;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String id;
private String title;
private String pic;
private String collect_num;
private String food_str;
private int num;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getCollect_num() {
return collect_num;
}
public void setCollect_num(String collect_num) {
this.collect_num = collect_num;
}
public String getFood_str() {
return food_str;
}
public void setFood_str(String food_str) {
this.food_str = food_str;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
}
MyAdapter
public class MyAdapter extends BaseAdapter {
private List<JavaBean.DataBean> list;
private Context context;
private LayoutInflater layoutInflater;
public MyAdapter(List<JavaBean.DataBean> list, Context context) {
this.list = list;
this.context = context;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if (view == null){
holder = new ViewHolder();
view = layoutInflater.inflate(R.layout.layout_item,null);
holder.imageView_item = view.findViewById(R.id.image_item);
holder.textView_item = view.findViewById(R.id.text_item);
view.setTag(holder);
}else {
holder = (ViewHolder) view.getTag();
}
Glide.with(context).load(list.get(i).getPic()).into(holder.imageView_item);
holder.textView_item.setText(list.get(i).getTitle());
return view;
}
static class ViewHolder{
private ImageView imageView_item;
private TextView textView_item;
}
}
MyAsyncTask
public class MyAsyncTask extends AsyncTask<String,Void, List<JavaBean.DataBean>> {
private List<JavaBean.DataBean> list;
private MyAdapter adapter;
private Context context;
public MyAsyncTask(List<JavaBean.DataBean> list, MyAdapter adapter, Context context) {
this.list = list;
this.adapter = adapter;
this.context = context;
}
@Override
protected List<JavaBean.DataBean> doInBackground(String... strings) {
String json = HttpUtils.getJson(strings[0]);
Log.i("tag", "doInBackground: +++++++++++++++"+json);
if (json!=null){
return new Gson().fromJson(json,JavaBean.class).getData();
}
return null;
}
@Override
protected void onPostExecute(List<JavaBean.DataBean> dataBeans) {
super.onPostExecute(dataBeans);
list.addAll(dataBeans);
adapter.notifyDataSetChanged();
}
}
Java代码
public class MainActivity extends AppCompatActivity {
private String urlString = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1";
private ListView listView;
private LinearLayout line;
private List<JavaBean.DataBean> list = new ArrayList<>();
private MyAdapter adapter;
private BlankFragment blankFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
new MyAsyncTask(list, adapter, this).execute(urlString);
}
private void initView() {
listView = (ListView) findViewById(R.id.listView);
line = (LinearLayout) findViewById(R.id.line);
adapter = new MyAdapter(list, this);
listView.setAdapter(adapter);
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.line,new BlankFragment());
fragmentTransaction.commit();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
FragmentManager supportFragmentManager1 = getSupportFragmentManager();
FragmentTransaction fragmentTransaction1 = supportFragmentManager1.beginTransaction();
Bundle bundle = new Bundle();
bundle.putString("str",list.get(i).getFood_str());
blankFragment = new BlankFragment();
blankFragment.setArguments(bundle);
fragmentTransaction1.replace(R.id.line,blankFragment);
fragmentTransaction1.commit();
}
});
}
}