//android
1.此模块,多条目点击子条目跳转
package jx.com.moth3.pdglfrag;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.List;
import jx.com.moth3.R;
import jx.com.moth3.activity.BannerActivity;
import jx.com.moth3.adapter.NewAdapter;
import jx.com.moth3.bean.NewBean;
import jx.com.moth3.util.NetUtil;
@SuppressLint("ValidFragment")
public class BFragment extends jx.com.moth3.base.BaseFragment {
private PullToRefreshListView listView;
private int page;
private final int COUNT = 30;
private NewAdapter adapter;
private List<NewBean.Data> data;
@Override
protected void initData() {
page = 1;
listView.setMode(PullToRefreshBase.Mode.BOTH);
adapter = new NewAdapter(getActivity());
listView.setAdapter(adapter);
listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
page = 1;
getinfo();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
getinfo();
}
});
getinfo();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(), BannerActivity.class);
//跳转的时候传过去一个值,请求的数据里的一个路径,到BannerActivity要在webview
上展示出来
intent.putExtra("uri",data.get(position).getUrl());
startActivity(intent);
}
});
}
private void getinfo(){
if(NetUtil.getInstance().isweb(getActivity())){
Toast.makeText(getActivity(),"无网络",Toast.LENGTH_SHORT).show();
listView.onRefreshComplete();
return;
}
NetUtil.getInstance().getload("http://www.xieast.com/api/news/news.php?type=top&page=1", NewBean.class, new NetUtil.CallBack<NewBean>() {
@Override
public void getload(NewBean o) {
if(o == null || !o.ismsg()){
Toast.makeText(getActivity(),"请求失败",Toast.LENGTH_SHORT).show();
listView.onRefreshComplete();
return;
}
data = o.getData();
if (page == 1) {
adapter.setMdata(o.getData());
}else {
adapter.addMdata(o.getData());
}
page++;
listView.onRefreshComplete();
if(o.getData().size()<=COUNT){
listView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
}else {
listView.setMode(PullToRefreshBase.Mode.BOTH);
}
}
});
}
@Override
protected void initView(View view) {
listView = view.findViewById(R.id.pull_list);
}
@Override
protected int getViewById() {
return R.layout.bfragment;
}
}
//adapter
package jx.com.moth3.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
import jx.com.moth3.R;
import jx.com.moth3.bean.NewBean;
public class NewAdapter extends BaseAdapter {
public List<NewBean.Data> mdata;
public Context context;
public NewAdapter(Context context) {
this.context = context;
mdata = new ArrayList<>();
}
public void setMdata(List<NewBean.Data> data) {
mdata.clear();
if(data != null){
mdata.addAll(data);
}
notifyDataSetChanged();
}
public void addMdata(List<NewBean.Data> data) {
if(data != null){
mdata.addAll(data);
}
notifyDataSetChanged();
}
@Override
public int getCount() {
return mdata.size();
}
private final int COUNT = 2;
private final int NEW_TYPE = 0;
private final int IMAGE_TYPE = 1;
@Override
public int getItemViewType(int position) {
//isimage是bean类里的一个方法
return getItem(position).isimage()?NEW_TYPE:IMAGE_TYPE;
}
@Override
public int getViewTypeCount() {
return COUNT;
}
@Override
public NewBean.Data getItem(int position) {
return mdata.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(getItemViewType(position) == NEW_TYPE? R.layout.itemone:R.layout.itemtwo,parent,false);
holder = new ViewHolder(convertView);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.binddata(getItem(position));
return convertView;
}
class ViewHolder{
TextView text1,text2;
ImageView image1,image2,image3;
private ViewHolder(View con){
text1 = con.findViewById(R.id.text1);
text2 = con.findViewById(R.id.text2);
image1 = con.findViewById(R.id.image1);
image2 = con.findViewById(R.id.image2);
image3 = con.findViewById(R.id.image3);
con.setTag(this);
}
private void binddata(NewBean.Data data){
text1.setText(data.getTitle());
text2.setText(data.getDate());
ImageLoader.getInstance().displayImage(data.getThumbnail_pic_s(),image1);
if(image2 != null && image3 != null){
ImageLoader.getInstance().displayImage(data.getThumbnail_pic_s02(),image2);
ImageLoader.getInstance().displayImage(data.getThumbnail_pic_s03(),image3);
}
}
}
}
//bean
package jx.com.moth3.bean;
import java.util.List;
public class NewBean {
public List<Data> data;
public String msg;
public boolean ismsg(){
return msg.equals("成功的返回");
}
public List<Data> getData() {
return data;
}
public class Data{
public String title;
public String date;
public String thumbnail_pic_s;
public String thumbnail_pic_s02;
public String thumbnail_pic_s03;
public String url;
public String getUrl() {
return url;
}
public String getTitle() {
return title;
}
public String getDate() {
return date;
}
public String getThumbnail_pic_s() {
return thumbnail_pic_s;
}
public String getThumbnail_pic_s02() {
return thumbnail_pic_s02;
}
public String getThumbnail_pic_s03() {
return thumbnail_pic_s03;
}
public boolean isimage(){
return thumbnail_pic_s02 == null && thumbnail_pic_s03 == null;
}
}
}
///点击子条目的页面
package jx.com.moth3.activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoaderInterface;
import java.util.ArrayList;
import java.util.List;
import jx.com.moth3.R;
import jx.com.moth3.base.BannerBean;
import jx.com.moth3.base.BaseActivity;
import jx.com.moth3.bean.NewBean;
import jx.com.moth3.util.NetUtil;
public class BannerActivity extends BaseActivity {
private Banner banner;
private WebView webView;
@Override
protected void initData() {
banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
banner.setImageLoader(new ImageLoaderInterface<ImageView>() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
BannerBean.Data bean = (BannerBean.Data) path;
ImageLoader.getInstance().displayImage(bean.getImg(),imageView);
}
@Override
public ImageView createImageView(Context context) {
ImageView imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
return imageView;
}
});
getdata();
//得到传来的值,放到webview展示出来
Intent intent = getIntent();
String uri = intent.getStringExtra("uri");
webView.loadUrl(uri);
}
private void getdata() {
NetUtil.getInstance().getload("http://www.xieast.com/api/banner.php", BannerBean.class, new NetUtil.CallBack<BannerBean>() {
@Override
public void getload(BannerBean o) {
banner.setImages(o.getData());
banner.setBannerTitles(gettitleaa(o.getData()));
banner.setDelayTime(2000);
banner.start();
}
private List<String> gettitleaa(List<BannerBean.Data> data) {
List<String> list = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
list.add(data.get(i).getTitle());
}
return list;
}
});
}
@Override
protected void initView(Bundle savedInstanceState) {
banner = findViewById(R.id.mybanner);
webView = findViewById(R.id.mywebview);
}
@Override
protected int getViewById() {
return R.layout.activity_banner;
}
}
//上面模块的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".activity.BannerActivity">
<com.youth.banner.Banner
android:id="@+id/mybanner"
android:layout_width="0dp"
android:layout_height="200dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
></com.youth.banner.Banner>
<WebView
android:id="@+id/mywebview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/mybanner"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
></WebView>
</android.support.constraint.ConstraintLayout>