java - 如何在 Activity 底部使用带有选项卡 View 的 ChrisBanes 的 ActionBar-PullToRefresh 库

标签 java android tabs pull-to-refresh

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

我在我的应用中使用屏幕底部的标签。正如您在屏幕截图中看到的那样。 我通读了示例代码。他说您所要做的就是像这样将可刷新 View 包装在 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

最佳答案

将所有 ListView 放入 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 相同

在 Activity 的 onCreate 中的 java 中:

    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);
    }
}

关于java - 如何在 Activity 底部使用带有选项卡 View 的 ChrisBanes 的 ActionBar-PullToRefresh 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20769425/

相关文章:

java - Android:具有两个不同 View 的 EfficientAdapter

android - 如何在 Android 中处理丢失的 KeyStore 密码?

ios - iOS 开发中的选项卡栏 Controller 。为什么当我切换标签时它会重新运行所有代码?

php - 如何在我的帐户页面的 woocommerce 中重新排序自定义选项卡?

java - OpenSSL 始终无法验证 Java 中 Bouncy CaSTLe 生成的签名

java - SQLite中如何进行递归查询?

java - 如何实现Java可比接口(interface)?

html - Angular 4 MaterializeCSS ngfor 和标签

java - hibernate 抛出 NullPointerException

java - 将字符串写入文件,用户从 &lt;input type ="file"> 中选择