先来看看效果:
开始上代码 , 首先是布局:
MainActivity的布局:
<?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" android:orientation="vertical" tools:context="bawe.guotaian0920rikao.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:text="我的频道" android:textSize="40sp" android:layout_weight="1" /> <GridView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="5" android:id="@+id/gv_gd" android:numColumns="3" ></GridView> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:text="推荐频道" android:textSize="40sp" android:layout_weight="1" /> <GridView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="5" android:id="@+id/gv__wd" android:numColumns="3" ></GridView> </LinearLayout>
======================================================================================
item布局:
==========================================<?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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_name" android:textSize="30sp" /> </LinearLayout>
接下来
MainActivity代码:
import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.TextView; import com.google.gson.Gson; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity { GridView gv_wd, gv_gd; channeldata cd; List<String> list_top; List<String> list; private myadapter myadapter; private myadapter1 myadapter1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gv_gd = (GridView) findViewById(R.id.gv_gd); gv_wd = (GridView) findViewById(R.id.gv__wd); initdata(); myadapter = new myadapter(); myadapter1 = new myadapter1(); gv_gd.setAdapter(myadapter); gv_wd.setAdapter(myadapter1); } private void initdata() { // TODO Auto-generated method stub new AsyncTask<String, String, String>() { @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub return getdata(); } @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub super.onPostExecute(result); Gson gson = new Gson(); cd = gson.fromJson(result, channeldata.class); // Log.i("=====", cd.toString()); list_top = new ArrayList<String>(); list = new ArrayList<String>(); for (int i = 0; i < cd.getChannel().size(); i++) { list_top.add(cd.getChannel().get(i).getChannel_me()); list.add(cd.getChannel().get(i).getChannel_more()); } gv_gd.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub list_top.add(list.get(position)); list.remove(position); myadapter.notifyDataSetChanged(); myadapter1.notifyDataSetChanged(); } }); gv_wd.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub list.add(list_top.get(position)); list_top.remove(position); myadapter.notifyDataSetChanged(); myadapter1.notifyDataSetChanged(); } }); myadapter.notifyDataSetChanged(); myadapter1.notifyDataSetChanged(); } }.execute(); } class myadapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub if (list != null) { return list.size(); } return 0; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder; if (convertView == null) { convertView = View.inflate(MainActivity.this, R.layout.item, null); holder = new ViewHolder(); holder.tv_name = (TextView) convertView .findViewById(R.id.tv_name); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.tv_name.setText(list.get(position)); return convertView; } } static class ViewHolder { TextView tv_name; } class myadapter1 extends BaseAdapter { private static final int Object = 0; @Override public int getCount() { // TODO Auto-generated method stub if (list_top != null) { return list_top.size(); } return 0; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list_top.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder1 holder1; if (convertView == null) { convertView = View.inflate(MainActivity.this, R.layout.item, null); holder1 = new ViewHolder1(); holder1.tv_name1 = (TextView) convertView .findViewById(R.id.tv_name); convertView.setTag(holder1); } else { holder1 = (ViewHolder1) convertView.getTag(); } holder1.tv_name1.setText(list_top.get(position)); return convertView; } } static class ViewHolder1 { TextView tv_name1; } protected String getdata() { // TODO Auto-generated method stub String result = null; try { URL url = new URL( "http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=channel"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); int code = conn.getResponseCode(); if (code == 200) { InputStream is = conn.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len = -1; byte[] buff = new byte[1024]; while ((len = is.read(buff)) != -1) { bos.write(buff, 0, len); } result = new String(bos.toByteArray()); } } catch (Exception e) { e.printStackTrace(); } return result; } }最后写一个接口封装类就好了,接口http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=channel