如何在选项卡视图布局中添加滑动向下刷新功能

时间:2017-07-10 06:20:41

标签: android

这是我的标签视图布局的主要活动

package com.nitol.aust.cse.austclassmanager;


public class ResultCheckActivity extends AppCompatActivity implements 
NavigationView.OnNavigationItemSelectedListener{

private TabLayout t1;
private ViewPager vp1;
Toolbar toolbar;
DrawerLayout drawerLayout;
NavigationView navigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.navi_draw_result);

    t1 = (TabLayout) findViewById(R.id.tab1);
    vp1 = (ViewPager) findViewById(R.id.ViewPager1);


    setUpMyViewPager(vp1);
    t1.setupWithViewPager(vp1);


    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout) ;
    navigationView = (NavigationView) findViewById(R.id.navigation_view);
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle
            (this,drawerLayout,toolbar, R.string.open_drawer, R.string.close_drawer);
    drawerLayout.setDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();
    navigationView.setNavigationItemSelectedListener(this);
    navigationView.getMenu().getItem(5).setChecked(true);



}

void setUpMyViewPager(ViewPager vp){

    ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
    viewPagerAdapter.addMyFragment(new WebViewEvents(),"News & Events");
    viewPagerAdapter.addMyFragment(new WebViewAcademic(),"Academic Calendar");
    viewPagerAdapter.addMyFragment(new WebViewTheory(),"Theory Results");
    viewPagerAdapter.addMyFragment(new WebViewLab(),"Lab Results");
    viewPagerAdapter.addMyFragment(new WebViewTeacher(),"Faculty Members");


    vp.setAdapter(viewPagerAdapter);
}



class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> my_list = new ArrayList<Fragment>();
    private final List<String> my_title = new ArrayList<String>();

    public ViewPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public Fragment getItem(int position) {
        return my_list.get(position);
    }

    @Override
    public int getCount() {
        return my_list.size();
    }


    void addMyFragment(Fragment f, String title){
        my_list.add(f);
        my_title.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return my_title.get(position);
    }

}




@Override
public void onBackPressed() {

    if(drawerLayout.isDrawerOpen(GravityCompat.START)){
        drawerLayout.closeDrawer(GravityCompat.START);
    }

    else{
        super.onBackPressed();
    }

}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

    int id = item.getItemId();

    switch (id){

        case R.id.home:
            Intent intent0 = new Intent(ResultCheckActivity.this, MenuActivity.class);
            startActivity(intent0);
            finish();

            break;

        case R.id.routine:
            Intent intent = new Intent(ResultCheckActivity.this, ClassRoutineActivity.class);
            startActivity(intent);
            finish();

            break;

        case R.id.cgpa:
            Intent intent2 = new Intent(ResultCheckActivity.this, CgpaCalculator.class);
            startActivity(intent2);
            finish();

            break;

        case R.id.details:
            Toast.makeText(getApplicationContext(),"Class Details",Toast.LENGTH_SHORT).show();

            break;

        case R.id.quiz:
            Toast.makeText(getApplicationContext(),"Quiz Reminder",Toast.LENGTH_SHORT).show();

            break;

        case R.id.result:

            break;


        case R.id.profile:
            Intent intent3 = new Intent(ResultCheckActivity.this, ProfileActivity.class);
            startActivity(intent3);
            finish();

            break;

        case R.id.about:
            Toast.makeText(getApplicationContext(),"About",Toast.LENGTH_SHORT).show();

            break;

        case R.id.settings:
            Intent intent4 = new Intent(ResultCheckActivity.this, Settings.class);
            startActivity(intent4);
            finish();

            break;

    }

    drawerLayout.closeDrawer(GravityCompat.START);


    return true;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_tool,menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if(id == R.id.tool_settings){
        Intent intent = new Intent(ResultCheckActivity.this, Settings.class);
        startActivity(intent);

    }
    else if(id == R.id.tool_about){

        Toast.makeText(getApplicationContext(),"This is About !", Toast.LENGTH_SHORT).show();

    }

    return super.onOptionsItemSelected(item);
}
}

这是我想要添加刷新功能

的标签
void setUpMyViewPager(ViewPager vp){

ViewPagerAdapter viewPagerAdapter = new 
ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addMyFragment(new WebViewEvents(),"News & Events");
viewPagerAdapter.addMyFragment(new WebViewAcademic(),"Academic Calendar");
viewPagerAdapter.addMyFragment(new WebViewTheory(),"Theory Results");
viewPagerAdapter.addMyFragment(new WebViewLab(),"Lab Results");
viewPagerAdapter.addMyFragment(new WebViewTeacher(),"Faculty Members");


vp.setAdapter(viewPagerAdapter);
}

现在假设我想在“WebViewEvents”类上添加刷新。课程如下:

package com.nitol.aust.cse.austclassmanager;

public class WebViewEvents  extends Fragment {

View v;
WebView wb4;

private String currentUrl = "http://aust.edu/news_events.htm";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    v = inflater.inflate(R.layout.webview_event, container,false);

    wb4 = (WebView) v.findViewById(R.id.webView_event);

    wb4.setInitialScale(1);
    wb4.getSettings().setJavaScriptEnabled(true);
    wb4.getSettings().setLoadWithOverviewMode(true);
    wb4.getSettings().setUseWideViewPort(true);
    wb4.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    wb4.setScrollbarFadingEnabled(false);
    wb4.loadUrl(currentUrl);
    wb4.setWebViewClient(new WebViewClient());
    wb4.getSettings().setBuiltInZoomControls(true);
    wb4.getSettings().setUseWideViewPort(true);
    wb4.getSettings().setLoadWithOverviewMode(true);
    wb4.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);


    return v;

}

}

我在这里找到了一些说明,但最多的是说我必须使用@Override onRefresh()方法...但我将我的类声明为非活动类。所以在非活动类中,我无法覆盖方法。这就是为什么我找不到我的解决方案......

2 个答案:

答案 0 :(得分:1)

您只需阅读docs on SwipeRefreshLayout

即可

首先,将SwipeRefreshLayout添加到您的活动布局:

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swiperefresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    // here is your content

</android.support.v4.widget.SwipeRefreshLayout>

然后在您的活动中找到刷卡刷新布局的实例:

mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefresh);

设置刷新侦听器,在其中触发更新方法

/*
 * Sets up a SwipeRefreshLayout.OnRefreshListener that is invoked when the user
 * performs a swipe-to-refresh gesture.
 */
mSwipeRefreshLayout.setOnRefreshListener(
    new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout");

            // This method performs the actual data-refresh operation.
            // The method calls setRefreshing(false) when it's finished.
            myUpdateOperation();
        }
    }
);

有关更多信息,请阅读文档,在答案开头链接。

您也可以查看Google示例:

您可以找到的所有Google样本列表here

希望有所帮助

答案 1 :(得分:0)

最后这对我有用

package com.nitol.aust.cse.austclassmanager;
public class WebViewEvents  extends Fragment{

View v;
WebView wb4;
SwipeRefreshLayout mySwipeRefreshLayout;

private String currentUrl = "http://aust.edu/news_events.htm";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    v = inflater.inflate(R.layout.webview_event, container,false);

    mySwipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swiperefresh);
    wb4 = (WebView) v.findViewById(R.id.webView_event);

    wb4.setInitialScale(1);
    wb4.getSettings().setJavaScriptEnabled(true);
    wb4.getSettings().setLoadWithOverviewMode(true);
    wb4.getSettings().setUseWideViewPort(true);
    wb4.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    wb4.setScrollbarFadingEnabled(false);
    wb4.loadUrl(currentUrl);
    wb4.setWebViewClient(new WebViewClient());
    wb4.getSettings().setBuiltInZoomControls(true);
    wb4.getSettings().setUseWideViewPort(true);
    wb4.getSettings().setLoadWithOverviewMode(true);
    wb4.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    wb4.setWebViewClient(new MyWebViewClient());

    mySwipeRefreshLayout.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    wb4.loadUrl(currentUrl);
                }
            }
    );


    return v;

}

public class MyWebViewClient extends WebViewClient{

    @Override
    public void onPageFinished(WebView view, String url) {
        mySwipeRefreshLayout.setRefreshing(false);
        currentUrl = url;
        super.onPageFinished(view, url);
    }
}

}

我在班级

中添加了这种覆盖方法
mySwipeRefreshLayout.setOnRefreshListener(
        new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                wb4.loadUrl(currentUrl);
            }
        }
);

然后添加此项以停止刷新动画

public class MyWebViewClient extends WebViewClient{

@Override
public void onPageFinished(WebView view, String url) {
    mySwipeRefreshLayout.setRefreshing(false);
    currentUrl = url;
    super.onPageFinished(view, url);
}

} 不要忘记添加上次停止动画的实例

wb4.setWebViewClient(new MyWebViewClient());