需要加网络权限
<uses-permission android:name="android.permission.INTERNET">uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">uses-permission>
1:布局文件
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <LinearLayout android:orientation="horizontal" android:layout_width="150dp" android:layout_height="match_parent" android:id="@+id/left"/> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/right"/> LinearLayout>let.xml
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" > <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv_left" >ListView> LinearLayout>right.xml
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"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/lv_right" >ListView> LinearLayout>left_item.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"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/left_text" android:text="dwd" /> LinearLayout>right_item.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"> <LinearLayout android:layout_margin="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:padding="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fwfwf" android:id="@+id/text1" /> <TextView android:padding="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="fwfwf" android:id="@+id/text22" /> LinearLayout> <ImageView android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/image22" android:src="@mipmap/ic_launcher" /> LinearLayout> LinearLayout>
2:代码
MainActivity
package com.example.demo_sanzhou20170422; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Bundle; import android.provider.Settings; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import fragment.Fragment_left; public class MainActivity extends AppCompatActivity { Fragment_left mFragment_left = new Fragment_left(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportFragmentManager().beginTransaction().replace(R.id.left,mFragment_left,null).commit(); NetWorkStatus(); } public boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } } return false; } private boolean NetWorkStatus() { boolean netSataus = isNetworkConnected(this); if (!netSataus) { AlertDialog.Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络") .setMessage("是否对网络进行设置?"); b.setPositiveButton("是", new DialogInterface.OnClickListener() { private ProgressDialog mDialog; public void onClick(DialogInterface dialog, int whichButton) { if(android.os.Build.VERSION.SDK_INT > 10 ){ //3.0以上打开设置界面 startActivity(new Intent(Settings.ACTION_SETTINGS)); }else { startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS)); } } }).setNeutralButton("否", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.cancel(); } }).show(); } return netSataus; } }3左右fragment
Fragment_left
package fragment; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import com.example.demo_sanzhou20170422.R; import java.util.ArrayList; import java.util.List; import bean.ListBean; /** * Created by 1 on 2017/4/22. */ public class Fragment_left extends Fragment { //定义左边fragment数据 private ListmList = new ArrayList (); private View mV; private ListView mMlv_left; private int location=0; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { mV = inflater.inflate(R.layout.let, null); //初始化控件 mMlv_left = (ListView) mV.findViewById(R.id.lv_left); initData();//初始化数据 return mV; } private void initData() { for(int x=0; x<10 ; x=x+2){ mList.add(new ListBean(x,x+1)); } MyAdapter adapter = new MyAdapter(); mMlv_left.setAdapter(adapter); mMlv_left.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { if(position!=location){ //Color.TRANSPARENT透明色 parent.getChildAt(location).setBackgroundColor(Color.TRANSPARENT); } //设置选中item的颜色 view.setBackgroundColor(Color.RED); //把数据传给右边的Fragment Fragment_Right rf =new Fragment_Right(); Bundle bundle = new Bundle(); bundle.putInt("num1",mList.get(position).getNum1()); bundle.putInt("num2",mList.get(position).getNum2()); rf.setArguments(bundle); getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.right,rf,null).commit(); //得到解析数据 rf.initData(); location=position; } }); } class MyAdapter extends BaseAdapter { @Override public int getCount() { return mList.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView==null){ convertView=View.inflate(getActivity(),R.layout.left_item,null); TextView text_left= (TextView) convertView.findViewById(R.id.left_text); text_left.setText("条目"+mList.get(position).getNum1()+""+mList.get(position).getNum2()); } return convertView; } } }
Fragment_Right
package fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import com.example.demo_sanzhou20170422.R; import com.google.gson.Gson; import org.xutils.common.Callback; import org.xutils.http.RequestParams; import org.xutils.x; import java.util.ArrayList; import java.util.List; import adapter.RightAdapter; import bean.GsonBean; /** * Created by 1 on 2017/4/22. */ public class Fragment_Right extends Fragment { List3:适配器list = new ArrayList<>(); private View mView_right; private ListView mLv; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { mView_right = inflater.inflate(R.layout.right, null); mLv = (ListView) mView_right.findViewById(R.id.lv_right); return mView_right; } public void initData(){ RequestParams params = new RequestParams("http://www.meirixue.com/api.php?c=list&a=index"); x.http().get(params, new Callback.CommonCallback () { private RightAdapter mAdapter; @Override public void onSuccess(String result) { Gson gson = new Gson(); GsonBean bean = gson.fromJson(result, GsonBean.class); List datalist = bean.getDatalist(); Bundle bundle = getArguments(); int num1 = bundle.getInt("num1"); int num2 = bundle.getInt("num2"); for (int i = 0; i if (num1 == i || num2 == i) { list.add(datalist.get(i)); } } RightAdapter mAdapter = new RightAdapter(getActivity(), list); mLv.setAdapter(mAdapter); } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); } }
MyAdapter_left
package adapter; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; /** * Created by 1 on 2017/4/22. */ public class MyAdapter_left extends BaseAdapter { private Context context; @Override public int getCount() { return 0; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { return null; } }
RightAdapter
package adapter; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.example.demo_sanzhou20170422.R; import org.xutils.image.ImageOptions; import org.xutils.x; import java.util.List; import bean.GsonBean; public class RightAdapter extends BaseAdapter { private Listbean文件:list; private Context context; private ImageOptions options; public RightAdapter(Context context, List list) { this.context = context; this.list = list; options=new ImageOptions.Builder().setFadeIn(true).setLoadingDrawableId(R.mipmap.ic_launcher) .setCrop(true).setSize(300,190).setUseMemCache(true).build(); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView==null){ convertView=View.inflate(context,R.layout.right_item,null); holder=new ViewHolder(); holder.t1= (TextView) convertView.findViewById(R.id.text1); holder.t2= (TextView) convertView.findViewById(R.id.text22); holder.image= (ImageView) convertView.findViewById(R.id.image22); convertView.setTag(holder); }else { holder= (ViewHolder) convertView.getTag(); } holder.t1.setText(list.get(position).getCourse_name()); holder.t2.setText(list.get(position).getCourse_price()); x.image().bind(holder.image,list.get(position).getCourse_pic(),options); return convertView; } class ViewHolder{ TextView t1; TextView t2; ImageView image; } }
ListBean
package bean; public class ListBean { private int num1; private int num2; public ListBean(int num1, int num2) { this.num1 = num1; this.num2 = num2; } public ListBean() { } @Override public String toString() { return "ListBean{" + "num1=" + num1 + ", num2=" + num2 + '}'; } public int getNum1() { return num1; } public void setNum1(int num1) { this.num1 = num1; } public int getNum2() { return num2; } public void setNum2(int num2) { this.num2 = num2; } }GsonBean
package bean; import java.util.List; public class GsonBean { /** * datalist : [{"cid":"5857","course_tname":"丁丁","course_name":"3DMAX效果图-实战篇(欧式二)","course_price":"20.00","course_pic":"http://img.dianfu.net/img/20170216/40799f98cfc235f2a2eb46837d5cff84.jpg","course_paycount":"0","school_name":"吉大教育"},{"cid":"1167","course_tname":"中公教育","course_name":"行测数量关系核心考点之行程问题1","course_price":"0.00","course_pic":"http://img.dianfu.net/img/20150914/1d132d22df1e8b5331ff960ff96f0158.jpg","course_paycount":"30","school_name":"中公教育"},{"cid":"1170","course_tname":"中公教育","course_name":"行测数量关系核心考点之行程问题2","course_price":"0.00","course_pic":"http://img.dianfu.net/img/20150914/92048cc02319aba296cdadf9140c6602.jpg","course_paycount":"41","school_name":"中公教育"},{"cid":"1173","course_tname":"中公教育","course_name":"行测数量关系核心考点之行程问题3","course_price":"0.00","course_pic":"http://img.dianfu.net/img/20150914/aaed07356bea4bc4e1a24b6164297f5d.jpg","course_paycount":"24","school_name":"中公教育"},{"cid":"1161","course_tname":"中公教育","course_name":"行测数量关系 核心考点之不定方程1","course_price":"0.00","course_pic":"http://img.dianfu.net/img/20150911/3e8af2128c1ead7f86e45797ae13f91a.jpg","course_paycount":"17","school_name":"中公教育"},{"cid":"1162","course_tname":"中公教育","course_name":"行测数量关系 核心考点之不定方程2","course_price":"0.00","course_pic":"http://img.dianfu.net/img/20150911/1623e1ea09471f3a7eef7c10d6fa6979.jpg","course_paycount":"33","school_name":"中公教育"},{"cid":"1163","course_tname":"中公教育","course_name":"行测数量关系 核心考点之不定方程3","course_price":"0.00","course_pic":"http://img.dianfu.net/img/20150911/7c86ea4816766dc04f491d5066a8a67c.jpg","course_paycount":"11","school_name":"中公教育"},{"cid":"1156","course_tname":"中公教育","course_name":"行测数量关系核心考点之数学运算必备知识1","course_price":"0.00","course_pic":"http://img.dianfu.net/img/20150911/7c3297250d255e59f781079b3e33db12.jpg","course_paycount":"21","school_name":"中公教育"},{"cid":"1146","course_tname":"中公教育","course_name":"行测数量关系核心考点之数学运算必备知识2","course_price":"0.00","course_pic":"http://img.dianfu.net/img/20150911/6f945dd193ef1ff8d2ae85b0ea7a725d.jpg","course_paycount":"32","school_name":"中公教育"},{"cid":"1149","course_tname":"中公教育","course_name":"行测数量关系核心考点之数学运算必备知识3","course_price":"0.00","course_pic":"http://img.dianfu.net/img/20150911/f5839810c1d9be265ccb8b29f11b206b.jpg","course_paycount":"34","school_name":"中公教育"}] * count : 3819 * limit : 10 * curpage : 1 */ private int count; private int limit; private int curpage; private Listdatalist; public int getCount() { return count; } public void setCount(int count) { this.count = count; } public int getLimit() { return limit; } public void setLimit(int limit) { this.limit = limit; } public int getCurpage() { return curpage; } public void setCurpage(int curpage) { this.curpage = curpage; } public List getDatalist() { return datalist; } public void setDatalist(List datalist) { this.datalist = datalist; } public static class DatalistBean { /** * cid : 5857 * course_tname : 丁丁 * course_name : 3DMAX效果图-实战篇(欧式二) * course_price : 20.00 * course_pic : http://img.dianfu.net/img/20170216/40799f98cfc235f2a2eb46837d5cff84.jpg * course_paycount : 0 * school_name : 吉大教育 */ private String cid; private String course_tname; private String course_name; private String course_price; private String course_pic; private String course_paycount; private String school_name; public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getCourse_tname() { return course_tname; } public void setCourse_tname(String course_tname) { this.course_tname = course_tname; } public String getCourse_name() { return course_name; } public void setCourse_name(String course_name) { this.course_name = course_name; } public String getCourse_price() { return course_price; } public void setCourse_price(String course_price) { this.course_price = course_price; } public String getCourse_pic() { return course_pic; } public void setCourse_pic(String course_pic) { this.course_pic = course_pic; } public String getCourse_paycount() { return course_paycount; } public void setCourse_paycount(String course_paycount) { this.course_paycount = course_paycount; } public String getSchool_name() { return school_name; } public void setSchool_name(String school_name) { this.school_name = school_name; } } }
//Myapp
package com.example.demo_sanzhou20170422; import android.app.Application; import org.xutils.x; /** * Created by 1 on 2017/4/23. */ public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); x.Ext.init(this); x.Ext.setDebug(false); } }