实施SwipeView,SwipeRefreshlayout和抽屉式导航于一体的活动 [英] Implementing SwipeView, SwipeRefreshlayout, and Navigation Drawer in one activity

查看:219
本文介绍了实施SwipeView,SwipeRefreshlayout和抽屉式导航于一体的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所暗示的,我想实现所有这些功能一次。本来我有一个全功能的侧导航与持有列表视图(使用自定义列表适配器)一个SwipeRefreshLayout。然后我加了一个ViewPager的SwipeRefreshLayout内,一切大多作品...只是因为我刷卡列表视图不会出现在新的标签有时。大部分时间我看到的第一页和列表视图的时候,我向右滑动,什么都没有,我向右滑动,什么都没有,我刷的离开,列表视图,我要刷一开始,什么都没有。

as the title suggests, I'm trying to implement all of these features at once. Originally I had a fully functioning side-nav with a SwipeRefreshLayout which holds a list view (using a custom list adapter). I then added a ViewPager inside of the SwipeRefreshLayout, and everything mostly works…except that as I swipe the list view does not appear in the new 'tabs' sometimes. Most of the time I see the first page and list view, I swipe right, nothing, I swipe right, nothing, I swipe left, list view, I swipe to the beginning, nothing.

我要补充一点,一切都是完全动态的,导航项目从我的服务器接收(这也是页数),每个页面都有不同的列表项自定义列表适配器。所有这些动态信息似乎收到每页刷卡等。

I should add that everything is entirely dynamic, the navigation items are received from my server (which is also the number of pages) and each page has a custom list adapter with different list items. All of this dynamic information seems to be received and adapted correctly per page swipe etc.

现在为code!

抽屉布局嵌入式swiperefreshlayout和查看传呼机。 drawer.xml

Drawer layout with swiperefreshlayout and view pager embedded. "drawer.xml"

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"> 

    <android.support.v4.widget.SwipeRefreshLayout       xmlns:android="http://schemas.android.com/apk/res/android"    
   android:id="@+id/swipe_container"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</android.support.v4.widget.SwipeRefreshLayout>
    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
     <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        />


    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         The drawer is given a fixed width in dp and extends the full height of
         the container. A solid background is used for contrast
         with the content view. -->
     <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@color/grey_lighter"
        android:dividerHeight="1dp"
        android:background="#FFFFFF"/>
</android.support.v4.widget.DrawerLayout>

列表视图其持有的活动dashboard.xml动态创建的自定义列表项:

Listview which holds the custom list items created dynamically in the activity "dashboard.xml":

<?xml version="1.0" encoding="utf-8"?>


    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LV_dashboard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1" >

    </ListView>

现在的片段活动:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this.getApplicationContext();
        navItems = new ArrayList<String>();
        this.setContentView(R.layout.drawer); 
        mMerchantIds = Session.get().getMerchID(); //array retrieved from cache used for api

        mCustomerPagerAdapter = new CustomerPagerAdapter(getSupportFragmentManager());


        // Set up the ViewPager, attaching the adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mCustomerPagerAdapter);

        mTitle = mDrawerTitle = getTitle();

        mNavTitles = getResources().getStringArray(R.array.nav_array); //I realize this isn't dynamic, I haven't gotten around to that just yet.

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        connection = new WiselyRequest();

        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        // set up the drawer's list view with items and click listener
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.nav_item, mNavTitles));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
                ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }    
    }

此分支为两个部分,我们将与抽屉启动:

This branches off into two parts, we'll start with the Drawer:

private class DrawerItemClickListener implements ListView.OnItemClickListener {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectItem(position);
            }
        }

        private void selectItem(int position) {
            // update the main content by replacing fragments (this is not what the pager does)
            Fragment fragment = null;

            fragment = new CustomerFragment();
            Bundle args = new Bundle();
            args.putString(KEY_MERCHANT_ID, (mMerchantIds.get(position)));
            fragment.setArguments(args);
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();


            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);

            setTitle(mNavTitles[position]);



            mDrawerLayout.closeDrawer(mDrawerList);

        }

下面认为寻呼机:

public class CustomerPagerAdapter extends FragmentPagerAdapter {

            public CustomerPagerAdapter(FragmentManager fm) {
                super(fm);
            }

            @Override
            public Fragment getItem(int i) {
                Fragment fragment = new CustomerFragment();
                Bundle args = new Bundle();
                Log.d("Wisely", "merchants: "+mMerchantIds.get(i));
                args.putString(KEY_MERCHANT_ID, (mMerchantIds.get(i))); // Our object is just an integer :-P
                fragment.setArguments(args);
                return fragment;
            }

            @Override
            public int getCount() {
                // For this contrived example, we have a 100-object collection.
                return mMerchantIds.size(); //equal to the number of merchantss (that many customer objects) 
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return "OBJECT " + (position + 1);
            }
        }

下面是两个抽屉和寻呼机都在刷新利用和凡客片段处理:

Here is the customer fragment that both the drawer and pager are utilizing and where on refresh is handled:

 public class CustomerFragment extends Fragment implements OnRefreshListener {
                private View rootView;
                SwipeRefreshLayout swipeLayout; 
                public CustomerFragment() {

                }

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                    Bundle args = getArguments();
                    String merchantID = args.getString(KEY_MERCHANT_ID);
                    if(connection.checkConnectivity(getApplicationContext())){
                        getRecentCustomers(API.getRecentCustomers()+"?merchant_id="+merchantID); //api call works totally fine, and correctly sets up the adapter
                    }
                    else
                        Toast.makeText(DashboardActivity.this, "Need to be connected to the internet!", 4000).show();
                    rootView = inflater.inflate(R.layout.dashboard, container, false);//inflates the dashboard
                    swipeLayout = (SwipeRefreshLayout)findViewById(R.id.swipe_container);
                    swipeLayout.setOnRefreshListener(this);
                     swipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light);
                    registerClickCallback(rootView);

                    return rootView;    
                }

                @Override
                public void onRefresh() {
                    if(connection.checkConnectivity(getApplicationContext()))
                        reloadActivity(); //literally turns the activity on and off without animation
                    else
                        Toast.makeText(DashboardActivity.this, "Need to be connected to the internet!", 4000).show();
                }

我相信这个问题退出在我的布局,不在列表中的适配器或客户的片段,这似乎是工作的罚款(我每次刷到一个新的页面调用API和正确的数据时,放入列表视图。我无法看到它。我也意识到navdrawer不是动态的,但我目前更大的问题是能够通过网页上的多个列表视图刷卡。当我改变我的customerFragment膨胀用一个简单的文本视图在这数字,它似乎很好地工作,除了第一页永远不会消失,其他页面只是堆积在它上面的(我认为这与在抽屉里的FrameLayout做,但去除它打破了code,因为我唠叨抽屉依赖于它)。有什么建议?

I believe the issue exits in my layouts, not in the list adapter or customer fragment which seem to be working fine (every time I swipe to a new page the api is called and the correct data is put into the list view. I just can't see it. Also I realize that the navdrawer is not dynamic, but my bigger issue at the moment is being able to swipe through multiple list views on pages. When I change my customerFragment to inflate a simple text view with a number in it, it seems to work fine, except that the first page never goes away, other pages just pile on top of it (I think this has to do with the framelayout in the drawer but removing it breaks the code because my nag drawer relies on it). Any suggestions?

推荐答案

我想,你必须创建扩展DrawerLayout或ViewPager自己的类。
里面你的类,你必须覆盖的方法:

I think, you will have to create your own classes extending DrawerLayout or ViewPager. Inside of your classes you have to Override methods:

onInterceptTouchEvent() - 在这里你评估的TouchEvent并返回true,如果拦截它(不传递给孩子视图)

onInterceptTouchEvent() - here you evaluate TouchEvent and return true, if you are intercepting it (not passing to the child View)

您必须调整基于你想达到什么拦截的条件。

You have to tweak the conditions of intercepting based on what you are trying to achieve.

指南是在这里:
http://developer.android.com/training/gestures/viewgroup.html

这篇关于实施SwipeView,SwipeRefreshlayout和抽屉式导航于一体的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆