如何在选项卡式活动android中添加Web视图?

时间:2017-09-01 05:16:16

标签: android android-tablayout android-tabs android-tabbed-activity

我已经看到很多在Tabbed活动中添加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相同。我想要做的是向服务器发送两个参数并在webview中显示html内容。根据参数html表的不同,两个标签布局都有所不同。我看不到在两个布局中操作。任何建议或指南都将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

你需要在每个标签选择中进行webservice调用还是什么?它不清楚。现在的问题是什么?