tablyout+多条目展示

来源:互联网 发布:软件开发培训技术学校 编辑:程序博客网 时间:2024/05/22 10:23

1.加入权限,导入xlistview

<uses-permission android:name="android.permission.INTERNET"></uses-permission><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
2.MainActivity类的实现,
   
 private XListView xlv;    boolean tag = true;    private MyAdapter adapter;    private List<DataBean.ListsBean> lists;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        xlv = (XListView) findViewById(R.id.xlv);        xlv.setPullLoadEnable(true);        xlv.setPullRefreshEnable(true);        xlv.setXListViewListener(this);        getData();    }    //网络判断    public boolean getData() {        ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);        NetworkInfo info = manager.getActiveNetworkInfo();        if (info != null && info.isAvailable()) {            MyTask task = new MyTask();            task.execute("");        } else {            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);            builder.setMessage("网络未开启,是否开启?");            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {                @Override                public void onClick(DialogInterface dialogInterface, int i) {                    Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);                    startActivity(intent);                    dialogInterface.dismiss();                }            });            builder.setNegativeButton("取消", null);            builder.show();        }        return false;    }    class MyTask extends AsyncTask<String, Integer, DataBean> {        @Override        protected DataBean doInBackground(String... strings) {            String json = getJson();            DataBean bean = new Gson().fromJson(json, DataBean.class);            return bean;        }        @Override        protected void onPostExecute(DataBean dataBean) {            super.onPostExecute(dataBean);            lists = dataBean.result.lists;            adapter = new MyAdapter(MainActivity.this);            xlv.setAdapter(adapter);            adapter.addData(lists);        }    }    @Override    public void onRefresh() {        adapter.updateData(lists);        xlv.stopLoadMore();        xlv.stopRefresh();    }    @Override    public void onLoadMore() {        adapter.addData(lists);        xlv.stopLoadMore();        xlv.stopRefresh();    }    public String getJson() {        try {            URL url = new URL("http://v.juhe.cn/movie/cinemas.movies?" + "cinemaid=" + URLEncoder.encode("2", "utf-8") + "&key=74b7931bc6bbdfcc32184cfd69c2ce7c");            HttpURLConnection connection = (HttpURLConnection) url.openConnection();            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {                InputStream inputStream = connection.getInputStream();                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));                StringBuilder builder = new StringBuilder();                String str;                while ((str = reader.readLine()) != null) {                    builder.append(str);                }                return builder.toString();            }        } catch (Exception e) {            e.printStackTrace();        }        return null;    }}
3,MyAdapter类
   
Context context;    List<DataBean.ListsBean> list = new ArrayList<>();    private final ImageLoader loader;    private final int banner_type = 0, item1_type = 1, item2_type = 2;    private DataBean.ListsBean bean;    private final DisplayImageOptions options;    public MyAdapter(Context context) {        this.context = context;        loader = ImageLoader.getInstance();        options = new DisplayImageOptions.Builder()                .cacheInMemory(true)                .cacheOnDisk(true)                .showImageOnLoading(R.drawable.ic_stub)                .showImageForEmptyUri(R.drawable.error)                .showImageOnFail(R.drawable.ic_error)                .build();    }    public void addData(List<DataBean.ListsBean> list) {        this.list.addAll(list);        notifyDataSetChanged();    }    public void updateData(List<DataBean.ListsBean> list) {        this.list.clear();        addData(list);    }    @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 int getItemViewType(int position) {        if (position == 0) {            return banner_type;        }        if (position % 2 == 1) {            return item1_type;        }        return item2_type;    }    @Override    public int getViewTypeCount() {        return 3;    }    @Override    public View getView(int i, View view, ViewGroup viewGroup) {        ViewHolder1 holder1 = null;        ViewHolder2 holder2 = null;        ViewHolder3 holder3 = null;        int type = getItemViewType(i);        if (view == null) {            switch (type) {                case banner_type:                    view = View.inflate(context, R.layout.banner_item, null);                    holder1 = new ViewHolder1(view);                    view.setTag(holder1);                    break;                case item1_type:                    view = View.inflate(context, R.layout.list_item, null);                    holder2 = new ViewHolder2(view);                    view.setTag(holder2);                    break;                case item2_type:                    view = View.inflate(context, R.layout.list_item1, null);                    holder3 = new ViewHolder3(view);                    view.setTag(holder3);                    break;                default:                    break;            }        } else {            switch (type) {                case banner_type:                    holder1 = (ViewHolder1) view.getTag();                    break;                case item1_type:                    holder2 = (ViewHolder2) view.getTag();                    break;                case item2_type:                    holder3 = (ViewHolder3) view.getTag();                    break;                default:                    break;            }        }        bean = list.get(i);        switch (type) {            case banner_type:                holder1.banner.setImageLoader(new com.youth.banner.loader.ImageLoader() {                    @Override                    public void displayImage(Context context, Object path, ImageView imageView) {                        DataBean.ListsBean listsBean = (DataBean.ListsBean) path;                        loader.displayImage(listsBean.pic_url, imageView, options);                    }                });                holder1.banner.setImages(list.subList(0, 6));                holder1.banner.start();                break;            case item1_type:                holder2.tv1.setText(bean.movieName);                loader.displayImage(bean.pic_url, holder2.iv1, options);                loader.displayImage(bean.pic_url, holder2.iv2, options);                break;            case item2_type:                holder3.tv1.setText(bean.movieName);                loader.displayImage(bean.pic_url, holder3.iv1, options);                loader.displayImage(bean.pic_url, holder3.iv2, options);                loader.displayImage(bean.pic_url, holder3.iv3, options);                loader.displayImage(bean.pic_url, holder3.iv4, options);                break;            default:                break;        }        return view;    }    class ViewHolder1 {        View rootView;        Banner banner;        public ViewHolder1(View rootView) {            this.rootView = rootView;            this.banner = (Banner) rootView.findViewById(R.id.banner);        }    }    class ViewHolder2 {        View rootView;        ImageView iv1, iv2;        TextView tv1;        public ViewHolder2(View rootView) {            this.rootView = rootView;            this.iv1 = (ImageView) rootView.findViewById(R.id.item1_iv1);            this.iv2 = (ImageView) rootView.findViewById(R.id.item1_iv2);            this.tv1 = (TextView) rootView.findViewById(R.id.item1_title);        }    }    class ViewHolder3 {        View rootView;        ImageView iv1, iv2, iv3, iv4;        TextView tv1;        public ViewHolder3(View rootView) {            this.rootView = rootView;            this.iv1 = (ImageView) rootView.findViewById(R.id.item2_iv1);            this.iv2 = (ImageView) rootView.findViewById(R.id.item2_iv2);            this.iv3 = (ImageView) rootView.findViewById(R.id.item2_iv3);            this.iv4 = (ImageView) rootView.findViewById(R.id.item2_iv4);            this.tv1 = (TextView) rootView.findViewById(R.id.item2_title);        }    }}
4.MyApplication 
extends Application 
 @Override    public void onCreate() {        super.onCreate();        ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(getApplicationContext());        ImageLoader.getInstance().init(configuration);    }}5.bean类,
public class DataBean {    ResultBean result;    class ResultBean {        List<ListsBean> lists;    }    class ListsBean {        String movieName, pic_url;    }}
布局的设置,

MyActivity布局·

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.monthb_demo.MainActivity">    <com.example.monthb_demo.xlist.XListView        android:id="@+id/xlv"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:divider="#ccc"        android:dividerHeight="2dp"></com.example.monthb_demo.xlist.XListView></RelativeLayout> banner_itemdebu布局,
<?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">    <com.youth.banner.Banner        android:id="@+id/banner"        android:layout_width="match_parent"        android:layout_height="250dp"></com.youth.banner.Banner></LinearLayout> list_item布局,
<?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:id="@+id/item1_title"        android:layout_width="match_parent"        android:layout_height="80dp"        android:gravity="center"        android:textColor="#f00"        android:textSize="20sp" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <ImageView            android:id="@+id/item1_iv1"            android:layout_width="0dp"            android:layout_height="150dp"            android:layout_margin="10dp"            android:layout_weight="1" />        <ImageView            android:id="@+id/item1_iv2"            android:layout_width="0dp"            android:layout_height="150dp"            android:layout_margin="10dp"            android:layout_weight="1" />    </LinearLayout></LinearLayout>
list_item1布局,
<?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:id="@+id/item2_title"        android:layout_width="match_parent"        android:layout_height="80dp"        android:gravity="center"        android:textColor="#f00"        android:textSize="20sp" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <ImageView            android:id="@+id/item2_iv1"            android:layout_width="0dp"            android:layout_height="150dp"            android:layout_margin="10dp"            android:layout_weight="1" />        <ImageView            android:id="@+id/item2_iv2"            android:layout_width="0dp"            android:layout_height="150dp"            android:layout_margin="10dp"            android:layout_weight="1" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <ImageView            android:id="@+id/item2_iv3"            android:layout_width="0dp"            android:layout_height="150dp"            android:layout_margin="10dp"            android:layout_weight="1" />        <ImageView            android:id="@+id/item2_iv4"            android:layout_width="0dp"            android:layout_height="150dp"            android:layout_margin="10dp"            android:layout_weight="1" />    </LinearLayout></LinearLayout>

原创粉丝点击