如何使用ChrisBanes的ActionBar-PullToRefresh库以及活动底部的Tab视图

时间:2013-12-25 05:43:16

标签: java android tabs pull-to-refresh

我正在尝试使用Chris Banes的库Actionbar-PullToRefresh。它可以在这里找到。

我在我的应用中使用屏幕底部的标签。正如你在屏幕截图中看到的那样。 我仔细阅读了示例代码。他说你需要做的就是将你的可刷新视图包装在PullToRefreshLayout中,如下所示:我如何将它集成到我的应用程序中。

public class MainActivity extends Activity  implements OnTabChangeListener {

    private TabHost mTabHost;
    public static final String TAB_1 = "Open";
    public static final String TAB_2 = "Approved";
    public static final String TAB_3 = "Decline";
    private int mCurrentTab;
    public String result;
    Vector<String> msgvector = new Vector<String>();
    Vector<String> usernamevector = new Vector<String>();
    Vector<String> feedIdvector = new Vector<String>();
    Vector<String> userIdvector = new Vector<String>();

    ListView listview,listview1,listview2;
    private ProgressDialog simpleWaitDialog;
    List<Rowfeeds> rowfeedlist;
    public String result2;
    public String result3;

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


        mTabHost = (TabHost) findViewById(android.R.id.tabhost);

        listview = (ListView)findViewById(R.id.tab_1);
        listview1 = (ListView)findViewById(R.id.tab_2);
        listview2 = (ListView)findViewById(R.id.tab_3);

        new Feedsdata().execute();

        setupTabs();

        mTabHost.setOnTabChangedListener(this);

        mTabHost.setCurrentTab(mCurrentTab);




    }


    private class Feedsdata extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub

            try {
                Log.i("doInBackground", "doInBackground");
                HttpClient hc = new DefaultHttpClient();

                HttpPost postMethod = new HttpPost("http://192.168.21.74/mynetwork/formapi/top_feeds");

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

                nameValuePairs.add(new BasicNameValuePair("from", "0"));
                nameValuePairs.add(new BasicNameValuePair("to", "10"));

                postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));


                HttpResponse rp = hc.execute(postMethod);

                if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    result = EntityUtils.toString(rp.getEntity());
                    Log.v("suggestion_data", "" + result);

                    JSONArray jsonary = new JSONArray(result);

                    for (int i = 0; i < jsonary.length(); i++) {

                        //Log.v("jsonary.length", "" +jsonary.length());

                        String sr = ""+jsonary.getJSONObject(i);
                        //Log.v("sr", "" +sr);

                        JSONObject jobj = new JSONObject(sr);
                        Log.v("======================", "=================" );
                        Log.v("feed_id", "" +jobj.getString("feed_id"));
                        Log.v("feed_message", "" +jobj.getString("feed_message"));
                        Log.v("user_id", "" +jobj.getString("user_id"));
                        Log.v("username", "" +jobj.getString("username"));
                        Log.v("======================", "=================" );

                        msgvector.add(""+jobj.getString("feed_message").toString().trim());
                        usernamevector.add("" +jobj.getString("username").toString().trim());
                        feedIdvector.add("" +jobj.getString("feed_id").toString().trim());
                        userIdvector.add("" +jobj.getString("user_id").toString().trim());

                    }

                    //for (int i = 0; i < feedIdvector.size(); i++) {
                        //Log.v("feedIdvector", "" +feedIdvector.elementAt(i));

                    //}


                }else{

                    Log.d("JSON", "StatusCode "+rp.getStatusLine().getStatusCode());
                }

            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.i("AsyncTaskException", e.toString());
            }

            Log.i("popularAsyncTaskException", e.toString());
            }
            return null;
        }

        @Override
        protected void onPreExecute() {
            Log.i("Async-Example", "onPreExecute Called");
            simpleWaitDialog = ProgressDialog.show(MainActivity.this,"Wait", "Downloading feeds");

        }

        @Override
        protected void onPostExecute(String result) {

            rowfeedlist = new ArrayList<Rowfeeds>();

            for (int i = 0; i < msgvector.size(); i++) {

                Rowfeeds rowfeeds = new Rowfeeds(msgvector.elementAt(i),usernamevector.elementAt(i),feedIdvector.elementAt(i));
                rowfeedlist.add(rowfeeds);

            }

            Customfeedadapter feedadapter = new Customfeedadapter(getApplicationContext(),R.layout.feeds,rowfeedlist
                    ,feedIdvector,userIdvector);
            listview.setAdapter(feedadapter);
            simpleWaitDialog.dismiss();





    }



    private void setupTabs() {

        mTabHost.setup(); // you must call this before adding your tabs!
        mTabHost.addTab(newTab(TAB_1, R.id.tab_1));
        mTabHost.addTab(newTab(TAB_2, R.id.tab_2));
        mTabHost.addTab(newTab(TAB_3, R.id.tab_3));

    }


    private TabSpec newTab(String tag, int tabContentId) {

        Log.i("tag", tag);//fff498
        Log.i("tabContentId", ""+tabContentId);//fff498

         TabSpec spec = mTabHost.newTabSpec(tag);
         spec.setContent(tabContentId);

        if(tag.equalsIgnoreCase("Open")){
             spec.setIndicator("", getResources().getDrawable(R.drawable.feed));
             spec.setContent(tabContentId);

        }else if(tag.equalsIgnoreCase("Approved")){
            spec.setIndicator("", getResources().getDrawable(R.drawable.trend));
            spec.setContent(tabContentId);

        }else if(tag.equalsIgnoreCase("Decline")){
            spec.setIndicator("", getResources().getDrawable(R.drawable.popularr));
            spec.setContent(tabContentId);

        }

        for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
        {
            mTabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#fff498"));

        }


        return spec;
    }

    public void onTabChanged(String tabId) {

        for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
        {
            mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.WHITE);
        }


        if (TAB_1.equals(tabId)) {
           // updateTab(R.id.tab_1);
            mCurrentTab = 0;
            //Toast.makeText(getApplicationContext(), "1No Guest list for Today", Toast.LENGTH_SHORT).show();
            mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#fff498"));



        }
        if (TAB_2.equals(tabId)) {
            //updateTab(R.id.tab_2);
             mCurrentTab = 1;
             //Toast.makeText(getApplicationContext(), "2No Guest list for Today", Toast.LENGTH_SHORT).show();
             mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#fff498"));
            //http://192.168.21.74/mynetwork/formapi/trending
            //http://192.168.21.74/mynetwork/formapi/popular


        }
        if (TAB_3.equals(tabId)) {
            //updateTab(R.id.tab_2);
             mCurrentTab = 2;
            // Toast.makeText(getApplicationContext(), "3No Guest list for Today", Toast.LENGTH_SHORT).show();
             mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#fff498"));
        }
    }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Xml布局:

<?xml version="1.0" encoding="utf-8"?>
<TabHost  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" >

         <ListView
        android:id="@+id/tab_1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />


          <ListView
        android:id="@+id/tab_2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         />

           <ListView
        android:id="@+id/tab_3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         />

    </FrameLayout>

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:layout_weight="0" >
    </TabWidget>
  </LinearLayout>

 </TabHost>

enter image description here

1 个答案:

答案 0 :(得分:1)

将所有列表视图放入PullToRefreshLayout,并将R.id.tab_1 R.id.tab_2 R.id.tab_3个ID提供给PullToRefreshLayout而不是ListView

<uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   <ListView
        android:id="@+id/listview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
</uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout>
对于listview的其余部分,

相同

在java里面的onCreate活动:

    mTabHost = (TabHost) findViewById(android.R.id.tabhost);

    mPullToRefreshLayout= (ListView)findViewById(R.id.tab_1);
    mPullToRefreshLayout1 = (ListView)findViewById(R.id.tab_2);
    mPullToRefreshLayout2 = (ListView)findViewById(R.id.tab_3);

    listview = (ListView)findViewById(R.id.listview1);
    listview1 = (ListView)findViewById(R.id.listview2);
    listview2 = (ListView)findViewById(R.id.listview3);

    ActionBarPullToRefresh.from(this)
        // Mark All Children as pullable
        .allChildrenArePullable()
        // Set the OnRefreshListener
        .listener(this)
        // Finally commit the setup to our PullToRefreshLayout
        .setup(mPullToRefreshLayout);

关于标签更改:

public void onTabChanged(String tabId) {

    for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
    {
        mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.WHITE);
    }


    if (TAB_1.equals(tabId)) {
       // updateTab(R.id.tab_1);
        mCurrentTab = 0;
        //Toast.makeText(getApplicationContext(), "1No Guest list for Today", Toast.LENGTH_SHORT).show();
        mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#fff498"));

ActionBarPullToRefresh.from(this)
        // Mark All Children as pullable
        .allChildrenArePullable()
        // Set the OnRefreshListener
        .listener(this)
        // Finally commit the setup to our PullToRefreshLayout
        .setup(mPullToRefreshLayout);


    }
    if (TAB_2.equals(tabId)) {
        //updateTab(R.id.tab_2);
         mCurrentTab = 1;
         //Toast.makeText(getApplicationContext(), "2No Guest list for Today", Toast.LENGTH_SHORT).show();
         mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#fff498"));
        //http://192.168.21.74/mynetwork/formapi/trending
        //http://192.168.21.74/mynetwork/formapi/popular

ActionBarPullToRefresh.from(this)
        // Mark All Children as pullable
        .allChildrenArePullable()
        // Set the OnRefreshListener
        .listener(this)
        // Finally commit the setup to our PullToRefreshLayout
        .setup(mPullToRefreshLayout1);
    }
    if (TAB_3.equals(tabId)) {
        //updateTab(R.id.tab_2);
         mCurrentTab = 2;
        // Toast.makeText(getApplicationContext(), "3No Guest list for Today", Toast.LENGTH_SHORT).show();
         mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#fff498"));
   ActionBarPullToRefresh.from(this)
        // Mark All Children as pullable
        .allChildrenArePullable()
        // Set the OnRefreshListener
        .listener(this)
        // Finally commit the setup to our PullToRefreshLayout
        .setup(mPullToRefreshLayout2);
    }
}