package com.qianqian.day0822yk1.fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import com.google.gson.Gson;
import com.qianqian.day0822yk1.R;
import com.qianqian.day0822yk1.adapter.MyGvAdapter;
import com.qianqian.day0822yk1.bean.PinDaoBean;
import com.qianqian.day0822yk1.utils.Gvutils;
import java.util.ArrayList;
import java.util.List;
public class Fragmentwd extends Fragment {
View view;
GridView gv1,gv2;
PinDaoBean pinDaoBean;
List<String> list1;
List<String> list2;
MyGvAdapter adapter1;
MyGvAdapter adapter2;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view=View.inflate(getActivity(), R.layout.fragmentjiewd,null);
ininview();
inidata();
return view;
}
private void ininview() {
gv1= view.findViewById(R.id.gv1);
gv2= view.findViewById(R.id.gv2);
adapter1=new MyGvAdapter(list1,getActivity());
adapter2=new MyGvAdapter(list2,getActivity());
gv1.setAdapter(adapter1);
gv2.setAdapter(adapter2);
gv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
list2.add(list1.get(i));
list1.remove(i);
adapter1.setData(list1);
adapter2.setData(list2);
}
});
gv2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
list1.add(list2.get(i));
list2.remove(i);
adapter1.setData(list1);
adapter2.setData(list2);
}
});
}
private void inidata() {
new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... strings) {
String result=Gvutils.getgvdata();
Gson gson=new Gson();
pinDaoBean = gson.fromJson(result, PinDaoBean.class);
list1=new ArrayList<String>();
list2=new ArrayList<String>();
for (int i = 0; i <pinDaoBean.getChannel().size() ; i++) {
list1.add(pinDaoBean.getChannel().get(i).getChannel_me());
list2.add(pinDaoBean.getChannel().get(i).getChannel_more());
}
return null;
}
@Override
protected void onPostExecute(String s) {
adapter1.setData(list1);
adapter2.setData(list2);
super.onPostExecute(s);
}
}.execute();
}
}
package com.qianqian.day0822yk1.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.qianqian.day0822yk1.R;
import com.qianqian.day0822yk1.bean.PinDaoBean;
import java.util.List;
public class MyGvAdapter extends BaseAdapter {
List<String>list;
Context context;
public MyGvAdapter(List<String> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
if(list!=null){
return list.size();
}
return 0;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHoler holer;
if (view==null) {
view=View.inflate(context, R.layout.gvitem,null);
holer=new ViewHoler();
holer.tv=view.findViewById(R.id.tv);
view.setTag(holer);
} else {
holer= (ViewHoler) view.getTag();
}
holer.tv.setText(list.get(i));
return view;
}
public void setData(List<String> list){
this.list=list;
notifyDataSetChanged();
}
}
class ViewHoler{
TextView tv;
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginLeft="20dp"
android:text="我的频道"/>
<GridView
android:id="@+id/gv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="5"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_marginLeft="20dp"
android:text="更多频道"/>
<GridView
android:id="@+id/gv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="5"
/>
</LinearLayout>