Android:从 viewPager 中的 Fragment 为 ListView 的适配器调用 getview()
Posted
技术标签:
【中文标题】Android:从 viewPager 中的 Fragment 为 ListView 的适配器调用 getview()【英文标题】:Android: calling getview() for ListView's Adapter from Fragment in viewPager 【发布时间】:2015-11-28 18:06:52 【问题描述】:我有 ViewPager
包含 3 个不同的 Fragment
。每个Fragment
都包含不同的View
和ListView
,当我试图在ViewPager
的Fragment
之一中显示ListView
时遇到问题,它没有显示任何内容。我试图调试我的adapter
,似乎我的getView()
方法没有被调用。我尝试不从ViewPager
调用我的片段,结果是getView()
从适配器调用,ListView
正在显示。从ViewPager?
显示ListView
是否有任何问题我已经尝试通过从onViewCreated()
调用我的适配器来尝试this solution,但没有任何变化。那么我的方法有什么问题吗?这是我的代码:
用于管理 ViewPager 的 Fragment 类
public class Frag_Provider extends Fragment
private String[] tabsTitles = "TERDEKAT", "SEMUA", "PROVIDERKU";
String url = "";
List<ModelProvider> list_provider;
DB_Esehat db_esehat = null;
SQLiteDatabase db = null;
ContentLoadingProgressBar progressbar;
TabLayout tabLayout;
ViewPager pager;
public Frag_Provider ()
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
((MainActivity) getActivity()).custom_toolbar("Provider", R.color.toolbar_provider, R.color.toolbar_provider_dark);
View result=inflater.inflate(R.layout.fragment_provider, container, false);
list_provider = new ArrayList<ModelProvider>();
progressbar = (ContentLoadingProgressBar)result.findViewById(R.id.progressbar);
db_esehat = new DB_Esehat(getActivity());
db = db_esehat.getWritableDatabase();
db.delete("LST_PROVIDER", null, null);
pager=(ViewPager)result.findViewById(R.id.pager);
tabLayout = (TabLayout)result.findViewById(R.id.sliding_tabs);
url = getResources().getString(R.string.url_host)+getResources().getString(R.string.url_provider);
new ProviderTask(url).execute();
pager.setAdapter(buildAdapter(tabsTitles));
tabLayout.post(new Runnable()
@Override
public void run()
tabLayout.setupWithViewPager(pager);
);
return(result);
public class ProviderTask extends AsyncTask<String, Void, String>
String url = "";
public ProviderTask(String url)
this.url = url;
@Override
protected void onPreExecute()
super.onPreExecute();
progressbar.setVisibility(View.VISIBLE);
@Override
protected String doInBackground(String... params)
String result = "";
try
result = Connection.get(url);
catch (Exception e)
result = "";
return result;
@Override
protected void onPostExecute(String result)
progressbar.setVisibility(View.GONE);
pager.setVisibility(View.VISIBLE);
tabLayout.setVisibility(View.VISIBLE);
super.onPostExecute(result);
if (result.equals("") || result.equals(null))
MethodSupport.AlertDialog(getActivity());
else
try
JSONArray Data = new JSONArray(result);
for (int i = 0; i < Data.length(); i++)
String LSKA_NOTE = "";
String RSALAMAT = "";
String RSTELEPON = "";
String RSNAMA = "";
String MAPPOS = "";
int RSTYPE = 0;
int RSID = 0;
int RS_NTT = 0;
JSONObject json = Data.getJSONObject(i);
if (json.has("LSKA_NOTE"))
LSKA_NOTE = json.getString("LSKA_NOTE");
if (json.has("RSALAMAT"))
RSALAMAT = json.getString("RSALAMAT");
if (json.has("RSTELEPON"))
RSTELEPON = json.getString("RSTELEPON");
if (json.has("RSNAMA"))
RSNAMA = json.getString("RSNAMA");
if (json.has("MAPPOS"))
MAPPOS = json.getString("MAPPOS");
if (json.has("RSTYPE"))
RSTYPE = json.getInt("RSTYPE");
if (json.has("RSID"))
RSID = json.getInt("RSID");
if (json.has("RS_NTT"))
RS_NTT = json.getInt("RS_NTT");
db_esehat.InsertRS(LSKA_NOTE, RSALAMAT, RSTELEPON, RSNAMA, MAPPOS, RSTYPE, RSID, RS_NTT);
catch (Exception e)
Log.d("TES", e.getMessage());
private PagerAdapter buildAdapter(String[] tabsTitles)
return(new FragmentStatePagerAdapter(getActivity(), getChildFragmentManager(),tabsTitles));
这是 FragmentStatePagerAdapter.java
public class FragmentStatePagerAdapter extends FragmentPagerAdapter
Context ctxt=null;
private String[] tabsTitles;
public FragmentStatePagerAdapter(Context ctxt, FragmentManager mgr, String[] tabsTitles)
super(mgr);
this.ctxt=ctxt;
this.tabsTitles = tabsTitles;
@Override
public int getCount()
return tabsTitles.length;
@Override
public Fragment getItem(int position)
switch(position)
case 0:
return Frag_Provider_Terdekat.newInstance(position);
case 1:
return Frag_Provider_Semua.newInstance(position);
case 2:
return Frag_Provider_Ku.newInstance(position);
return null;
// @Override public float getPageWidth(int position) return(0.7f);
@Override
public String getPageTitle(int position)
return tabsTitles[position];
这是我的 Fragment_Provider.xml,用于管理我的 ViewPager
的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
android:orientation="vertical">
<android.support.v4.widget.ContentLoadingProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleLarge"
android:layout_
android:layout_
android:layout_centerInParent="true"
android:visibility="gone"
android:indeterminate="false" />
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_
android:layout_
app:tabMaxWidth="0dp"
app:tabGravity="fill"
style="@style/MyCustomTabLayout"
app:tabMode="fixed"
android:fillViewport="true"
android:visibility="gone" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_
android:layout_
android:layout_weight="1"
android:background="@android:color/white"
android:layout_below="@id/sliding_tabs"
android:visibility="gone"/>
</RelativeLayout>
这是我在ViewPager
包含ListView
的片段:
public class Frag_Provider_Terdekat extends Fragment
private static final String KEY_POSITION="position";
private ListView list_provider;
List<ModelProviderTerdekat> list_ekamedicare;
DB_Esehat db_esehat;
SQLiteDatabase db;
ProviderTerdekatAdapter adapter;
static Frag_Provider_Terdekat newInstance(int position)
Frag_Provider_Terdekat frag=new Frag_Provider_Terdekat();
Bundle args=new Bundle();
args.putInt(KEY_POSITION, position);
frag.setArguments(args);
return(frag);
static String getTitle(Context ctxt, int position)
return("PROVIDER KU");
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
View result=inflater.inflate(R.layout.fragment_child_providerterdekat, container, false);
list_provider = (ListView)result.findViewById(R.id.list_provider);
list_ekamedicare = new ArrayList<ModelProviderTerdekat>();
db_esehat = new DB_Esehat(getActivity());
list_ekamedicare = db_esehat.getProvider();
adapter = new ProviderTerdekatAdapter(getActivity().getApplicationContext(), R.layout.adapter_provider, list_ekamedicare);
list_provider.setAdapter(adapter);
return result;
@Override
public void onViewCreated(View view, Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
@Override
public void onAttach(Activity activity)
super.onAttach(activity);
@Override
public void onDetach()
super.onDetach();
这是我的ListView
Adapter
public class ProviderTerdekatAdapter extends ArrayAdapter<ModelProviderTerdekat>
List<ModelProviderTerdekat> data = Collections.emptyList();
private LayoutInflater inflater;
private Context context;
static class ViewHolder
ImageView imvprov_map;
ImageView imvprov_fav;
TextView textprov_nama_rs;
TextView textprov_alamat_rs;
TextView textprov_km_rs;
public ProviderTerdekatAdapter (Context context, int viewResourceId, List<ModelProviderTerdekat> data)
super(context, R.layout.adapter_provider, data);
this.context = context;
inflater = LayoutInflater.from(context);
this.data = data;
@Override
public int getCount()
return data.size();
@Override
public long getItemId(int arg0)
return 0;
@Override
public View getView(int position, View view, ViewGroup parent)
if (view == null)
view = inflater.inflate(R.layout.adapter_provider, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.imvprov_map = (ImageView) view.findViewById(R.id.imvprov_map);
viewHolder.imvprov_fav = (ImageView) view.findViewById(R.id.imvprov_fav);
viewHolder.textprov_nama_rs = (TextView) view.findViewById(R.id.textprov_nama_rs);
viewHolder.textprov_alamat_rs = (TextView) view.findViewById(R.id.textprov_alamat_rs);
viewHolder.textprov_km_rs = (TextView) view.findViewById(R.id.textprov_km_rs);
view.setTag(viewHolder);
ViewHolder viewHolder = (ViewHolder) view.getTag();
viewHolder.textprov_nama_rs.setText(data.get(position).getRSNAMA());
viewHolder.textprov_alamat_rs.setText(data.get(position).getRSALAMAT());
return view;
我不知道为什么我的GetView()
没有在我的Adapter
中调用,是因为我输入了ViewPager
吗?好吧,我希望有人了解它并帮助我解决我的问题。非常感谢。
【问题讨论】:
你调试了吗。运行时的列表大小是多少。 Collections.emptyList() 返回一个不可变列表,即您无法向其中添加元素的列表。尝试使用 new ListViewPager
时没有调用getView()
方法,我已经尝试了你的建议但仍然没有调用
列表大小为真或大于 0?它不是布尔值。
并将您的 getItemId() 返回类型更改为返回 arg0;
【参考方案1】:
终于..我找到了解决我的问题的方法,这是因为我在更改为LinearLayout
后将ViewPager
放入了RelativeLayout
,所有视图都按我想要的方式显示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_
android:layout_
android:orientation="vertical">
<android.support.v4.widget.ContentLoadingProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleLarge"
android:layout_
android:layout_
android:layout_centerInParent="true"
android:visibility="gone"
android:indeterminate="false" />
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_
android:layout_
app:tabMaxWidth="0dp"
app:tabGravity="fill"
style="@style/MyCustomTabLayout"
app:tabMode="fixed"
android:fillViewport="true"
android:visibility="gone" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_
android:layout_
android:layout_weight="1"
android:background="@android:color/white"
android:layout_below="@id/sliding_tabs"
android:visibility="gone"/>
</LinearLayout>
【讨论】:
以上是关于Android:从 viewPager 中的 Fragment 为 ListView 的适配器调用 getview()的主要内容,如果未能解决你的问题,请参考以下文章
android - 将数据从 Activity 传递到 ViewPager 中的 Fragment
Android:从 viewPager 中的 Fragment 为 ListView 的适配器调用 getview()