1
我見過很多例子中的選項卡式添加Web視圖activity.but它不符合我的need.What我想是如何在選項卡式活動android中添加web視圖?
主要Activity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_statement);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mSectionsPagerAdapter = new
SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
name = getIntent().getStringExtra("Username");
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) { super(fm) }
@Override
public Fragment getItem(int position) {
switch(position){
case 0 :
TabFragment1 tab1=new TabFragment1(name);
return tab1;
case 1:
TabFragment2 tab2 = new TabFragment2();
return tab2;
default:
return null;
} }
@Override
public int getCount() {
// Show 3 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
//new Daily().execute();
return "Daily";
case 1:
//new Week().execute();
return "Week"; }
return null;
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
this.finish();
return true; }
return super.onOptionsItemSelected(item);
}
TabFragment1.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroucontainer,
Bundle savedInstanceState) {
tab1 = inflater.inflate(R.layout.tabfragment1, container, false);
new Daily.execute();
return tab1;
}
class Daily extends AsyncTask<String,Void,String> {
String B = "";
@Override
protected String doInBackground(String... params) {
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(Sate_URL) //Setting the Root URL
.build();
DAPI api = adapter.create(DAPI.class);
api.in(
A,
B,
new Callback<Response>() {
@Override
public void success(Response result, Response response){
BufferedReader reader = null;
String output = "";
try {
//Initializing buffered reader
reader = new BufferedReader(new
InputStreamReader(result.getBody().in()))
output = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
web = (WebView)tab1.findViewById(R.id.statement1);
web.setWebViewClient(new WebViewClient(){
ProgressDialog prDialog;
@Override
public void onPageStarted(WebView view, String
url, Bitmap favicon) {
prDialog =
ProgressDialog.show(getActivity(), null, "loading, please wait...");
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String
url) {
prDialog.dismiss();
//
mySwipeRefreshLayout.setRefreshing(false);
super.onPageFinished(view, url);
}
});
web.loadUrl(URL);
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(getActivity(), error.toString(),
Toast.LENGTH_LONG).show(); }
});
return null;
}
}
我的tabfragment 2幾乎與tabfragment 1相同。我試圖做的是將兩個參數發送到服務器,並顯示html內容在webview.according參數html表中變化的兩個標籤layout.i沒有看到雙方都在進行操作任何建議或指導將不勝感激。提前感謝。
它工作? – A2N