ViewPager中的碎片不刷新内容或其视图

时间:2015-07-17 08:12:41

标签: android android-fragments android-listview

MainActivity.java

public class MainActivity extends ActionBarActivity {

    Toolbar toolbar;
    ViewPager pager;
    ViewPagerAdapter adapter;
    SlidingTabLayout tabs;
    private ArrayList<String> mLanguagesArray = null;
    int Numboftabs;

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

    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            mLanguagesArray = new ArrayList<String>();
            mLanguagesArray.addAll(extras.getStringArrayList("languages"));
            Numboftabs = mLanguagesArray.size();
        } 
    }

        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setIcon(R.drawable.ic_launcher);

        adapter = new ViewPagerAdapter(getSupportFragmentManager(),
                mLanguagesArray, Numboftabs, MainActivity.this);

        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);

        // Assiging the Sliding Tab Layout View
        tabs = (SlidingTabLayout) findViewById(R.id.tabs);

        tabs.setViewPager(pager);
        tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int arg0) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageSelected(int arg0) {
                refreshListInFragment(arg0);
            }
        });
    }
    protected void refreshListInFragment(int position) {
        String currentLanguage = mLanguagesArray.get(position);
        FragmentManager fm = getSupportFragmentManager();
        ListviewFragment fragment = (ListviewFragment) fm
                .findFragmentById(R.id.pager);
        fragment.loadListview(currentLanguage);
    }
}

ViewPagerAdapter.java

import java.util.ArrayList;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    ArrayList<String> Titles;
    int NumbOfTabs;
    Context mContext;

    public ViewPagerAdapter(FragmentManager fm,
            ArrayList<String> mLanguagesArray, int mNumbOfTabsumb,
            Context context) {
        super(fm);
        Titles = new ArrayList<String>();
        this.Titles = mLanguagesArray;
        this.NumbOfTabs = mNumbOfTabsumb;
        this.mContext = context;

    }
    @Override
    public Fragment getItem(int position) {
        ListviewFragment listfragment = new ListviewFragment(mContext);
        return listfragment;
    }

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

    @Override
    public int getCount() {
        return NumbOfTabs;
    }
}

ListviewFragment.java

import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;

public class ListviewFragment extends Fragment {
    ProgressBar progressbar;
    ListView list;
    ArrayList<RStore> mRstoreList = null;
    RadioListAdapter adapter;
    Context mContext;
    Button tryagain;
    String mCurrentLanguage = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.listviewfragment, container, false);
        progressbar = (ProgressBar) v.findViewById(R.id.progressbar);
        list = (ListView) v.findViewById(R.id.listview);
        if (ListviewFragment.this.isMenuVisible()) {
            if (mRstoreList == null) {
                loadListview("English");
            }
        }
        return v;
    }

    public void loadListview(final String currentlanguage) {
        this.mCurrentLanguage = currentlanguage;
        mRstoreList = new ArrayList<RStore>();
        new AsyncTask<Void, Void, String>() {
            private String jsonStr;
            @Override
            protected void onPreExecute() {
                list.setVisibility(View.GONE);
                progressbar.setVisibility(View.VISIBLE);
                tryagain.setVisibility(View.GONE);
                super.onPreExecute();
            }
            @Override
            protected String doInBackground(Void... arg0) {
                try {
                    String URL = ServiceUrls.MUSIC_PHP;
                    CustomHttpClient sh = new CustomHttpClient();
                    JSONObject jsonobjpost = new JSONObject();
                    jsonobjpost.put("languages", currentlanguage);
                    jsonStr = sh.makeServiceCall(URL, CustomHttpClient.POST,
                            jsonobjpost.toString());
                    System.out.println("MUSIC_PHP_Response " + jsonStr);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return jsonStr;
            }

            @Override
            protected void onPostExecute(String result) {
                if (result == null || result.trim().equalsIgnoreCase("null")
                        || result.trim().equalsIgnoreCase("")) {
                    progressbar.setVisibility(View.GONE);
                    super.onPostExecute(result);
                    return;
                }
                try {
                    String DUMMYJASON = result.toString();
                    JSONObject jsonObject = new JSONObject(DUMMYJASON);
                    JSONArray jsonArray = jsonObject.getJSONArray("Language");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject obj = jsonArray.getJSONObject(i);
                        String id = obj.getString("id");
                        String language = obj.getString("language");
                        String tittle = obj.getString("Tittle");
                        String link = obj.getString("link");
                        RStore storecon = new RStore();
                        storecon.setId(id);
                        storecon.setLanguage(language);
                        storecon.setTitle(tittle);
                        storecon.setLink(link);
                        mRstoreList.add(storecon);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                adapter = new RadioListAdapter(mContext, mRstoreList);
                list.setAdapter(adapter);
                list.setVisibility(View.VISIBLE);
                progressbar.setVisibility(View.GONE);
                super.onPostExecute(result);
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }
    public ListviewFragment(Context context) {
        this.mContext = context;
    }
}

我正在使用一个名为ListviewFragment的片段类动态创建多个片段,并将其设置为视图页面适配器。 ListviewFragment包含listview,它是从服务动态填充的。使用我的代码一切正常,唯一的问题是,有时片段内的数据不会刷新或使用新值更新。在我的情况下,listview显示以前加载的数据,但在后台调用服务并更新listview适配器,但更新的列表视图未显示在片段中。如果我犯了任何错误,请查看我的代码并纠正我。提前谢谢

1 个答案:

答案 0 :(得分:0)

我没有使用findFragmentById调用带有(currentLanguage)作为活动片段中参数的方法,而是在ViewPagerAdapter getItem()方法中传递(currentLanguage)作为bundle,而在ListViewFragment类中使用了onCreateView(currentlanguage)使用getArguments将值传递给(loadListview)方法。现在一切都很好,干净。

ViewPagerAdapter.java

@Override
public Fragment getItem(int position) {
    Bundle args = new Bundle();
    args.putString("language", Titles.get(position));
    ListviewFragment f = new ListviewFragment(mContext);
    f.setArguments(args);
    return f;
} 

ListviewFragment.java

private void getarugs() {
    Bundle args = getArguments();
    mCurrentLanguage = args.getString("language");
    loadListview(mCurrentLanguage);
}