关于多条目 xlistview
activity_main
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout 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:id="@+id/myDrawer" tools:context="zhanghaijiao.bawei.com.zhonghelianxi.MainActivity"> <!--主内容区域--> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true" android:background="#00f" android:id="@+id/rg"> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:text="首页" android:button="@null" android:layout_weight="1" android:gravity="center" android:id="@+id/rb_index"/> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:text="资讯" android:button="@null" android:layout_weight="1" android:gravity="center" android:id="@+id/rb_news"/> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:text="设置" android:button="@null" android:layout_weight="1" android:gravity="center" android:id="@+id/rb_set"/> </RadioGroup> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fcontent" android:layout_above="@id/rg"></FrameLayout> </RelativeLayout> <!--菜单区域--> <LinearLayout android:layout_width="180dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#eee"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> </LinearLayout> </android.support.v4.widget.DrawerLayout>
main_activity
package zhanghaijiao.bawei.com.zhonghelianxi; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.RadioGroup; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RadioGroup radioGroup=findViewById(R.id.rg); //默认显示首页的fragement getSupportFragmentManager().beginTransaction().replace(R.id.fcontent,new IndexFragment()).commit(); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { switch (i){ case R.id.rb_index: getSupportFragmentManager().beginTransaction().replace(R.id.fcontent,new IndexFragment()).commit(); break; case R.id.rb_news: getSupportFragmentManager().beginTransaction().replace(R.id.fcontent,new NewsFragment()).commit(); break; case R.id.rb_set: getSupportFragmentManager().beginTransaction().replace(R.id.fcontent,new SetFragment()).commit(); break; } } }); } }
NewsFragment
package zhanghaijiao.bawei.com.zhonghelianxi; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.ArrayList; import java.util.List; /** * Created by jane on 2018/4/20. */ public class NewsFragment extends Fragment { private TabLayout myTab; private ViewPager viewPager; private List<DataType> menus; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v=inflater.inflate(R.layout.news,container,false); myTab = v.findViewById(R.id.myTab); viewPager = v.findViewById(R.id.vp); return v; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //初使化菜单内容 initMenus(); //getSupportFragmentManager //getChildFragmentManager:在嵌套的fragment中获取管理类对象 //getFragmentManager(); 在多层嵌套的fragment中获取管理类对象 //设置适配器 MyVpAdapter adapter=new MyVpAdapter(getChildFragmentManager()); viewPager.setAdapter(adapter); //进行关联 myTab.setupWithViewPager(viewPager); } class MyVpAdapter extends FragmentPagerAdapter{ public MyVpAdapter(FragmentManager fm) { super(fm); } @Override public CharSequence getPageTitle(int position) { //注意:返回是的title return menus.get(position).getTitle(); } @Override public Fragment getItem(int position) { ContentFragment contentFragment=new ContentFragment(); //将类型传递过去 Bundle bundle=new Bundle(); bundle.putString("type",menus.get(position).getType()); contentFragment.setArguments(bundle); return contentFragment; } @Override public int getCount() { return menus.size(); } } private void initMenus() { menus=new ArrayList<>(); menus.add(new DataType("数据新闻","xbsjxw")); menus.add(new DataType("快讯","txs")); menus.add(new DataType("头条","toutiao")); menus.add(new DataType("精编公告","news/mobile/jbgg")); menus.add(new DataType("美股","news/mobile/mgxw")); menus.add(new DataType("港股","news/mobile/ggxw")); menus.add(new DataType("基金","news/mobile/jjxw")); menus.add(new DataType("理财","news/mobile/lcxw")); } }
ContentFragment
import com.google.gson.reflect.TypeToken; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Type; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; /** * Created by jane on 2018/4/20. */ public class ContentFragment extends Fragment { private XListView xListView; private int pageIndex=1; private int operType=1;//1:刷新 2:加载更多 private String myUrl; private List<ResultData.DataBean> datas; private MyAdapter adapter; private String type; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v=inflater.inflate(R.layout.content,container,false); xListView = v.findViewById(R.id.xlv); datas=new ArrayList<>(); return v; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //初使化 initXLv(); //接收参数 Bundle arguments = getArguments(); type = arguments.getString("type"); //拼接url地址 myUrl="http://mnews.gw.com.cn/wap/data/news/"+ type +"/page_"+pageIndex+".json"; //请求网络数据 requestNetData(); } private void requestNetData() { MyTask myTask=new MyTask(); myTask.execute(myUrl); } class MyTask extends AsyncTask<String,Void,String>{ @Override protected String doInBackground(String... strings) { try { URL url=new URL(strings[0]); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setConnectTimeout(5000); urlConnection.setReadTimeout(5000); if(urlConnection.getResponseCode()==200){ InputStream inputStream = urlConnection.getInputStream(); String s = streamToString(inputStream); return s; } } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); List<ResultData> lists=new ArrayList<>(); //得到type对象 Type t=new TypeToken<List<ResultData>>(){}.getType(); //对数组数据json 的解析 Gson gson=new Gson(); lists=gson.fromJson(s,t); //选择出 列表 要显示的数据源 List<ResultData.DataBean> data = lists.get(0).getData(); if(operType==1){ datas.clear();//刷新时,清空所有数据 } //添加新数据过来 datas.addAll(data); setAdapter(); if(operType==1){ xListView.stopRefresh(); //设置刷新时间 }else{ xListView.stopLoadMore(); } } } private void setAdapter(){ if(adapter==null){ //设置适配器 adapter = new MyAdapter(datas,getActivity()); xListView.setAdapter(adapter); }else{ adapter.notifyDataSetChanged(); } } private String streamToString(InputStream stream) { StringBuilder sbuilder=new StringBuilder(); String str; BufferedReader reader=new BufferedReader(new InputStreamReader(stream)); try { while ((str=reader.readLine())!=null){ sbuilder.append(str); } } catch (Exception e) { e.printStackTrace(); } return sbuilder.toString(); } private void initXLv() { xListView.setPullRefreshEnable(true);//下拉刷新 xListView.setPullLoadEnable(true);//上拉加载 xListView.setXListViewListener(new XListView.IXListViewListener() { @Override public void onRefresh() { pageIndex=1; myUrl="http://mnews.gw.com.cn/wap/data/news/"+ type +"/page_"+pageIndex+".json"; operType=1; requestNetData(); } @Override public void onLoadMore() { pageIndex++; myUrl="http://mnews.gw.com.cn/wap/data/news/"+ type +"/page_"+pageIndex+".json"; operType=2; requestNetData(); } }); } }
IndexFragment
package zhanghaijiao.bawei.com.zhonghelianxi; 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; /** * Created by jane on 2018/4/20. */ public class IndexFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v=inflater.inflate(R.layout.index,container,false); return v; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } }
DataType
package zhanghaijiao.bawei.com.zhonghelianxi; /** * Created by jane on 2018/4/20. */ public class DataType { private String title; private String type; public DataType(String title, String type) { this.title = title; this.type = type; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getType() { return type; } public void setType(String type) { this.type = type; } }
MyAdapter
package zhanghaijiao.bawei.com.zhonghelianxi; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; /** * Created by jane on 2018/4/20. */ public class MyAdapter extends BaseAdapter { private List<ResultData.DataBean> data; private Context context; public MyAdapter(List<ResultData.DataBean> data, Context context) { this.data = data; this.context = context; } @Override public int getCount() { return data.size(); } @Override public Object getItem(int i) { return data.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder; if(view==null){ view=View.inflate(context,R.layout.item,null); holder=new ViewHolder(); holder.textView=view.findViewById(R.id.tv_title); view.setTag(holder); }else { holder= (ViewHolder) view.getTag(); } holder.textView.setText(data.get(i).getTitle()); return view; } class ViewHolder{ TextView textView; } }
ResultData
略过
newsitems
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" > <!--横向滑动菜单 --> <android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/myTab" app:tabGravity="center" app:tabIndicatorColor="@color/colorAccent" app:tabMode="scrollable" app:tabSelectedTextColor="@color/colorPrimaryDark" app:tabTextColor="@color/colorPrimary"></android.support.design.widget.TabLayout> <!--viewPager组件--> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/vp" android:layout_below="@+id/myTab"></android.support.v4.view.ViewPager> </RelativeLayout>
setitem
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0ff"> </RelativeLayout>
contentitem
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <com.bawei.jane.mxlistview.view.XListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/xlv" > </com.bawei.jane.mxlistview.view.XListView> </RelativeLayout>
indexitem
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fae"> </RelativeLayout>
items
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 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_title"/> </RelativeLayout>