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 {
/**
* ret : 1
* data : [{"id":"8289","title":"油焖大虾","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg","collect_num":"1669","food_str":"大虾 葱 生姜 植物油 料酒","num":1669},{"id":"2127","title":"四川回锅肉","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2127.jpg","collect_num":"1591","food_str":"猪肉 青蒜 青椒 红椒 姜片","num":1591},{"id":"30630","title":"超简单芒果布丁","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/31/30630.jpg","collect_num":"1544","food_str":"QQ糖 牛奶 芒果","num":1544},{"id":"9073","title":"家常红烧鱼","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/10/9073.jpg","collect_num":"1425","food_str":"鲜鱼 姜 葱 蒜 花椒","num":1425},{"id":"10097","title":"家常煎豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10097.jpg","collect_num":"1419","food_str":"豆腐 新鲜红椒 青椒 葱花 油","num":1419},{"id":"10509","title":"水煮肉片","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10509.jpg","collect_num":"1341","food_str":"瘦猪肉 生菜 豆瓣酱 干辣椒 花椒","num":1341},{"id":"46968","title":"红糖苹果银耳汤","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/47/46968.jpg","collect_num":"1252","food_str":"银耳 苹果 红糖","num":1252},{"id":"10191","title":"麻婆豆腐","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/11/10191.jpg","collect_num":"1221","food_str":"豆腐 肉末 生抽 白糖 芝麻油","num":1221},{"id":"2372","title":"皮蛋瘦肉粥","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2372.jpg","collect_num":"1151","food_str":"大米 皮蛋 猪肉 油条 香葱","num":1151},{"id":"2166","title":"蚂蚁上树","pic":"http://www.qubaobei.com/ios/cf/uploadfile/132/3/2166.jpg","collect_num":"1144","food_str":"红薯粉 肉 姜 蒜 花椒","num":1144}]
*/
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 {
/**
* id : 8289
* title : 油焖大虾
* pic : http://www.qubaobei.com/ios/cf/uploadfile/132/9/8289.jpg
* collect_num : 1669
* food_str : 大虾 葱 生姜 植物油 料酒
* num : 1669
*/
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();
}
});
}
}
转载:https://blog.csdn.net/DreamMan_/article/details/102490776
查看评论