xml页面使用
<WebView
android:id="@+id/Web"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
在mainActivity中长按xlistview显示webview,(通过跳转传值的)
public class MainActivity extends AppCompatActivity {
private List<MyShuJu.NewslistBean> data = new ArrayList<>();
private List<String> list = new ArrayList<>();
private XListView xlistview;
private ViewPager viewPager;
private String part= "http://api.tianapi.com/tiyu/?key=36bafe109f1b68cc2c3829db69c7c180&num=";
private int index = 3;
private String url=part+index;
private int type=1;
private DisplayImageOptions option;
private Myadapter myadapter;
@SuppressLint("HandlerLeak")
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what == 0){
int currentItem = viewPager.getCurrentItem();
viewPager.setCurrentItem(currentItem+1);
handler.sendEmptyMessageDelayed(0,2000);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化图片
ImageLoaderutils.initImageLoader(MainActivity.this);
option = ImageLoaderutils.getImageOption(MainActivity.this);
//找控件
xlistview = findViewById(R.id.xlistview);
viewPager = findViewById(R.id.viewpager);
//viewpager
Newviewpager();
//xlistview
NewXlistview();
//xlistview获取参数与实例化适配器
requestData();
}
//xlistview相关操作
private void NewXlistview() {
xlistview.setPullRefreshEnable(true);
xlistview.setPullLoadEnable(true);
xlistview.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
index=3;
url=part+index;
type=1;
requestData();
}
@Override
public void onLoadMore() {
index++;
url=part+index;
type=2;
requestData();
}
});
//XListView的长摁事件
xlistview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("url",data.get(position-1).getUrl());
startActivity(intent);
return true;
}
});
}
//xlistview获取参数与实例化适配器
private void requestData() {
MyNetTask task = new MyNetTask(new MyNetTask.IcallBack() {
@Override
public void getJsonData(String jsonStr) {
Gson gson = new Gson();
MyShuJu myShuJu = gson.fromJson(jsonStr, MyShuJu.class);
if (type==1)
{
data.clear();
}
data.addAll(myShuJu.getNewslist());
if (myadapter==null)
{
myadapter = new Myadapter();
xlistview.setAdapter(myadapter);
}else {
myadapter.notifyDataSetChanged();
}
if (type==1)
{
xlistview.stopRefresh();
}else {
xlistview.stopLoadMore();
}
}
});
task.execute(url);
}
//viewpager相关操作
private void Newviewpager() {
MyNetTask task = new MyNetTask(new MyNetTask.IcallBack() {
@Override
public void getJsonData(String jsonStr) {
Gson gson = new Gson();
MypagerBean myShuJu = gson.fromJson(jsonStr, MypagerBean.class);
List<MypagerBean.NewslistBean> newslist = myShuJu.getNewslist();
for (int i = 0 ;i<newslist.size();i++){
String picUrl = newslist.get(i).getPicUrl();
list.add(picUrl);
}
viewPager.setAdapter(new MyPagerAdapter());
viewPager.setCurrentItem(list.size()*10000);
handler.sendEmptyMessageDelayed(0,2000);
}
});
task.execute("https://api.tianapi.com/wxnew/?key=48a7d7193e11bd2dd4a683b6e2f90a4f&num=10&page=1");
}
//ViewPager的适配器
class MyPagerAdapter extends PagerAdapter{
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(MainActivity.this);
ImageLoader.getInstance().displayImage(list.get(position%list.size()),imageView,option);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
}
//xlistview的适配器
class Myadapter extends BaseAdapter{
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if(position%2==0){
return 0;
}else{
return 1;
}
}
@Override
public int getCount() {
return data.size();
}
@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) {
int itemViewType = getItemViewType(i);
if(itemViewType == 0){
ViewHolder0 holder0;
if(view == null){
view = View.inflate(MainActivity.this,R.layout.listview0,null);
holder0= new ViewHolder0();
holder0.tu = view.findViewById(R.id.tu_list0);
holder0.name = view.findViewById(R.id.name_list0);
view.setTag(holder0);
}else{
holder0 = (ViewHolder0) view.getTag();
}
ImageLoader.getInstance().displayImage(data.get(i).getPicUrl(),holder0.tu,option);
holder0.name.setText(data.get(i).getTitle());
}else{
ViewHolder1 holder1;
if(view == null){
view = View.inflate(MainActivity.this,R.layout.listview1,null);
holder1= new ViewHolder1();
holder1.name = view.findViewById(R.id.name_list1);
view.setTag(holder1);
}else{
holder1 = (ViewHolder1) view.getTag();
}
holder1.name.setText(data.get(i).getTitle());
}
return view;
}
class ViewHolder0{
ImageView tu;
TextView name;
}
class ViewHolder1{
TextView name;
}
}
接收到传的URl
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
WebView web = findViewById(R.id.Web);
Intent intent = getIntent();
String url = intent.getStringExtra("url");
//设置WebView属性,能够执行Javascript脚本
web.getSettings().setJavaScriptEnabled(true);
//加载需要显示的网页
web.loadUrl(url);
web.setWebViewClient(new HelloWebViewClient ());
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}